Maven多项目依赖配置

    xiaoxiao2021-03-25  105

    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代码   <modelVersion>4.0.0</modelVersion>    <groupId>cn.jess.platform</groupId>    <artifactId>aggregator</artifactId>    <version>0.0.1-SNAPSHOT</version>  <!-- 因为是父工程 ,因此此处的packaging必须为pom -->    <packaging>pom</packaging>    <name>aggregator</name>        <modules>        <module>../open-platform-common</module>        <module>../open-platform-web</module>        <module>../open-bug-m</module>    </modules>        <!-- 配置部署的远程仓库 -->      <distributionManagement>        <snapshotRepository>          <id>nexus-snapshots</id>          <name>nexus distribution snapshot repository</name>          <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>        </snapshotRepository>      </distributionManagement>        <build>         <pluginManagement>           <plugins>                   <plugin>                     <groupId>org.apache.maven.plugins</groupId>                     <artifactId>maven-resources-plugin</artifactId>                     <version>2.6</version>                     <configuration>                         <encoding>UTF-8</encoding>                     </configuration>                 </plugin>                   <plugin>                     <groupId>org.apache.maven.plugins</groupId>                     <artifactId>maven-compiler-plugin</artifactId>                     <version>2.5.1</version>                     <configuration>                         <encoding>UTF-8</encoding>                       <source>1.6</source>                       <target>1.6</target>                     </configuration>                 </plugin>               </plugins>         </pluginManagement>     </build>         <dependencyManagement>           <dependencies>               <dependency>                 <groupId>com.sun</groupId>                 <artifactId>tools</artifactId>                 <version>1.6.0</version>                 <scope>system</scope>                 <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>             </dependency>           </dependencies>       </dependencyManagement>   <modelVersion>4.0.0</modelVersion> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 因为是父工程 ,因此此处的packaging必须为pom --> <packaging>pom</packaging> <name>aggregator</name> <modules> <module>../open-platform-common</module> <module>../open-platform-web</module> <module>../open-bug-m</module> </modules> <!-- 配置部署的远程仓库 --> <distributionManagement> <snapshotRepository> <id>nexus-snapshots</id> <name>nexus distribution snapshot repository</name> <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencyManagement> <dependencies> <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.6.0</version> <scope>system</scope> <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath> </dependency> </dependencies> </dependencyManagement>

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

     

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

     

    Xml代码   <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程-->    <modelVersion>4.0.0</modelVersion>    <artifactId>open-platform-common</artifactId>   <!-- 因为此工程要发布到webapp的lib目录下,因此为jar(不知道这样解释对否?) -->   <packaging>jar</packaging>    <properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>        <!-- 指定Maven仓库 -->      <repositories>          <!-- my的maven仓库 -->          <repository>              <id>myRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>true</enabled>              </snapshots>          </repository>      </repositories>    <!-- 指定maven plugin仓库 -->      <pluginRepositories>          <!-- oschina的maven plugin仓库 -->          <pluginRepository>              <id>myPluginRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>false</enabled>              </snapshots>          </pluginRepository>      </pluginRepositories>    <dependencies>          <!-- 此处的类库根据自己的需要进行添加 -->    </dependencies>    <!-- 用来指定父工程-->    <parent>      <groupId>cn.jess.platform</groupId>      <artifactId>aggregator</artifactId>      <version>0.0.1-SNAPSHOT</version>      <relativePath>../aggregator</relativePath>    </parent>   <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程--> <modelVersion>4.0.0</modelVersion> <artifactId>open-platform-common</artifactId> <!-- 因为此工程要发布到webapp的lib目录下,因此为jar(不知道这样解释对否?) --> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 指定Maven仓库 --> <repositories> <!-- my的maven仓库 --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin仓库 --> <pluginRepositories> <!-- oschina的maven plugin仓库 --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <dependencies> <!-- 此处的类库根据自己的需要进行添加 --> </dependencies> <!-- 用来指定父工程--> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent>

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

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

     

    Xml代码   <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程-->    <modelVersion>4.0.0</modelVersion>    <artifactId>open-platform-web</artifactId>  <!-- 因为此工程要发布到webapp应用的根目录下,因此为war(不知道这样解释对否?) -->    <packaging>war<ng>    <properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>        <!-- 指定Maven仓库 -->      <repositories>          <!-- my的maven仓库 -->          <repository>              <id>myRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>true</enabled>              </snapshots>          </repository>      </repositories>    <!-- 指定maven plugin仓库 -->      <pluginRepositories>          <!-- oschina的maven plugin仓库 -->          <pluginRepository>              <id>myPluginRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>false</enabled>              </snapshots>          </pluginRepository>      </pluginRepositories>        <parent>      <groupId>cn.jess.platform</groupId>      <artifactId>aggregator</artifactId>      <version>0.0.1-SNAPSHOT</version>      <relativePath>../aggregator</relativePath>    </parent>  </project>   <!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程--> <modelVersion>4.0.0</modelVersion> <artifactId>open-platform-web</artifactId> <!-- 因为此工程要发布到webapp应用的根目录下,因此为war(不知道这样解释对否?) --> <packaging>war<ng> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 指定Maven仓库 --> <repositories> <!-- my的maven仓库 --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin仓库 --> <pluginRepositories> <!-- oschina的maven plugin仓库 --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent> </project>

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

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

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

     

    Xml代码   <groupId>open-bug-m</groupId>    <artifactId>open-bug-m</artifactId>    <packaging>war</packaging>    <name/>    <description/>    <properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <parent>      <groupId>cn.jess.platform</groupId>      <artifactId>aggregator</artifactId>      <version>0.0.1-SNAPSHOT</version>      <relativePath>../aggregator</relativePath>    </parent>       <!-- 指定Maven仓库 -->      <repositories>          <!-- my的maven仓库 -->          <repository>              <id>myRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>true</enabled>              </snapshots>          </repository>      </repositories>    <!-- 指定maven plugin仓库 -->      <pluginRepositories>          <!-- oschina的maven plugin仓库 -->          <pluginRepository>              <id>myPluginRepository</id>              <name>local private nexus</name>              <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>              <releases>                  <enabled>true</enabled>              </releases>              <snapshots>                  <enabled>false</enabled>              </snapshots>          </pluginRepository>      </pluginRepositories>    <dependencies>      <dependency>        <groupId>cn.jess.platform</groupId>        <artifactId>open-platform-common</artifactId>        <version>0.0.1-SNAPSHOT</version>        <type>jar</type>      </dependency>      <dependency>        <groupId>cn.jess.platform</groupId>        <artifactId>open-platform-web</artifactId>        <version>0.0.1-SNAPSHOT</version>        <type>war</type>      </dependency>            <!-- 此处的类库根据自己的需要进行添加 -->            </dependencies>    <build>      <finalName>open-bug</finalName>      <plugins>        <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-war-plugin</artifactId>            <version>2.3</version>              <configuration>                 <packagingExcludes>WEB-INF/web.xml</packagingExcludes>                   <overlays>                    <overlay>                      <groupId>cn.jess.platform</groupId>                      <artifactId>open-platform-web</artifactId>                    </overlay>                 </overlays>              </configuration>          </plugin>          <plugin>            <groupId>org.codehaus.cargo</groupId>            <artifactId>cargo-maven2-plugin</artifactId>            <version>1.2.3</version>            <configuration>              <container>                <containerId>tomcat7x</containerId>                <home>F:\apache-tomcat-7.0.42(x64)</home>              </container>              <configuration>                <type>existing</type>                <home>F:\apache-tomcat-7.0.42(x64)</home>                <properties>                  <cargo.jvmargs>                      -Xdebug                    -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787                  </cargo.jvmargs>                </properties>              </configuration>            </configuration>            <executions>              <execution>                 <id>cargo-run</id>                 <phase>pre-integration-test</phase>                 <goals>                     <goal>run</goal>                 </goals>              </execution>            </executions>        </plugin>       </plugins>    </build>   <groupId>open-bug-m</groupId> <artifactId>open-bug-m</artifactId> <packaging>war</packaging> <name/> <description/> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent> <!-- 指定Maven仓库 --> <repositories> <!-- my的maven仓库 --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin仓库 --> <pluginRepositories> <!-- oschina的maven plugin仓库 --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-common</artifactId> <version>0.0.1-SNAPSHOT</version> <type>jar</type> </dependency> <dependency> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-web</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> <!-- 此处的类库根据自己的需要进行添加 --> </dependencies> <build> <finalName>open-bug</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <packagingExcludes>WEB-INF/web.xml</packagingExcludes> <overlays> <overlay> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-web</artifactId> </overlay> </overlays> </configuration> </plugin> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.3</version> <configuration> <container> <containerId>tomcat7x</containerId> <home>F:\apache-tomcat-7.0.42(x64)</home> </container> <configuration> <type>existing</type> <home>F:\apache-tomcat-7.0.42(x64)</home> <properties> <cargo.jvmargs> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 </cargo.jvmargs> </properties> </configuration> </configuration> <executions> <execution> <id>cargo-run</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </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
    发表评论

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

    转载请注明原文地址: https://ju.6miu.com/read-9014.html

    最新回复(0)