eclipse把java web项目转为maven项目

自己开发一个项目,建立的是dynamic web project,结果提交代码交付给运维人员时,他们要求用maven项目,说这样更好管理。苦于我以前maven用的太少了,那就学吧。

1.转换项目类型

右键项目,看到有Configure选项,直接Convert To Maven Project,整个项目目录会发生变化,如图

多了maven的标志性文件pom.xml,在编辑框弹出如下

OK,已经转为了maven项目,这里可以编辑你的版本,Artifact Id等信息。

2.编辑pom.xml

点击下面的pom.xml,进入pom的编辑模式。

在普通的项目中我们只需要添加一些依赖项,我们可以到http://www.mvnrepository.com/去找相关的依赖包。

从项目的Referenced Libraries子目录可以看到自己的依赖包,比如我的项目需要mahout包,那么我去网站搜索mahout,根据自己需要的包及版本,进入到以下页面。

可以看到有个xml标记语言的dependency,把这个复制到自己的pom.xml中。下面是我的pom.xml部分代码。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>SmartPush</groupId>  
  5.     <artifactId>SmartPush</artifactId>  
  6.     <packaging>war</packaging>  
  7.   
  8.     <dependencies>  
  9.   
  10.         <dependency>  
  11.             <groupId>org.apache.mahout</groupId>  
  12.             <artifactId>mahout-core</artifactId>  
  13.             <version>0.9</version>  
  14.         </dependency>  
  15.         <dependency>  
  16.             <groupId>org.apache.mahout</groupId>  
  17.             <artifactId>mahout-math</artifactId>  
  18.             <version>0.9</version>  
  19.         </dependency>  
  20.         <dependency>  
  21.             <groupId>mysql</groupId>  
  22.             <artifactId>mysql-connector-java</artifactId>  
  23.             <version>5.1.30</version>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>log4j</groupId>  
  27.             <artifactId>log4j</artifactId>  
  28.             <version>1.2.17</version>  
  29.         </dependency>  
  30.   
  31.   
  32.     </dependencies>  
  33.   
  34.     <build>  
  35.         <sourceDirectory>src</sourceDirectory>  
  36.         <resources>  
  37.             <resource>  
  38.                 <directory>src</directory>  
  39.                 <excludes>  
  40.                     <exclude>**/*.java</exclude>  
  41.                 </excludes>  
  42.             </resource>  
  43.         </resources>  
  44.         <plugins>  
  45.             <plugin>  
  46.                 <artifactId>maven-compiler-plugin</artifactId>  
  47.                 <version>3.1</version>  
  48.                 <configuration>  
  49.                     <source>1.7</source>  
  50.                     <target>1.7</target>  
  51.                 </configuration>  
  52.             </plugin>  
  53.             <plugin>  
  54.                 <artifactId>maven-war-plugin</artifactId>  
  55.                 <version>2.3</version>  
  56.                 <configuration>  
  57.                     <warSourceDirectory>WebContent</warSourceDirectory>  
  58.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  59.                 </configuration>  
  60.             </plugin>  
  61.         </plugins>  
  62.     </build>  
  63.     <version>0.0.1</version>  
  64. </project>  

3.编译

进入到自己的项目目录,执行以下命令

  1. $mvn compile  
  2.   
  3. $mvn package  
你就可以在target目录下看到需要的war文件。

猜你喜欢

转载自blog.csdn.net/weikzhao0521/article/details/52779450