Maven聚合工程的父工程packing必须为pom。
父工程用于管理子工程,不进行业务实现,因此src目录可以选择性删除。
1.新建一个maven工程
2.修改父工程的pom.xml,设置打包方式为pom。
<?xml version="1.0" encoding="UTF-8"?>
<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.qfedu</groupId><artifactId>maven-pro</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
删去src文件。
3.创建子工程
创建成功后,发现子工程commen的pom继承了父工程。
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>maven-pro</artifactId><groupId>com.qfedu</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>common</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
而父工程的pom中,自动生成了一个<module>标签,代表被子工程继承。
<?xml version="1.0" encoding="UTF-8"?>
<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.qfedu</groupId><artifactId>maven-pro</artifactId><version>1.0-SNAPSHOT</version><modules><module>common</module></modules><packaging>pom</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
4.再创建一个spring子工程。
选择依赖。
发现springboot项目继承了spring-boot-starter-parent,而不是继承了父maven项目。
而父工程的pom中也没有引入新创建的springboot项目。
此时可以选择手动继承。(没必要)
删去spring-boot-starter-parent继承,将父工程maven继承拷贝上去。
<?xml version="1.0" encoding="UTF-8"?>
<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><parent><artifactId>maven-pro</artifactId><groupId>com.qfedu</groupId><version>1.0-SNAPSHOT</version></parent><groupId>com.qfedu</groupId><artifactId>usersystem</artifactId><version>0.0.1-SNAPSHOT</version><name>usersystem</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
<?xml version="1.0" encoding="UTF-8"?>
<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.qfedu</groupId><artifactId>maven-pro</artifactId><version>1.0-SNAPSHOT</version><modules><module>common</module><module>usersystem</module></modules><packaging>pom</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
另一种方式:
在common的java下新建一个包 com.qfedu.beans。
在新建的包中新建一个类 Goods 。
在usersystem中新建一个service层,GoodsServiceImpl类。
在usersystem中添加依赖:
<dependency><groupId>com.qfedu</groupId><artifactId>common</artifactId>
</dependency>
打包common。
此时,在GoodsServiceImpl.java中,可以直接使用Goods!
GoodsServiceImpl.java:
package com.qfedu.service;import com.qfedu.beans.Goods;public class GoodsServiceImpl {private Goods goods;
}