Maven多项目依赖配置

来源:互联网 发布:python 矩阵 查看 编辑:程序博客网 时间:2024/06/05 09:44
niweiwei

Maven多项目依赖配置

    博客分类:
  • Maven
 

最近在学习Maven,把一个开源的项目改成maven管理,期间使用到了多项目,从网上查阅了一些资料,主要参考的是http://kyfxbl.iteye.com/blog/1680045,在此把自己的一些心得体会写出来,供大家学习交流。

关于maven的安装,在此就不进行阐述,请参考网上其他教程。

本实例由4个项目组成,其中,aggregator是父工程,同时承担聚合模块和父模块的作用,没有实际代码和资源文件open-plagform-common是公共的java工程;open-platfor-web是公共的web文件,主要包括cssjs等;open-bug-m是最终要发布的应用,形成war包。

一、建立一个Maven工程:aggregator

/aggregator

   /src/main/java

   /src/test/java

   pom.xml

 

 

此工程主要是父模块,聚合其他工程,没有实际代码和资源文件,最主要的是pom.xml文件,其主要内容如下:

Xml代码  收藏代码
  1. <modelVersion>4.0.0</modelVersion>  
  2.   <groupId>cn.jess.platform</groupId>  
  3.   <artifactId>aggregator</artifactId>  
  4.   <version>0.0.1-SNAPSHOT</version>  
  5. <!-- 因为是父工程 ,因此此处的packaging必须为pom -->  
  6.   <packaging>pom</packaging>  
  7.   <name>aggregator</name>  
  8.     
  9.   <modules>    
  10.     <module>../open-platform-common</module>    
  11.     <module>../open-platform-web</module>    
  12.     <module>../open-bug-m</module>  
  13.   </modules>  
  14.     
  15.   <!-- 配置部署的远程仓库 -->    
  16.   <distributionManagement>    
  17.     <snapshotRepository>    
  18.       <id>nexus-snapshots</id>    
  19.       <name>nexus distribution snapshot repository</name>    
  20.       <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>    
  21.     </snapshotRepository>    
  22.   </distributionManagement>  
  23.     
  24.   <build>    
  25.      <pluginManagement>    
  26.        <plugins>    
  27.   
  28.              <plugin>    
  29.                  <groupId>org.apache.maven.plugins</groupId>    
  30.                  <artifactId>maven-resources-plugin</artifactId>    
  31.                  <version>2.6</version>    
  32.                  <configuration>    
  33.                      <encoding>UTF-8</encoding>    
  34.                  </configuration>    
  35.              </plugin>    
  36.   
  37.              <plugin>    
  38.                  <groupId>org.apache.maven.plugins</groupId>    
  39.                  <artifactId>maven-compiler-plugin</artifactId>    
  40.                  <version>2.5.1</version>    
  41.                  <configuration>    
  42.                      <encoding>UTF-8</encoding>  
  43.                      <source>1.6</source>  
  44.                      <target>1.6</target>    
  45.                  </configuration>    
  46.              </plugin>    
  47.   
  48.          </plugins>    
  49.      </pluginManagement>    
  50.  </build>    
  51.     
  52.  <dependencyManagement>    
  53.   
  54.      <dependencies>    
  55.   
  56.          <dependency>    
  57.              <groupId>com.sun</groupId>    
  58.              <artifactId>tools</artifactId>    
  59.              <version>1.6.0</version>    
  60.              <scope>system</scope>    
  61.              <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>    
  62.          </dependency>    
  63.   
  64.      </dependencies>    
  65.   
  66.  </dependencyManagement>  

    二、建立一个Maven工程:open-platform-common

 

此工程主要是项目中使用到的公共java类库,pom文件主要内容如下:

 

Xml代码  收藏代码
  1. <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程-->  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <artifactId>open-platform-common</artifactId>  
  4.  <!-- 因为此工程要发布到webapp的lib目录下,因此为jar(不知道这样解释对否?) -->  
  5.  <packaging>jar</packaging>  
  6.   <properties>  
  7.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  8.   </properties>    
  9.     <!-- 指定Maven仓库 -->  
  10.     <repositories>  
  11.         <!-- my的maven仓库 -->  
  12.         <repository>  
  13.             <id>myRepository</id>  
  14.             <name>local private nexus</name>  
  15.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  16.             <releases>  
  17.                 <enabled>true</enabled>  
  18.             </releases>  
  19.             <snapshots>  
  20.                 <enabled>true</enabled>  
  21.             </snapshots>  
  22.         </repository>  
  23.     </repositories>  
  24.   <!-- 指定maven plugin仓库 -->  
  25.     <pluginRepositories>  
  26.         <!-- oschina的maven plugin仓库 -->  
  27.         <pluginRepository>  
  28.             <id>myPluginRepository</id>  
  29.             <name>local private nexus</name>  
  30.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  31.             <releases>  
  32.                 <enabled>true</enabled>  
  33.             </releases>  
  34.             <snapshots>  
  35.                 <enabled>false</enabled>  
  36.             </snapshots>  
  37.         </pluginRepository>  
  38.     </pluginRepositories>  
  39.   <dependencies>  
  40.         <!-- 此处的类库根据自己的需要进行添加 -->  
  41.   </dependencies>  
  42.   <!-- 用来指定父工程-->  
  43.   <parent>  
  44.     <groupId>cn.jess.platform</groupId>  
  45.     <artifactId>aggregator</artifactId>  
  46.     <version>0.0.1-SNAPSHOT</version>  
  47.     <relativePath>../aggregator</relativePath>  
  48.   </parent>  

    三、建立一个Maven工程:open-platform-web

    此工程主要是项目中使用到的公共web文件pom文件主要内容如下:

 

Xml代码  收藏代码
  1. <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程-->  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <artifactId>open-platform-web</artifactId>  
  4. <!-- 因为此工程要发布到webapp应用的根目录下,因此为war(不知道这样解释对否?) -->  
  5.   <packaging>war<ng>  
  6.   <properties>  
  7.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  8.   </properties>    
  9.     <!-- 指定Maven仓库 -->  
  10.     <repositories>  
  11.         <!-- my的maven仓库 -->  
  12.         <repository>  
  13.             <id>myRepository</id>  
  14.             <name>local private nexus</name>  
  15.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  16.             <releases>  
  17.                 <enabled>true</enabled>  
  18.             </releases>  
  19.             <snapshots>  
  20.                 <enabled>true</enabled>  
  21.             </snapshots>  
  22.         </repository>  
  23.     </repositories>  
  24.   <!-- 指定maven plugin仓库 -->  
  25.     <pluginRepositories>  
  26.         <!-- oschina的maven plugin仓库 -->  
  27.         <pluginRepository>  
  28.             <id>myPluginRepository</id>  
  29.             <name>local private nexus</name>  
  30.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  31.             <releases>  
  32.                 <enabled>true</enabled>  
  33.             </releases>  
  34.             <snapshots>  
  35.                 <enabled>false</enabled>  
  36.             </snapshots>  
  37.         </pluginRepository>  
  38.     </pluginRepositories>  
  39.     
  40.   <parent>  
  41.     <groupId>cn.jess.platform</groupId>  
  42.     <artifactId>aggregator</artifactId>  
  43.     <version>0.0.1-SNAPSHOT</version>  
  44.     <relativePath>../aggregator</relativePath>  
  45.   </parent>  
  46. </project>  

    注意:此工程的WEB-INF目录下必须包含web.xml文件,否则在执行mvn时会报错

    四、建立一个Maven工程:open-bug-m

此工程是最终要发布的应用,其依赖于open-platform-commonopen-platform-web,因此在pom文件中要加入这两个工程的依赖,pom文件内容如下所示:

 

Xml代码  收藏代码
  1. <groupId>open-bug-m</groupId>  
  2.   <artifactId>open-bug-m</artifactId>  
  3.   <packaging>war</packaging>  
  4.   <name/>  
  5.   <description/>  
  6.   <properties>  
  7.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  8.   </properties>  
  9.   <parent>  
  10.     <groupId>cn.jess.platform</groupId>  
  11.     <artifactId>aggregator</artifactId>  
  12.     <version>0.0.1-SNAPSHOT</version>  
  13.     <relativePath>../aggregator</relativePath>  
  14.   </parent>   
  15.     <!-- 指定Maven仓库 -->  
  16.     <repositories>  
  17.         <!-- my的maven仓库 -->  
  18.         <repository>  
  19.             <id>myRepository</id>  
  20.             <name>local private nexus</name>  
  21.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  22.             <releases>  
  23.                 <enabled>true</enabled>  
  24.             </releases>  
  25.             <snapshots>  
  26.                 <enabled>true</enabled>  
  27.             </snapshots>  
  28.         </repository>  
  29.     </repositories>  
  30.   <!-- 指定maven plugin仓库 -->  
  31.     <pluginRepositories>  
  32.         <!-- oschina的maven plugin仓库 -->  
  33.         <pluginRepository>  
  34.             <id>myPluginRepository</id>  
  35.             <name>local private nexus</name>  
  36.             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>  
  37.             <releases>  
  38.                 <enabled>true</enabled>  
  39.             </releases>  
  40.             <snapshots>  
  41.                 <enabled>false</enabled>  
  42.             </snapshots>  
  43.         </pluginRepository>  
  44.     </pluginRepositories>  
  45.   <dependencies>  
  46.     <dependency>  
  47.       <groupId>cn.jess.platform</groupId>  
  48.       <artifactId>open-platform-common</artifactId>  
  49.       <version>0.0.1-SNAPSHOT</version>  
  50.       <type>jar</type>  
  51.     </dependency>  
  52.     <dependency>  
  53.       <groupId>cn.jess.platform</groupId>  
  54.       <artifactId>open-platform-web</artifactId>  
  55.       <version>0.0.1-SNAPSHOT</version>  
  56.       <type>war</type>  
  57.     </dependency>        
  58.     <!-- 此处的类库根据自己的需要进行添加 -->  
  59.   
  60.       
  61.   </dependencies>  
  62.   <build>  
  63.     <finalName>open-bug</finalName>  
  64.     <plugins>  
  65.       <plugin>    
  66.           <groupId>org.apache.maven.plugins</groupId>    
  67.           <artifactId>maven-war-plugin</artifactId>  
  68.           <version>2.3</version>    
  69.           <configuration>    
  70.              <packagingExcludes>WEB-INF/web.xml</packagingExcludes>      
  71.              <overlays>    
  72.                 <overlay>    
  73.                   <groupId>cn.jess.platform</groupId>    
  74.                   <artifactId>open-platform-web</artifactId>    
  75.                 </overlay>    
  76.              </overlays>    
  77.           </configuration>    
  78.       </plugin>    
  79.       <plugin>    
  80.         <groupId>org.codehaus.cargo</groupId>    
  81.         <artifactId>cargo-maven2-plugin</artifactId>    
  82.         <version>1.2.3</version>    
  83.         <configuration>    
  84.           <container>    
  85.             <containerId>tomcat7x</containerId>    
  86.             <home>F:\apache-tomcat-7.0.42(x64)</home>    
  87.           </container>    
  88.           <configuration>    
  89.             <type>existing</type>    
  90.             <home>F:\apache-tomcat-7.0.42(x64)</home>    
  91.             <properties>    
  92.               <cargo.jvmargs>    
  93.                   -Xdebug                    -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787    
  94.               </cargo.jvmargs>    
  95.             </properties>    
  96.           </configuration>    
  97.         </configuration>    
  98.         <executions>    
  99.           <execution>    
  100.              <id>cargo-run</id>    
  101.              <phase>pre-integration-test</phase>    
  102.              <goals>    
  103.                  <goal>run</goal>    
  104.              </goals>    
  105.           </execution>    
  106.         </executions>    
  107.     </plugin>   
  108.     </plugins>  
  109.   </build>  

    关于maven-war-plugincargo-maven2-plugin的使用方法请参考网上其他使用教程。

所有上述四个工程准备就绪后,执行mvn install就可对工程项目进行部署。

现在存在的一个问题主要是:

如果把open-platform-commonopen-platform-web工程合并为一个,则在open-bug-m不知道如何去引用它?

 

 

 

分享到:
Tomcat在eclipse启动报错 |导入2万多条数据时报 MySQL server has go ...
  • 2013-10-28 17:25
  • 浏览 42128
  • 评论(2)
  • 分类:开源软件
  • 相关推荐
参考知识库
人工智能知识库10945  关注 | 521  收录
Python知识库20495  关注 | 1340  收录
Java SE知识库23693  关注 | 471  收录
微信开发知识库19144  关注 | 779  收录
评论
2 楼 smartdog 2017-02-06  
使用的maven的版本很老的,而且写的不是很清楚,建议可以参考下http://juvenshun.iteye.com/blog/305865
1 楼 spring_springmvc 2015-08-10  
可以参考最新的文档:
如何在eclipse jee中检出项目并转换为Maven project,最后转换为Dynamic web project,地址:http://www.zuidaima.com/blog/1618180875144192.htm
如何在eclipse jee中创建Maven project并且转换为Dynamic web project
地址:http://www.zuidaima.com/blog/1618162161323008.htm
发表评论

您还没有登录,请您登录后再发表评论

0 0