6.maven继承(慕课网)
场景: 在maven中,多个项目之间会重复使用同一个构建,我们可以将其抽出来单独作为父构建,使新项目继承此父构建。
1.新建一个maven项目,命名为project-parent 作为父构建,配置其pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--抽出部分属性,作为公共部分,后期版本有变动方便维护 -->
<junit.version>4.11</junit.version>
</properties>
<!--定义在dependencyManagement里的构建在本项目中不会被依赖下载 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--使用properties里定义的量 使用类似于EL表达式进行调用 -->
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.在demo项目中继承此父构建
<!--继承父构建 -->
<parent>
<groupId>com.steven.maven</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!--依赖列表-->
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<!--使用继承的构建-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>