`

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文件,其主要内容如下:

<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文件主要内容如下:

 

<!-- 由于存在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文件主要内容如下:

 

<!-- 由于存在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文件内容如下所示:

 

<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不知道如何去引用它?

 

 

 

分享到:
评论
2 楼 smartdog 2017-02-06  
使用的maven的版本很老的,而且写的不是很清楚,建议可以参考下http://juvenshun.iteye.com/blog/305865

相关推荐

    maven项目的配置POM配置文件

    maven项目的配置POM配置文件,主要是maven项目的配置,直接从中央仓库下载内容。

    ​实现maven项目中多版本依赖兼容使用解决方案

    既能不升级jar包又能使用高版本依赖同时还不造成冲突的完美解决方案

    File 转 MultipartFile 和MultipartFile的maven配置

    File 转 MultipartFile 和需要的maven配置File 转 MultipartFile 和需要的maven配置File 转 MultipartFile 和需要的maven配置

    allatori第二代Java代码混淆器Maven打包配置,依赖库,以及配置文件

    allatori第二代Java代码混淆器Maven打包配置,依赖库,以及配置文件。示例运行,可行。

    Maven配置项目依赖使用本地仓库的方法汇总(小结)

    主要介绍了Maven配置项目依赖使用本地仓库的方法汇总(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    springboot+maven打包demo【将依赖与配置文件打包到jar包外部】

    这是一个springboot+maven写的demo,主要演示springboot项目通过maven插件,将依赖与配置文件打包到jar包外部,实现灵活的项目管理.仅供参考学习

    添加非托管到Maven项目依赖关系.doc

    一些Java应用程序都有依赖性,不可以在一个公共Maven存储库。本文档向您展示了如何在应用程序中添加这些库项目,告诉Maven如何找到他们…

    maven配置指南

    maven工具的使用 目 录 Maven 环境的配置 1. settings.xml存放路径 1 2. 设置Maven的环境变量 2 3. 配置 settings.xml 2 4. 安装Maven插件 2 ...11、MAVEN安装到私服(依赖于Maven Nexus配置) 11

    chm版本Maven教程

    Maven项目文档 Maven项目模板 Maven快照 Maven构建自动化 Maven依赖管理 Maven自动化部署 Maven Web应用 Eclispe IDE集成Maven NetBeans IDE集成Maven Eclipse构建Maven项目 转换基于Maven的Java项目支持Eclipse IDE...

    kettle7.2和8.2相关maven依赖包仓库

    kettle是国外开源的一款ETL集成工具,其最新版8.2版本改为了maven项目,由于其maven仓库下载实在太慢,这里提供了我费了一两天才下载好的依赖包,直接解压放在本地仓库即可。方便好使,好用的给个五星好评。

    apache-maven3.5 依赖包

    eclipse和Myeclipse项目开发中,maven项目管理依赖配置包

    一套SSM做项目用的Maven依赖

    做基于maven的SSM开发用这个就好了,不用到处找依赖了,完全免费,建议配置阿里云的maven镜像,会快一点

    软件开发+Maven技术+Maven安装与配置+基础课程

    课程从Maven的安装和配置起步,逐步深入到项目依赖管理、构建生命周期、插件使用等高级特性。学员将学习如何通过POM文件精确控制项目构建过程,如何利用Maven管理项目版本和依赖,以及如何通过Maven仓库进行依赖的...

    Maven2使用项目开发规范说明.doc

    《Maven2使用项目开发规范说明》,图文并茂,详细介绍了使用 Maven2 开发一个规范项目的流程。 目录: 1. 环境配置 3 1.1. 配置Maven2 3 1.2. 配置Eclipse环境变量MIA_LIB 4 1.3. 安装Maven2 的eclipse plugin 4 2. ...

    Maven的Settings的较为不错的文件以及配置介绍

    Maven是一个流行的Java项目管理工具,它使用一个名为settings.xml的配置文件来配置Maven的行为。settings.xml文件包含了Maven的全局设置,包括仓库位置、代理设置、构建配置等。 在Maven中,settings.xml文件通常...

    Maven介绍安装和配置详解

    2. **依赖管理**:Maven可以自动管理项目的依赖项,开发者只需要在pom.xml文件中指定依赖项的坐标,Maven就会自动下载并管理这些依赖项。 3. **自动化构建**:通过简单的命令行指令,Maven可以自动化地完成编译、...

    Maven配置阿里云镜像settings.xml文件

    在安装Maven构建工具后,Maven仓库镜像站点默认是国外的,因为网络原因,在构建项目时下载依赖文件会很慢,甚至有可能下载失败。所以我们一定要把仓库镜像站点改为国内的才能顺利下载,通常比较常用得是阿里云镜像,...

    maven:项目管理工具;安装、使用及配置

    Maven基于项目对象模型(Project Object Model,POM)来管理项目,通过定义一系列规范化的目录结构和配置文件来管理项目的构建过程和依赖关系。Maven的主要作用是提高Java项目的可维护性、可重用性和可扩展性。

    maven项目pom.xml最详细配置

    maven的pom.xml的最详细配置,内含pom的依赖、jdk配置等

    编程开发+Maven技术+Maven安装与配置+技术课程

    此外,课程还包括对Maven核心特性的深入讲解,如依赖管理、构建生命周期、插件使用等,以及如何通过自定义settings.xml文件来优化Maven项目的构建过程。我们的目标是让学员不仅能够安装和配置Maven,还能够灵活运用...

Global site tag (gtag.js) - Google Analytics