1. 项目与项目之间的依赖
maven_project_dependent
依赖maven_java_helloworld_01
的项目依赖包
方式1: 将所要依赖的项目的<groupId><artifactId><version>
粘贴至maven_project_dependent
的pom.xml文件
maven_java_helloworld_01
项目的<groupId><artifactId><version>
:
方式2: 在maven_project_dependent
的pom.xml文件中搜索并插入所要依赖的项目
maven_project_dependent
的pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <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.emp</groupId> <artifactId>maven_project_dependent</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project_dependent</name>
<dependencies> <dependency> <groupId>com.emp</groupId> <artifactId>maven_java_helloworld_01</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
|
maven_project_dependent
的测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.test;
import org.springframework.core.io.ClassPathResource;
import com.bean.User;
public class Test { public static void main(String[] args) { ClassPathResource classPathResource = new ClassPathResource(""); System.out.println(classPathResource.exists()); User user = new User(); user.setId(1); user.setName("Tim"); user.setAge(10); System.out.println(user.toString()); }
}
|
output:
1 2
| true User [id=1, name=Tim, age=10]
|
2. 项目的继承
2.1 项目的继承方式1: 继承父Maven项目的所有的依赖包
(1) 创建父Maven项目
打包方式必须为pom,<packaging>pom</packaging>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <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.emp</groupId> <artifactId>maven_parent_01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>parent_01</name>
<properties> <spring.version>5.2.12.RELEASE</spring.version> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> </dependencies> </project>
|
(2) 创建子项目
打包方式可以是jar, pom, war
选择Parent项目:
(3) 总结
父项目的打包方式必须为pom
子项目会继承父项目所有的依赖
2.2 项目的继承方式2: 使用<dependencyManagement>
管理子项目可以继承的依赖包
(1) parent maven project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <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.emp</groupId> <artifactId>maven_parent_01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>parent_01</name>
<properties> <spring.version>5.2.12.RELEASE</spring.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> </dependencies> </dependencyManagement> </project>
|
(2) child maven project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <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.emp</groupId> <artifactId>maven_parent_01</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<groupId>com.emp</groupId> <artifactId>maven_child_01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>child_01</name>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency>
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency>
</dependencies> </project>
|
(3) 总结
如果父项目加入了依赖管理器<dependencyManagement>
,那么子项必须显式去声明需要的依赖。
如果父项目有该依赖包,子项目在声明时可以不指定版本号
子项目中可以添加没有父项目没有的依赖包,子项目新添加的依赖包必须有<groupId><artifactId><version>
3. 项目的聚合
maven module
1 2 3 4 5 6
| common, maven project, jar parent, maven project, pom, 依赖common |--vo, maven module, jar |--dao, maven module, jar, 依赖vo |--service, maven module, jar, 依赖dao |--controller, maven module, war, 依赖service
|
(1) 创建common,项目类型maven project,打包方式jar
1 2 3 4 5 6 7 8 9
| <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.emp</groupId> <artifactId>mall_common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven_common</name> </project>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.util;
import java.util.UUID;
public class RandomUtil {
public static String getRandomStr() { String randomStr = UUID.randomUUID().toString() .replace("-", "").toUpperCase(); return randomStr; } }
|
(2) 创建parent,项目类型maven project,打包方式pom,依赖common
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <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.emp</groupId> <artifactId>mall_parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>maven_parent</name>
<dependencies> <dependency> <groupId>com.emp</groupId> <artifactId>mall_common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <modules> <module>mall_vo</module> <module>mall_dao</module> <module>mall_service</module> <module>mall_controller</module> </modules> </project>
|
(3) 创建vo,项目类型maven module,打包方式jar
1 2 3 4 5 6 7 8 9 10 11 12
| <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.emp</groupId> <artifactId>mall_parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>mall_vo</artifactId> <name>maven_vo</name> </project>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.vo;
public class User { private Integer id; private String name; private Integer age; public User() { super(); }
public User(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; }
}
|
(4) 创建dao,项目类型maven module,打包方式 jar,依赖vo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <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.emp</groupId> <artifactId>mall_parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>mall_dao</artifactId> <name>maven_dao</name>
<dependencies> <dependency> <groupId>com.emp</groupId> <artifactId>mall_vo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
</project>
|
1 2 3 4 5 6 7 8
| package com.dao;
import com.vo.User;
public interface UserDao {
public User getUserById(Integer id); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.dao.impl;
import com.dao.UserDao; import com.vo.User;
public class UserDaoImpl implements UserDao {
public User getUserById(Integer id) { User user = new User(); switch (id) { case 1: user.setId(1); user.setName("user1"); user.setAge(10); break; case 2: user.setId(2); user.setName("user2"); user.setAge(15); break; default: break; } return user; }
}
|
(5) 创建service,项目类型maven module,打包方式jar,依赖dao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <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.emp</groupId> <artifactId>mall_parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>mall_service</artifactId> <name>maven_service</name>
<dependencies> <dependency> <groupId>com.emp</groupId> <artifactId>mall_dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
|
1 2 3 4 5 6 7 8
| package com.service;
import com.vo.User;
public interface UserService {
public User getUserById(Integer id); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.service.impl;
import com.dao.UserDao; import com.dao.impl.UserDaoImpl; import com.service.UserService; import com.vo.User;
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl(); public User getUserById(Integer id) { return userDao.getUserById(id); }
}
|
(6) 创建controller,项目类型maven module,打包方式war,依赖service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <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.emp</groupId> <artifactId>mall_parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>mall_controller</artifactId> <packaging>war</packaging> <name>maven_controller</name>
<dependencies> <dependency> <groupId>com.emp</groupId> <artifactId>mall_service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.controller;
import com.service.UserService; import com.service.impl.UserServiceImpl; import com.vo.User;
public class UserController {
private UserService userService = new UserServiceImpl(); public User getUserById(Integer id) { return userService.getUserById(id); } }
|
测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.test;
import com.controller.UserController; import com.util.RandomUtil; import com.vo.User;
public class Test {
public static void main(String[] args) { UserController userController = new UserController(); User user = userController.getUserById(1); System.out.println(user); String randomStr = RandomUtil.getRandomStr(); System.out.println(randomStr); } }
|
output:
1 2
| User [id=1, name=user1, age=10] 303E41526F2A47739BE1D673A3949235
|