Maven多项目聚合配置

        在项目中使用Maven可以大大简化开发及构建的过程,但一旦产品线庞大,包含的项目及模块繁多时,各模块间版本管理就会产生不一致现象,从而给维护及开发带来了不少难题。

        一般情况下我们采用 project -- modules形式聚合多个关联项目或模块,这样子模块就可以使用父工程中的相关配置。

 

        一 父子工程配置

        1.创建父工程(parent project)

        1)首先利用开发工具eclipse创建父工程,在workspace中右键——>选择New——>选择project,在创建窗口中找到Maven Project选项,如下图所示:

 

        2)选择工程类型

        这里需要注意的是需要勾选上图两个选项,否则将进入模板选择界面,从而无法创建父工程。


        3)填写工程详细信息

        填写相应工程信息,在Packaging选择项中选择pom,然后点击Finish完成创建。

 

        4)这样创建完的父工程结构如下:


        其中只会含有一个有效的pom文件,src文件夹没有其他作用的话也可以删除。

 

        2.创建子工程(module project)

        1)首先打开父工程(这里为parent.project)的pom.xml文件,打开后,eclipse插件会显示为如下界面:


 

        2)点击Modules标签下的 "Create..."按钮,将弹出子模块的创建界面,类似于创建父工程时的选择界面,在模块详细信息填写界面中可以选择模块的类型(pom、jar、war三种),如下图所示:

       

        3)此里我们选择的打包类型为jar,项目结构则如下所示:


 

        工程中包括了我们熟悉的Java项目的相关目录。

 

        3.pom.xml文件配置

        创建完父子工程后需要修改其pom.xml的配置信息,从而达到子模块复用父工程已配置的参数信息。

        1)配置父工程的pom.xml
        在父工程的pom文件中添加<properties><dependencyManagement>标签。

        <properties>标签的作用类似于properties资源文件的作用,提供key于value键值对的对应关系,这样在其他位置就可以直接输出这些已经配置过的公共参数。

        示例如下:

Xml代码   收藏代码
  1. <!-- 配置参数 -->  
  2. <properties>  
  3.     <maven.compile.source>1.7</maven.compile.source>  
  4.     <maven.compile.target>1.7</maven.compile.target>  
  5.     <maven.compiler.version>2.3.2</maven.compiler.version>  
  6.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  7.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  8.     <commons.encoding>UTF-8</commons.encoding>  
  9.     <jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>  
  10.     <!-- version -->  
  11.     <major.version>1</major.version>  
  12.     <minor.version>0</minor.version>  
  13.     <!-- SNAPSHOT -->  
  14.     <version.type>-SNAPSHOT</version.type>  
  15.     <full.version>${major.version}.${minor.version}${version.type}</full.version>  
  16.     <configure.maven.version>${full.version}</configure.maven.version>  
  17.     <hibernate5.version>5.2.1.Final</hibernate5.version>  
  18. </properties>  

 

        <dependencyManagement>作用顾名思义是对dependency依赖的管理,在传统项目中我们只需要配置

Xml代码   收藏代码
  1. <dependencies>  
  2.     <dependencie>  
  3.      ...   
  4.     </dependencie>  
  5. </dependencies>  

        依赖标签就可以使用Maven来自动装载相关依赖项目,然而在多个项目中这样的配置往往相同,在开发与维护过程中经常会出现偏差。所以Maven就提供了<dependencyManagement>标签来管理这些依赖,在子模块中就可以复用。

        示例如下:

Xml代码   收藏代码
  1. <!-- 依赖 -->  
  2. <dependencyManagement>  
  3.     <dependencies>  
  4.         <dependency>  
  5.             <groupId>org.hibernate</groupId>  
  6.             <artifactId>hibernate-core</artifactId>  
  7.             <version>${hibernate5.version}</version>  
  8.         </dependency>  
  9.         <dependency>  
  10.             <groupId>org.hibernate</groupId>  
  11.             <artifactId>hibernate-entitymanager</artifactId>  
  12.             <version>${hibernate5.version}</version>  
  13.         </dependency>  
  14.     </dependencies>  
  15. </dependencyManagement>  

        配置完成后,完整的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 <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd%22%3E%3CmodelVersion%3E4.0.0%3C/modelVersion%3E%3CgroupId%3Ecom.example%3C/groupId%3E%3CartifactId%3Eparent.project%3C/artifactId%3E%3Cversion%3E1.0-SNAPSHOT%3C/version%3E%3Cpackaging%3Epom%3C/packaging%3E%3Cname%3Eparent.project%3C/name%3E%3Cdescription%3EMaven">http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.example</groupId>  
  5.     <artifactId>parent.project</artifactId>  
  6.     <version>1.0-SNAPSHOT</version>  
  7.     <packaging>pom</packaging>  
  8.     <name>parent.project</name>  
  9.     <description>Maven</a>父工程</description>  
  10.     <modules>  
  11.         <module>module.project</module>  
  12.     </modules>  
  13.   
  14.     <!-- 配置参数 -->  
  15.     <properties>  
  16.         <maven.compile.source>1.7</maven.compile.source>  
  17.         <maven.compile.target>1.7</maven.compile.target>  
  18.         <maven.compiler.version>2.3.2</maven.compiler.version>  
  19.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  20.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  21.         <commons.encoding>UTF-8</commons.encoding>  
  22.         <jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>  
  23.         <!-- version -->  
  24.         <major.version>1</major.version>  
  25.         <minor.version>0</minor.version>  
  26.         <!-- SNAPSHOT -->  
  27.         <version.type>-SNAPSHOT</version.type>  
  28.         <full.version>${major.version}.${minor.version}${version.type}</full.version>  
  29.         <configure.maven.version>${full.version}</configure.maven.version>  
  30.         <hibernate5.version>5.2.1.Final</hibernate5.version>  
  31.     </properties>  
  32.   
  33.     <!-- 依赖 -->  
  34.     <dependencyManagement>  
  35.         <dependencies>  
  36.             <dependency>  
  37.                 <groupId>org.hibernate</groupId>  
  38.                 <artifactId>hibernate-core</artifactId>  
  39.                 <version>${hibernate5.version}</version>  
  40.             </dependency>  
  41.             <dependency>  
  42.                 <groupId>org.hibernate</groupId>  
  43.                 <artifactId>hibernate-entitymanager</artifactId>  
  44.                 <version>${hibernate5.version}</version>  
  45.             </dependency>  
  46.         </dependencies>  
  47.     </dependencyManagement>  
  48. </project>  

 

        2)子模块pom.xml文件配置

        父工程中我们看到已经配置了很多公共参数与依赖,接下来我们就要在子模块的pom文件中使用这些配置。

        首先配置dependency,在子模块中我们与其他普通项目一样只需要配置所使用的dependencie即可,如下所示:

Xml代码   收藏代码
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>org.hibernate</groupId>  
  4.         <artifactId>hibernate-core</artifactId>  
  5.     </dependency>  
  6.     <dependency>  
  7.         <groupId>org.hibernate</groupId>  
  8.         <artifactId>hibernate-entitymanager</artifactId>  
  9.     </dependency>  
  10. </dependencies>  

        可以看到我们在<dependency>中并不再需要配置<version>参数,因为version参数已经在父工程中指定。

 

        然后是复用<properties>中的配置参数,以build插件为例,配置如下:

Xml代码   收藏代码
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-compiler-plugin</artifactId>  
  7.                 <version>${maven.compiler.version}</version>  
  8.                 <configuration>  
  9.                     <source>${maven.compile.source}</source>  
  10.                     <target>${maven.compile.target}</target>  
  11.                     <encoding>${project.build.sourceEncoding}</encoding>  
  12.                 </configuration>  
  13.             </plugin>  
  14.         </plugins>  
  15.     </pluginManagement>  
  16. </build>  

        可以看到其中各项参数均使用的是父工程中<properties>所配置的值,这样既配置统一又利于维护。具体值是否引用成功可以在Effective POM标签下直接查看。

 

        二 引用其他工程配置

        虽然一父多子的项目结构可以将整个项目重构的更加整洁,但并未关联的项目间如果也需要有相同的配置Maven将如何处理?

        其实Maven在这一点上支持的还是不够强大,也许是出于安全或其他考虑吧,其只能引用其他项目中的<dependencyManagement>所配置的依赖项目。所以还做不到配置文件的组合。

        1.创建configure项目

        configure项目的作用就是用来存储各种dependency依赖的配置,在创建过程中与创建父子工程相同,不同之处为子模块在打包选择项中也选择pom类型,这样整个工程就变成了一个纯配置文件的工程,只用于存储配置信息。工程创建完成后结构如下所示:



        每一个子模块都是一种框架的依赖配置信息,这样添加与维护及其方便。

        以hibernate为例,打开其(configure.maven.hibernate工程)pom.xml文件,内容如下:

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.     <parent>  
  5.         <groupId>com.vanilla</groupId>  
  6.         <artifactId>configure.maven</artifactId>  
  7.         <version>1.0-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>configure.maven.hibernate</artifactId>  
  10.     <packaging>pom</packaging>  
  11.     <name>configure.maven.hibernate</name>  
  12.     <description>hibernate配置</description>  
  13.     <!-- 版本配置 -->  
  14.     <properties>  
  15.         <hibernate5.version>5.2.1.Final</hibernate5.version>  
  16.     </properties>  
  17.     <dependencyManagement>  
  18.         <dependencies>  
  19.             <dependency>  
  20.                 <groupId>org.hibernate</groupId>  
  21.                 <artifactId>hibernate-core</artifactId>  
  22.                 <version>${hibernate5.version}</version>  
  23.             </dependency>  
  24.             <dependency>  
  25.                 <groupId>org.hibernate</groupId>  
  26.                 <artifactId>hibernate-entitymanager</artifactId>  
  27.                 <version>${hibernate5.version}</version>  
  28.             </dependency>  
  29.         </dependencies>  
  30.     </dependencyManagement>  
  31. </project>  

        其中没有太特殊的配置,也仅仅声明了hibernate的dependency依赖及版本。

 

        2.引入configure配置信息

        在父工程(parent.project)的pom.xml文件中直接引入相关配置即可,如想引入hibernate的依赖配置,则配置信息如下:

Xml代码   收藏代码
  1. <!-- 依赖 -->  
  2. <dependencyManagement>  
  3.     <dependencies>  
  4.         <dependency>  
  5.             <groupId>com.demo</groupId>  
  6.             <artifactId>configure.maven.hibernate</artifactId>  
  7.             <version>${configure.maven.version}</version>  
  8.             <type>pom</type>  
  9.             <scope>import</scope>  
  10.         </dependency>  
  11.     </dependencies>  
  12. </dependencyManagement>  

        这样一来就直接把模块configure.maven.hibernate的<dependencyManagement>部分引入到了这里,也不需要配置版本号什么的了。

 

        3.子模块使用dependencyManagement配置

        在子模块中使用dependency跟之前一样,如下:

Xml代码   收藏代码
  1. <dependencies>  
  2.         <dependency>  
  3.             <groupId>org.hibernate</groupId>  
  4.             <artifactId>hibernate-core</artifactId>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.hibernate</groupId>  
  8.             <artifactId>hibernate-entitymanager</artifactId>  
  9.         </dependency>  
  10. </dependencies>  

 

        通过以上对Maven的相关配置,我们就可以很好的复用某些重复的配置,并做到统一管理,统一维护的目的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值