Maven继承和聚合

继承

1.情景:

  • javaProject1依赖的junit:4.0
  • javaProject2依赖的junit:4.0
  • javaProject1依赖的junit:4.9
    由于test范围的依赖不能传递,所以必然会分散在各个模块中,本版可能会不一致
    2.需求:统一各个模块工程对junit的依赖的版本。
    3.解决思路:将junit依赖统一提取到父工程中,在子工程中声明junit依赖时不指定的版本,以父工程中统一设定的为标准。也便于修改。
    4.操作步骤
    【1】创建一个Mvaen工程作为父工程,以pom方式打包
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.java.maven</groupId>
  <artifactId>Parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>

【2】在子工程中声明对父工程的引用

  <parent>
	<groupId>com.java.maven</groupId>
	<artifactId>Parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<relativePath>../Parent/pom.xml</relativePath>
</parent>

【3】将子工程中与父工程重复的坐标删除
【4】在父工程中统一junit依赖

<modelVersion>4.0.0</modelVersion>
	<groupId>com.java.maven</groupId>
	<artifactId>Parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.9</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

【5】在子工程中删除junit依赖的版本

	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<scope>test</scope>
  	</dependency>

聚合

1.作用:一键安装各个模块工程
2.配置方式:在总的聚合工程中配置各个参与聚合的模块。

	<modules>
		<module>../JavaProject01</module>
	</modules>

3.使用方式:只需要在总工程的pom.xml运行安装命令即可。

发布了127 篇原创文章 · 获赞 8 · 访问量 6268

猜你喜欢

转载自blog.csdn.net/OVO_LQ_Start/article/details/105244816