普通项目转为maven项目及相关操作说明 (转)

2013-07-16  文晶 

原文地址:http://mopishv0.blog.163.com/blog/static/54455932201303171612893/
1 原项目简述

普通项目转为maven项目及相关操作说明 - mopishv0 - MopishCrajorV0

 

         如图,一般的项目大致包括三类路径:src,源码路径;test,单元测试路径;lib第三方类包路径。

         示例项目中,BaseDao类依赖于mysql-connector-java-5.1.15-bin.jarSimTest类为依赖JUnit4的单元测试类。

2 将项目转为maven项目 2.1 添加pom.xml文件

    eclipse中邮件单击项目选择Maven->Enable Dependecy Management。如下图:

普通项目转为maven项目及相关操作说明 - mopishv0 - MopishCrajorV0

 

         然后根据提示,设定maven项目信息。

普通项目转为maven项目及相关操作说明 - mopishv0 - MopishCrajorV0

 

         之后生成的pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sogou</groupId>

  <artifactId>example.NormalToMaven</artifactId>

  <version>0.0.1-SNAPSHOT</version>

</project>

2.2 pom.xml中添加资源库和上传部署资源库

         为了能够正确下载依赖的第三方jar包,需要在pom.xml中配置我们的资源库。添加如下配置:

<repositories>

      <repository>

         <id>sogou.repo</id>  

         <url>

http://***/nexus/content/groups/public

</url>

         <name>Sogou Repository</name>

      </repository>

      <repository>

         <id>sogou.repo.old</id>

         <url>

http://***/nexus/content/groups/public

</url>

         <name>Old Sogou Repository</name>

      </repository>

   </repositories>

为了能将项目的jar包上传到我们的资源库并被其他maven项目依赖,需要在pom.xml中配置上传资源库。添加如下配置:

<distributionManagement>

   <repository>

      <id>sogou-wd-releases</id>

      <name>Sogou Release Distribution Repository</name>

      <url>

http://***/nexus/content/repositories/Release

</url>

   </repository>

   <snapshotRepository>

      <id>sogou-wd-snapshot</id>

      <name>Sogou Snapshot Repository</name>

      <url>

http://***/nexus/content/repositories/snapshots

</url>

   </snapshotRepository>

</distributionManagement>

         为了保证项目的jar包能正确上传,还需要确定本地maven的配置文件settings.xml中有对应上传资源库的用户名和密码。其配置如下:

   <servers>

      <server>

         <id>sogou-wd-releases</id>

         <username>你的用户名</username>

         <password>你的密码</password>

      </server>

      <server>

         <id>sogou-wd-snapshot</id>

         <username>你的用户名</username>

         <password>你的密码</password>

      </server>

   </servers>

   其中id要与之前配置的上传资源库id对应。默认为LDAP中的用户名(不带@及之后的用户名)和密码,如果无效可以找谭博侃申请。

         此外在settings.xml中可以配置我们的资源库为mirror,以我们的资源库为所有远程资源库的镜像,加快jar包的下载。其配置如下:

<mirrors>

<mirror>

          <id>nexus-sogou</id>

          <mirrorOf>*</mirrorOf>

<url>

http://***/nexus/content/groups/public

</url>

</mirror>

</mirrors>

 

当包括其他的所有配置都完成后,执行mvn clean deploy成功后,会在http://cloud.sogou-inc.com/nexus/中看到刚刚上传的jar包。如图:

普通项目转为maven项目及相关操作说明 - mopishv0 - MopishCrajorV0

 


2.3 pom.xml中添加依赖jar

以本项目为例,其配置如下:

   <dependencies>

      <dependency>

         <groupId>mysql</groupId>

         <artifactId>mysql-connector-java</artifactId>

         <version>5.1.16</version>

      </dependency>

      <dependency>

         <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <version>4.10</version>

         <type>jar</type>

         <scope>test</scope>

      </dependency>

</dependencies>


如果不知道依赖的第三方jar包的配置,可在http://***/nexus/ 中搜索。如下图:

普通项目转为maven项目及相关操作说明 - mopishv0 - MopishCrajorV0

 

2.4 配置源码和测试路径

         由于一般在eclipse中开发的项目路径都不符合maven默认源码和测试路径,因此要对源码路径进行配置,否则maven不会进行编译和测试。以本项目为例,其配置如下:

<build>

      <testSourceDirectory>test</testSourceDirectory>

      <plugins>

         <plugin>

            <groupId>org.codehaus.mojo</groupId>

            <artifactId>build-helper-maven-plugin</artifactId>

            <version>1.4</version>

            <executions>

                <execution>

                   <id>add-source</id>

                   <phase>generate-sources</phase>

                   <goals>

                      <goal>add-source</goal>

                   </goals>

                   <configuration>

                      <sources>

                         <source>src</source>

                      </sources>

                   </configuration>

                </execution>         

            </executions>

         </plugin>

         <plugin>

            <groupId>org.apahce.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>2.5</version>

            <configuration>

                <testClassesDirectory>

target/classes-test

</testClassesDirectory>

                <testSourceDirectory>test</testSourceDirectory>

                <includes>

                   <include>**/*.java</include>

                </includes>

            </configuration>

         </plugin>

      </plugins>

</build>

其中插件org.codehaus.mojo. build-helper-maven-plugin用于添加源码路径。testSourceDirectory org.apahce.maven.plugins. maven-surefire-plugin用于指定测试源码路径。

2.5 其他配置

         <properties>

<project.build.sourceEncoding>

GBK

</project.build.sourceEncoding>

      <compileSource>1.6</compileSource>

   </properties>

         其中project.build.sourceEncoding代表项目中源码文件所使用的编码。compileSource代表编译版本。

3 web项目的maven配置

         Web项目与示例项目不同,一方面需要打成war包;另一方面在转换成maven项目后,打包时需要将不同文件拷贝到不同路径下,这也是我们原有的项目不符合maven web项目文件路径的标准造成的。因此,除了之上的配置外还须一下配置。

3.1 配置打包

<plugin>

<groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-war-plugin</artifactId>

   <version>2.1-beta-1</version>

   <configuration>

   <webXml>WebContent/WEB-INF/web.xml</webXml>

      <archive>

<addMavenDescriptor>

false

</addMavenDescriptor>

      </archive>

      <webResources>

         <resource>

            <directory>src</directory>

            <includes>

               <include>**/struts.xml</include>

            </includes>

            <filtering>true</filtering>

            <targetPath>WEB-INF/classes</targetPath>

         </resource>

         <resource>

            <directory>WebContent</directory>

            <filtering>true</filtering>

            <targetPath>.</targetPath>

         </resource>

      </webResources>

</configuration>

</plugin>

其中webXml指定了web.xml文件所在的文件夹,如果插件无法找到web.xml则会打包失败。webResources表示在打包时资源文件夹需要移动到的指定目录。每个resourcedirectory代表资源文件原路径,可以用includesexcludes对文件进行过滤。targetPath为移动到的目录。

 

3.2 配置clean

         Web项目中,需要额外清理WebContent/WEB-INF中的内容。对此,其配置如下:

<plugin>

            <artifactId>maven-clean-plugin</artifactId>

            <configuration>

                <filesets>

                   <fileset>

                      <directory>

WebContent/WEB-INF

</directory>

                      <includes>

                         <include>lib</include>

                         <include>classes</include>

                      </includes>

                      <followSymlinks>false</followSymlinks>

                   </fileset>

                </filesets>

            </configuration>

         </plugin>


620°/6187 人阅读/2 条评论 发表评论

付民  2013-07-18

图片看不见


文晶  2013-07-19

付民: 图片看不见
看原文


登录 后发表评论