SpringBoot 多模块项目打包编译

2022-11-02 15:26:14 浏览数 (1)

1.先使用Idea创建一个父工程

2.创建子工程

3.配置父子工程的pom文件进行关联

父工程pom

代码语言:javascript复制
<?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">
    <!-- 基本信息 -->
    <description>SpringBoot 多模块构建示例</description>
    <modelVersion>4.0.0</modelVersion>
    <name>springboot-integration</name>
    <packaging>pom</packaging>
    <!-- 项目说明:这里作为聚合工程的父工程 -->
    <groupId>com.jingan</groupId>
    <artifactId>jinganplatform</artifactId>
    <version>1.0.0.RELEASE</version>
    <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>
    <!-- 模块说明:这里声明多个子模块 -->
    <modules>
        <module>jingan-public</module>
        <module>jingan-service</module>
    </modules>
    <!-- 版本说明:这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.jingan</groupId>
                <artifactId>jingan-public</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.jingan</groupId>
                <artifactId>jingan-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

公共类子工程

代码语言:javascript复制
<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jingan</groupId>
    <artifactId>jingan-public</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jingan-public</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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

业务类子工程

代码语言:javascript复制
<?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>com.jingan</groupId>
        <artifactId>jinganplatform</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>
    <groupId>com.jingan</groupId>
    <artifactId>jingan-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jingan-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.jingan</groupId>
            <artifactId>jingan-public</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.错误

非编译类不要加入以下代码,service引入public模块的代码,public的pom文件就不要写以下的配置,否则会报错

代码语言:javascript复制
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

报错信息

初步判断是引入另一个jar包时的指定打包插件重复引入内容被重置

5.打包

配置没问题以后再父工程先进行clean

再进行package

显示打包成功,去模块目录下有个target文件夹获取代码包

这样service就连带引入的public内容都打包了,service的包就可以直接执行使用了

0 人点赞