1.Parent 父项目: 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>
- <groupId>com.bj</groupId>
- <artifactId>parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>pom</packaging>
- <build/>
- <modules>
- <module>child3</module>
- </modules>
- <properties>
- <proj-version>0.0.1-SNAPSHOT</proj-version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.abc</groupId>
- <artifactId>test</artifactId>
- <version>${proj-version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
(1) 说明: proj-version 是自定义的, 可以随便命名. 这里用于统一进行版本标识.
(2) 获取 proj-version 的值, 使用 ${proj-version}.
2.Child 子项目: 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>
- <artifactId>parent</artifactId>
- <groupId>com.bj</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.bj</groupId>
- <artifactId>child</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <build/>
- <dependencies>
- <dependency>
- <groupId>com.abc</groupId>
- <artifactId>test</artifactId>
- </dependency>
- </dependencies>
- </project>
(1) 说明: 虽然子项目继承父项目, 但是如果在父项目中使用 <dependencyManagement > 标签, 子项目就没法使用父项目依赖的 jar 文件, 如果想在子项目中使用父项目依赖的 jar, 需要在子项目中添加如下:
- <dependencies>
- <dependency>
- <groupId>com.abc</groupId>
- <artifactId>test</artifactId>
- </dependency>
- </dependencies>
这样项目就可以使用父项目依赖的 jar 文件了, 且不用指定 jar 的版本号 version, 因为版本号统一在父项目的 pom.xml 中统一维护.
** 通过以上配置, maven 就可以统一对 jar 的版本进行统一管理了.**
来源: http://www.bubuko.com/infodetail-2790386.html