Maven聚合工程:就是可以在一个Maven父工程中创建多个组件(项目),这个多个组件之间可以相互依赖,实现组件的复用
Maven聚合工程的父工程 packing必须为 pom
<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.0modelVersion>
<groupId>com.blbgroupId>
<artifactId>fmmallartifactId>
<version>1.0.0version>
<packaging>pompackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
project>
父工程用于管理子工程,不进行业务实现,因此src目录可以选择性删除
选择父工程—右键—New—Module
输入子工程名称(g和v都从父工程继承)
并将打包方式设置为jar
<packaging>jarpackaging>
<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>fmmallartifactId>
<groupId>com.blbgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>commonartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
project>
<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.0modelVersion>
<groupId>com.blbgroupId>
<artifactId>fmmallartifactId>
<version>1.0.0version>
<modules>
<module>commonmodule>
modules>
<packaging>pompackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
project>
同理可以建其他的模块
如果想有springboot启动类,可以不创建普通的maven项目,可以用springboot的初始化
修改api的pom.xml,继承fmmall,删除自己的groupId 和 version
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.3version>
<relativePath/>
parent>
<module>apimodule>
<parent>
<artifactId>fmmallartifactId>
<groupId>com.blbgroupId>
<version>1.0.0version>
parent>
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.3version>
<relativePath/>
parent>
<groupId>com.blbgroupId>
<artifactId>fmmallartifactId>
<version>1.0.0version>
<modules>
<module>commonmodule>
<module>beansmodule>
<module>mappermodule>
<module>servicemodule>
<module>apimodule>
modules>
<packaging>pompackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
<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.0modelVersion>
<parent>
<artifactId>fmmallartifactId>
<groupId>com.blbgroupId>
<version>1.0.0version>
parent>
<groupId>com.blbgroupId>
<artifactId>apiartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>apiname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
project>
<dependency>
<groupId>com.blbgroupId>
<artifactId>beansartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>com.blbgroupId>
<artifactId>commonartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>com.blbgroupId>
<artifactId>mapperartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>com.blbgroupId>
<artifactId>serviceartifactId>
<version>1.0.0version>
dependency>
如果将依赖添加到父工程的pom中,根据依赖的继承关系,所有的子工程中都会继承父工程的依赖:
好处:当有多个子工程都需要相同的依赖时,无需在子工程中重复添加依赖
缺点:如果某些子工程不需要这个依赖,还是会被强行继承
如果在父工程中没有添加统一依赖,则每个子工程所需的依赖需要在子工程的pom中自行添加
如
果存在多个子工程需要添加相同的依赖,则需在父工程pom进行依赖版本的管理
例如l ombokcommon和beans两边都要使用并且版本还要一致,因此可以
在父工程的pom.xml的dependencyManagement中添加依赖,表示定义子工程中此依赖的默认版本(此定义并不会让子工程中添加当前依赖)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.20version>
<scope>providedscope>
dependency>
dependencies>
dependencyManagement>
添加lombok依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
添加lombok依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependencies>
<dependency>
<groupId>com.blbgroupId>
<artifactId>beansartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.20version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
resources
目录创建application.yml
#连接数据库的信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db3?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
# 扫描映射文件
mapper-locations: classpath:mapper/*.xml
# 配置别名扫描的包
type-aliases-package: com.blb.entity
configuration:
# 开启驼峰映射配置
map-underscore-to-camel-case: true
com.blb.entity
@MpperScan
声明mapper包的路径
@SpringBootApplication
@MapperScan("com.blb.mapper")
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
本来是给mapper添加的,但是由于找不到启动类而报错
所以可以给api模块添加响应的测试
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
@SpringBootTest
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
void queryUserByName() {
User user = userMapper.queryUserByName("dyk123");
System.out.println(user);
}
}
后面也可以接着写service,controller测试,跑完整项目时最好在父项目那install一下