Maven基础
Maven的聚合和继承
继承
何为继承?
- 继承是为了消除重复,我们把很多相同的配置提取出来
- 例如:grouptId,version等
a.新建工程Parent将公共配置提取并配置相应的内容
新建一个Maven工程Parent,修改Pom.xml文件,通过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.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
在Hello项目中的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>
<artifactId>Hello</artifactId>
<packaging>jar</packaging>
<name>Hello</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
</project>
HelloFriend的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>
<artifactId>HelloFriend</artifactId>
<packaging>jar</packaging>
<name>HelloFriend</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
</project>
在MakeFriend中的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>
<artifactId>MakeFriend</artifactId>
<packaging>jar</packaging>
<name>MakeFriend</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
</project>
b.解决工程配置出错问题
完成上述配置,此时工程会报错(Hello工程下多了HelloFriend依赖)
![df886b6f32b4048d56307a2e378a2d69.png](https://i-blog.csdnimg.cn/blog_migrate/cb5b05b7b58d821cbed68ad82ed14d97.png)
解决方案:在Parent项目中在<dependencies>......</dependencies>标签外层多加一层标签为<dependencyManagement></dependencyManagement>
完成配置会看到如下错误,所有的maven依赖都不存在了
![db7903b60872f02b84d131cccf4795b5.png](https://i-blog.csdnimg.cn/blog_migrate/01053d8b79a40ef7596d3182d440f1b1.png)
![4aa7ad5db91b1b152c1aab8e5ae8e7d6.png](https://i-blog.csdnimg.cn/blog_migrate/21a721430967f061bf7c43a17c6b3c61.png)
进一步进行配置,解决工程配置问题:修改每个项目中的pom.xml依赖
<1>Hello项目中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>
<artifactId>Hello</artifactId>
<packaging>jar</packaging>
<name>Hello</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>
<2>修改HelloFriend中的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>
<artifactId>HelloFriend</artifactId>
<packaging>jar</packaging>
<name>HelloFriend</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>Hello</artifactId>
</dependency>
</dependencies>
</project>
<3>MakeFriend中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>
<artifactId>MakeFriend</artifactId>
<packaging>jar</packaging>
<name>MakeFriend</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
</dependency>
</dependencies>
</project>
完成上述配置,工程恢复正常:
![e8644df10b9a0810b04ee56339b5a4ab.png](https://i-blog.csdnimg.cn/blog_migrate/b24c324d36572e415b66c62f5070b6a4.png)
优点:使用继承是为了统一管理每个使用的jar包的版本。由Parent项目中统一定义版本
缺点:Hello、HelloFriend 、MakeFriend Parent每个项目都需要执行Install安装
聚合
如果想要执行每个子工程都需要:编辑---测试—打包—部署
可以在父工程中统一定义
![a4bb50b684ff60bb40f954a22c78d76e.png](https://i-blog.csdnimg.cn/blog_migrate/82735cb350a34c323480f2c2048ffecb.png)
- 统一设置属性
<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.guigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<url>http://maven.apache.org</url>
<!-- 执行每个子程序:编辑、测试、打包、部署 -->
<modules>
<module>../Hello</module>
<module>../HelloFriend</module>
<module>../MakeFriend</module>
</modules>
<!-- 此处统一设置属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit-version>4.12</junit-version>
<log4j-version>1.2.17</log4j-version>
<maven-version>0.0.1-SNAPSHOT</maven-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>${maven-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.guigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>${maven-version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>