背景
由于springboot的便捷,目前基本上都是使用springboot进行开发,那遇到比较大型的项目又会需要使用maven的多模块功能,今天就来讲下这两者怎么结合使用
1. 项目结构(源码我会放在github上,后面会给出地址)
2.搭建父工程
点击idea上的file->new->project ,然后选择maven项目,除了选下jdk是否正确,其他都不用选,直接next,完成项目创建。创建完成之后,由于该项目是作为父工程,所以是不要src文件夹的,直接删除。然后打开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>
<!-- 将springboot作为父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>test</name>
<!--父工程的packaging方式必须是pom-->
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>model</module>
<module>dao</module>
<module>service</module>
<module>api</module>
</modules>
<!--自定义环境变量-->
<properties>
<main-version>1.0.0</main-version>
<java.version>1.8</java.version>
<!-- 设置maven版本,否则pom文件报错,原因就是maven版本和springboot版本不一致 -->
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 打包时跳过test测试 -->
<skipTests>true</skipTests>
<mybatis-plus-version>3.1.0</mybatis-plus-version>
<druid-version>1.1.16</druid-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!--dependencyManagement这里是作版本控制,相当于只是对依赖的定义,并不是真的依赖这些模块,而是在子模块依赖相对应的依赖时,能够不用写版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>api</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>service</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>model</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>dao</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--依赖,子模块会继承这些依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- 引入log4j2依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
注意点:
1.父工程的packaging必须是pom
2.父工程中不用写build标签
3.为了方便,我直接在父工程中引入了spring-boot-starter-web
4.可以使用Spring IO Platform进行依赖版本的管理(最好使用这个)
2.搭建子工程
2.1 创建模块
以common模块为例:
右击项目new 一个module,然后选择springboot的快捷创建方式。
2.2 删除无用文件及配置
2.2.1 删除每个子模块中没用的文件
.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml
2.2.2 删除配置
删除除了web模块以外其它模块中的Applicatin启动项,和resources目录下的application.properties配置文件
删除后大致是这样的:
2.2.3 修改子模块的pom文件
以common模块为例:
1.修改parent标签,使他指向父工程:
<parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.修改自身的坐标version,main-version在父工程中定义了,所以可以直接使用,所有的子模块都是用main-version,以便版本统一:
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
3.删除properties标签(会继承父工程的,所以这里不需要写了)
4.添加packaging标签,定义打包方式为jar,除了api这个web模块为war,其他均是jar包:
<packaging>jar</packaging>
5.删除build标签,除了api模块,其他均不需要该标签打包(特殊情况除外,后面会说到)
2.2.1 源码
common的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
<name>common</name>
<packaging>jar</packaging>
<dependencies>
</dependencies>
</project>
model:
<?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>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>model</artifactId>
<version>${main-version}</version>
<name>model</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
</project>
dao:
<?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>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>dao</artifactId>
<version>${main-version}</version>
<name>dao</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>model</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
service:
<?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>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>service</artifactId>
<version>${main-version}</version>
<name>service</name>
<dependencies>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>dao</artifactId>
</dependency>
</dependencies>
</project>
api:
<?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>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>api</artifactId>
<version>${main-version}</version>
<name>api</name>
<packaging>war</packaging>
<dependencies>
<!--指定使用外置的toomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>service</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--要加这个,否则报错java.sql.SQLException: com.mysql.cj.jdbc.Driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
依赖关系: common -> model -> dao -> service -> api
3. 创建controller,service,dao来做一个请求测试
源码可以去我的github上自行下载,这里就不做展示了,就很简单的一个请求流程
注意点:由于你将除了api的其他子模块的启动类删除了,那么spring就不能自动扫描对应模块下的包了,所以必须要在api的启动类上加上@ComponentScan("cn.tfp")注解来扫描整个项目的包
3.1 dao层的sql语句xml文件的放置位置
有两种一种是放在resources,一种是放在src/main/java的包下
第一种很简单,假如我放在了resources->mapper文件夹下,只需要在application.yml中如下配置即可
mybatis-plus:
mapper-locations:
- classpath:mapper/*.xml
第二种稍微有些复杂,我这个项目就是使用的第二种,我的xml文件都是放在cn.tfp.dao.sql包下的,那么首先需要在dao模块的pom文件中加一段配置:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
接着在application.yml中配置:
mybatis-plus:
mapper-locations:
- cn/tfp/dao/sql/*.xml
至此可以启动api的启动类,发送请求,进行测试了。
4. 打包war包
4.1 修改api启动类,使其继承SpringBootServletInitializer类,并重写configure方法
@SpringBootApplication
@ComponentScan("cn.tfp")
@MapperScan("cn.tfp.dao.mapper")
public class ApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ApiApplication.class);
}
}
4.2 向api模块的pom文件添加依赖,意思就是使用外部的tomcat,不使用springboot自带的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
4.3 install父pom文件进行打包
4.3.1 进入父pom文件,将modules标签全部注释,然后install父pom文件
4.3.2 将modules标签的注释解开,再install一下
这两步是必须的,如果不这么做,你直接install父pom,会直接报错。
4.4 获取war包,并发布
4.5 问题
打war包后,log4j2的自定义配置文件失效。例如:假设我的log4j2的配置文件是log4j2-sit.xml,并且在application.yml中配置
logging:
config: classpath:log4j2-sit.xml
只要你打了war包,运行,就会发现,这个xml文件根本没有起作用,但是如果你的命名是log4j2.xml,那么就不会有问题。这个问题依然没有解决,有办法的可以私聊下。虽然知道springboot会自动找log4j2.xml文件,但是我已经在配置文件中指定了log4j2-sit.xml,为什么不生效?还有如果不打war包,你直接启动api模块的启动类,进行请求等,使用log4j2-sit.xml配置文件,日志却又是ok的,没有问题。
5.总结
1.项目可以看成共享的,会合并起来,所以每个子项目的包名要有所区别,否则有可能重复
2.既然会合并起来,那么这么多模块也只能有一个application.yml,为了防止出错,将其他子模块的启动类和application.yml全部删除,只留api模块的
6. 项目地址:https://github.com/tanfangping/multi-maven-springboot/tree/master
7.开发时使用idea自带的tomcat模块启动项目
由于springboot自带了tomcat,所以很少用idea的tomcat来启动项目,但是我在摸索过程当中还是使用了这种方式,其中有些坑点说明下:
1.启动后tomcat出现乱码:参考https://www.cnblogs.com/shej123/p/10312806.html
2.使用这种方式的时候,启动类要集成 extends SpringBootServletInitializer类,并重写configure方法
3.开发过程中不要install项目,否则idea会先读取你本地install的项目,这样你就会发现,你就算修改了代码,却一直不起作用,tomcat两种模式,第一种web.war(会直接install项目,并把war包放到tomcat上),第二种web war.exploded(算是模拟,并不会真的将war包放到tomcat上),所以第二种能够热加载,如果使用第二种,就不要去install项目了,最后要发布项目时再insall