在项目中使用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键值对的对应关系,这样在其他位置就可以直接输出这些已经配置过的公共参数。
示例如下:
- <!-- 配置参数 -->
- <properties>
- <maven.compile.source>1.7</maven.compile.source>
- <maven.compile.target>1.7</maven.compile.target>
- <maven.compiler.version>2.3.2</maven.compiler.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <commons.encoding>UTF-8</commons.encoding>
- <jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>
- <!-- version -->
- <major.version>1</major.version>
- <minor.version>0</minor.version>
- <!-- SNAPSHOT -->
- <version.type>-SNAPSHOT</version.type>
- <full.version>${major.version}.${minor.version}${version.type}</full.version>
- <configure.maven.version>${full.version}</configure.maven.version>
- <hibernate5.version>5.2.1.Final</hibernate5.version>
- </properties>
<dependencyManagement>作用顾名思义是对dependency依赖的管理,在传统项目中我们只需要配置
- <dependencies>
- <dependencie>
- ...
- </dependencie>
- </dependencies>
依赖标签就可以使用Maven来自动装载相关依赖项目,然而在多个项目中这样的配置往往相同,在开发与维护过程中经常会出现偏差。所以Maven就提供了<dependencyManagement>标签来管理这些依赖,在子模块中就可以复用。
示例如下:
- <!-- 依赖 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
配置完成后,完整的pom文件内容为:
- <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 <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.example</groupId>
- <artifactId>parent.project</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>pom</packaging>
- <name>parent.project</name>
- <description>Maven</a>父工程</description>
- <modules>
- <module>module.project</module>
- </modules>
- <!-- 配置参数 -->
- <properties>
- <maven.compile.source>1.7</maven.compile.source>
- <maven.compile.target>1.7</maven.compile.target>
- <maven.compiler.version>2.3.2</maven.compiler.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <commons.encoding>UTF-8</commons.encoding>
- <jdk.path>${JAVA_HOME}/lib/tools.jar</jdk.path>
- <!-- version -->
- <major.version>1</major.version>
- <minor.version>0</minor.version>
- <!-- SNAPSHOT -->
- <version.type>-SNAPSHOT</version.type>
- <full.version>${major.version}.${minor.version}${version.type}</full.version>
- <configure.maven.version>${full.version}</configure.maven.version>
- <hibernate5.version>5.2.1.Final</hibernate5.version>
- </properties>
- <!-- 依赖 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
2)子模块pom.xml文件配置
父工程中我们看到已经配置了很多公共参数与依赖,接下来我们就要在子模块的pom文件中使用这些配置。
首先配置dependency,在子模块中我们与其他普通项目一样只需要配置所使用的dependencie即可,如下所示:
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- </dependency>
- </dependencies>
可以看到我们在<dependency>中并不再需要配置<version>参数,因为version参数已经在父工程中指定。
然后是复用<properties>中的配置参数,以build插件为例,配置如下:
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>${maven.compiler.version}</version>
- <configuration>
- <source>${maven.compile.source}</source>
- <target>${maven.compile.target}</target>
- <encoding>${project.build.sourceEncoding}</encoding>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
可以看到其中各项参数均使用的是父工程中<properties>所配置的值,这样既配置统一又利于维护。具体值是否引用成功可以在Effective POM标签下直接查看。
二 引用其他工程配置
虽然一父多子的项目结构可以将整个项目重构的更加整洁,但并未关联的项目间如果也需要有相同的配置Maven将如何处理?
其实Maven在这一点上支持的还是不够强大,也许是出于安全或其他考虑吧,其只能引用其他项目中的<dependencyManagement>所配置的依赖项目。所以还做不到配置文件的组合。
1.创建configure项目
configure项目的作用就是用来存储各种dependency依赖的配置,在创建过程中与创建父子工程相同,不同之处为子模块在打包选择项中也选择pom类型,这样整个工程就变成了一个纯配置文件的工程,只用于存储配置信息。工程创建完成后结构如下所示:
每一个子模块都是一种框架的依赖配置信息,这样添加与维护及其方便。
以hibernate为例,打开其(configure.maven.hibernate工程)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>
- <parent>
- <groupId>com.vanilla</groupId>
- <artifactId>configure.maven</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <artifactId>configure.maven.hibernate</artifactId>
- <packaging>pom</packaging>
- <name>configure.maven.hibernate</name>
- <description>hibernate配置</description>
- <!-- 版本配置 -->
- <properties>
- <hibernate5.version>5.2.1.Final</hibernate5.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>${hibernate5.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
其中没有太特殊的配置,也仅仅声明了hibernate的dependency依赖及版本。
2.引入configure配置信息
在父工程(parent.project)的pom.xml文件中直接引入相关配置即可,如想引入hibernate的依赖配置,则配置信息如下:
- <!-- 依赖 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.demo</groupId>
- <artifactId>configure.maven.hibernate</artifactId>
- <version>${configure.maven.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
这样一来就直接把模块configure.maven.hibernate的<dependencyManagement>部分引入到了这里,也不需要配置版本号什么的了。
3.子模块使用dependencyManagement配置
在子模块中使用dependency跟之前一样,如下:
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- </dependency>
- </dependencies>
通过以上对Maven的相关配置,我们就可以很好的复用某些重复的配置,并做到统一管理,统一维护的目的。