springboot多模块打包以及运行测试
- springboot多模块打包以及运行测试
- 项目目录结构
- weisite父项目下的pom.xml
- common项目下的pom.xml
- mbg项目下的pom.xml
- official-website项目下的pom.xml
- 多模块下项目下的打包操作
springboot多模块打包以及运行测试
这几天写完了一个多模块的springboot项目,没想到在打包的时候踩了一点坑,这里记录下自己打包的过程。
项目目录结构
其中common文件夹和mbg都是公共类,里面没有启动类。
只有official-website里面有启动类
weisite父项目下的pom.xml
在modules里面我只引入了公共模块common和mbg,所以在父项目打包的时候,只会打包common和mbg,official-website需要额外打包(后边内容会说到),如果你想通过父项目将这三个一次性打包,只需要在modules里面引入official-website模块即可。
<?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>
<packaging>pom</packaging>
<!--引入子模块公共模块-->
<modules>
<module>common</module>
<module>mbg</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bowei</groupId>
<artifactId>website</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>website</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--MyBatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!--集成druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--这里引入了spring-boot-maven-plugin,打包时会去扫描项目main方法入口,也就是说引入该配置,你就必须在项目src/main/java/下创建一个spring-boot启动类:-->
<!--因为我这里是多模块,所以我把父类下面的src文件给删除了,所以找不到父类的启动类,所以这里要注释掉,不然打包时就会报错-->
<!--<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>-->
<!--添加配置跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!--默认关掉单元测试-->
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--添加配置跳过测试-->
</plugins>
</build>
</project>
这是父模块下的pom.xml,对于没有启动类的模块以及子模块,不要加下面那个插件(因为父项目没有启动类,所以我就注释掉了这一个)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
至于为什么我要在父模块下加这个,是因为不加这个插件,在我package打包的时候会报如下错:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project common: There are test failures.
Please refer to D:\IdeaProjects\website\common\target\surefire-reports for the individual test results.
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
在父模块下加了下列插件才解决问题!
<!--添加配置跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!--默认关掉单元测试-->
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--添加配置跳过测试-->
common项目下的pom.xml
没有启动类的子模块不加build
没有启动类的子模块不加build
没有启动类的子模块不加build
<?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>
<groupId>com.bowei</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>common</name>
<description>Demo project for Spring Boot</description>
<!--继承本项目的父工程-->
<parent>
<artifactId>website</artifactId>
<groupId>com.bowei</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
<!--Swagger-UI API文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<!--Hutool Java工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.7</version>
</dependency>
</dependencies>
<!--<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>-->
</project>
mbg项目下的pom.xml
没有启动类的子模块不加build
没有启动类的子模块不加build
没有启动类的子模块不加build
<?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>
<groupId>com.bowei</groupId>
<artifactId>mbg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mbg</name>
<description>Demo project for Spring Boot</description>
<!--继承本项目的父工程-->
<parent>
<artifactId>website</artifactId>
<groupId>com.bowei</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bowei</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- MyBatis 生成器 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--Mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<!--<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>-->
</project>
official-website项目下的pom.xml
因为这是我们需要的主要子项目,是要启动服务去访问的,所以要写build
<?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>
<groupId>com.bowei</groupId>
<artifactId>official-website</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>official-website</name>
<description>Demo project for Spring Boot</description>
<!--继承本项目的父工程-->
<parent>
<artifactId>website</artifactId>
<groupId>com.bowei</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--加上這個就不會报数据库找不到驱动错误-->
<dependency>
<groupId>com.bowei</groupId>
<artifactId>mbg</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--访问静态资源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
多模块下项目下的打包操作
首先我们现在父项目下clean,清楚项目里面的target文件夹
父项目install下完成如下
然后再去打包你要启动的那个子项目
子项目package完成如下:
打包完会发现出现三个target包
然后我们去idea项目文件下将这个拷贝出来放到d盘下,不拷贝也行
如下:
我们可以看到jar包里面是包含了common和mbg子模块的
然后我们通过cmd启动项目
最后你在谷歌浏览器上http://localhost:8080/去访问你自己的接口即可。