学习大数据——maven的继承

为什么需要继承机制?

  • 由于非compile范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置。例如:
    表格
  • 此时如果项目需要将各个模块的junit版本统一为4.9,那么到各个工程中手动修改无疑是非常不可取的。使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。

创建父工程

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

在子工程中引用父工程

Parent:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.learn.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>
</project>

Hello:

<?xml version="1.0" ?>
<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>

	<artifactId>Hello</artifactId>

	<name>Hello</name>

	<!-- 配置父工程 -->
	<parent>
		<groupId>com.learn.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<!-- 从当前工程出发查询父工程中的pom.xml文件的相对路径 -->
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>

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

HelloFriend:

<?xml version="1.0" ?>
<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>
	<artifactId>HelloFriend</artifactId>
	<name>HelloFriend</name>

	<!-- 配置父工程 -->
	<parent>
		<groupId>com.learn.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<!-- 从当前工程出发查询父工程中的pom.xml文件的相对路径 -->
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.learn.maven</groupId>
			<artifactId>Hello</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<!-- 解决maven test命令时console出现中文乱码问题 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.7.2</version>
				<configuration>
					<forkMode>once</forkMode><!--在一个进程中进行所有测试 ; 默认值:once -->
					<argLine>-Dfile.encoding=UTF-8</argLine>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

文档结构:
文档结构

发布了37 篇原创文章 · 获赞 7 · 访问量 687

猜你喜欢

转载自blog.csdn.net/qq_40394792/article/details/104328884