• 让天下没有难学的技术
    多数学员都来自推荐,这就是口碑的力量

Maven继承机制

为什么需要继承机制?

由于非compile范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置。例如:

此时如果项目需要将各个模块的junit版本统一为4.9,那么到各个工程中手动修改无疑是非常不可取的。使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。

java培训

2 创建父工程

创建父工程和创建一般的Java工程操作一致,唯一需要注意的是:打包方式处要设置为pom。

3 在子工程中引用父工程

<parent>

<!– 父工程坐标 –>

<groupId>…</groupId>

<artifactId>…</artifactId>

<version>…</version>

<relativePath>从当前目录到父项目的pom.xml文件的相对路径</relativePath>

</parent>

<parent> <groupId>com.atguigu.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <!– 指定从当前子工程的pom.xml文件出发,查找父工程的pom.xml的路径 –> <relativePath>../Parent/pom.xml</relativePath></parent>

此时如果子工程的groupId和version如果和父工程重复则可以删除。

4 在父工程中管理依赖

将Parent项目中的dependencies标签,用dependencyManagement标签括起来

<dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies></dependencyManagement>

在子项目中重新指定需要的依赖,删除范围和版本号

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency></dependencies>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注