使用背景:
springboot+maven构建的多模块项目,没有使用springcloud,根项目下有多个module,一个module有springboot启动类,其他module没有启动类,作为module被有springboot启动类的module所引用。
1.根目录下项目maven packaging要声明为pom
<packaging>pom</packaging>
2.多个模块管理:
<modules>
<module>test-common</module>
<module>test-system</module>
<module>test-core</module>
</modules>
3.build构建声明:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
4.有启动类那个module的项目maven packaging要声明为jar
<artifactId>test-core</artifactId>
<packaging>jar</packaging>
5.有启动类那个module的pom.xml引入其他子模块
<dependencies>
<dependency>
<groupId>com.daniels</groupId>
<artifactId>test-system</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.daniels</groupId>
<artifactId>test-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
6.有启动类那个module的pom.xml增加build构建插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
其他没有启动类的module的pom.xml只用引入需要的jar即可,示例:
<?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>com.daniels</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>test-system</artifactId>
<description>
系统模块
</description>
<dependencies>
<dependency>
<groupId>com.daniels</groupId>
<artifactId>test-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
如此便可将有启动类的module打包成Jar,同时将其他引用到子module打包进该jar中。