**
第一
**
**1.点击 file -->new–>project
**
2.选择maven 新建父级项目,名字为parent
**
切记父级项目的作用是 可以统一的约束所有项目的依赖版本,同一管理项目依赖版本
**
3.在该父级项目的pom.xml文件中,如下图所示
<?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>
<!--继承的父级项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--继承父级项目的依赖,不需要写版本号-->
<dependencies>
<!--SpringBoot aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
**
第二
**
1.接下来创建子级项目,继承父级项目依赖
父级项目名上–》右键–》new–》Module
2.起名字叫做manager
pom.xml文件为
<?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>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baigu</groupId>
<artifactId>manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>manager</name>
<description>Demo project for Spring Boot</description>
</project>
此时父级parent项目的pom.xml文件为
<?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>
<!--继承的父级项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--继承父级项目的依赖,不需要写版本号-->
<dependencies>
<!--SpringBoot aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<!--任何子级项目集成了该父级项目 需要在父级项目的pom.xml文件中做配置-->
<modules>
<module>manager</module>
</modules>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
**
另一种依赖的继承关系
项目选择性继承依赖
**
**父级项目的pom.xml文件
<?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>
<!--继承的父级项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--继承父级项目的依赖,不需要写版本号-->
<dependencies>
<!--SpringBoot aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<!--依赖管理 写在dependencyManagement里的依赖 是一种声明 并不会被直接继承到
子级项目如果想要继承 需要声明(配置依赖) 但是不需要写版本号 会依赖父级项目中的版本号-->
<dependencyManagement>
<dependencies>
<!--EasyPoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--任何子级项目集成了该父级项目 需要在父级项目的pom.xml文件中做配置-->
<modules>
<module>manager</module>
</modules>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
**
子级manager项目的pom.xml文件
<?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>
<dependencies>
<!--EasyPoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
</dependency>
</dependencies>
<!--继承自己的父级项目-->
<parent>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baigu</groupId>
<artifactId>manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>manager</name>
<description>Demo project for Spring Boot</description>
</project>
在dependencymanager标签中的依赖 子级项目想要继承的话,需要额外声明, 但是可以不写版本号
<!-和ependencyManagement一样-->
<pluginManagement>
<plugins>
</plugins>
</pluginManagement>
另外插件的继承类似
**
第三 引用其他同级项目的类或方法
**
新建module 项目名为app,步骤与新建manager类似
父级pom.xml
<!--任何子级项目集成了该父级项目 需要在父级项目的pom.xml文件中做配置-->
<modules>
<module>manager</module>
<module>app</module>
</modules>
app的pom.xml
<?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>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baigu</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>Demo project for Spring Boot</description>
</project>
manager的pom.xml
<?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>
<dependencies>
<!--把app项目当作依赖引入当前项目-->
<dependency>
<groupId>com.baigu</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--EasyPoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
</dependency>
</dependencies>
<!--继承自己的父级项目-->
<parent>
<groupId>com.baigu</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baigu</groupId>
<artifactId>manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>manager</name>
<description>Demo project for Spring Boot</description>
</project>
**
在app项目中的实体类 manager项目中可以进行访问
**
manager的启动类
package com.baigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@SpringBootApplication
@Component("com.baigu")
public class ManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerApplication.class, args);
}
}
@Component(“com.baigu”)意思表示同级项目调用时,如果包名不一致,加上该注解,并且取前缀一致的包名可以相互调用 所以最好还是取一致的包名