我正在尝试使用Maven将Spring Boot与多个模块打包,这是我的主要模块pom.xml:

<modules>
    <module>my-data-services</module>
    <module>my-message-services</module>
    <module>my-common</module>
</modules>
<groupId>com.my</groupId>
<artifactId>my-multi-services</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencyManagement>....</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


我常见的pom.xml:

<parent>
    <artifactId>my-multi-services</artifactId>
    <groupId>com.my</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-common</artifactId>
<packaging>jar</packaging>
<dependencies>....</dependencies>


和my-data-services pom.xml:

<parent>
    <artifactId>my-multi-services</artifactId>
    <groupId>com.my</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-data-services</artifactId>

<dependencies>

    <dependency>
        <groupId>com.my</groupId>
        <artifactId>my-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

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


my-common模块只是一个通用的utils lib而不是一个可运行的模块,但是当我尝试mvn clean package时,异常抛出如下:

Execution default of goal org.springframework.boot:spring-boot-maven-p
lugin:1.4.2.RELEASE:repackage failed: Unable to find main class


然后我添加一个主类,这个模块可以打包,但不是lib jar,它像可运行的spring boot jar

 -BOOT-INF
 |
 -META-INF
 |
 -org


和异常抛出

Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:3.1:compile (default-compile) on project my-data-services: Compilation failure: Compilation failure: package com.my.common.utils does not exist;

com.my.common.utils在模块my-common

我如何解决此问题,以及在Spring Boot多模块项目中,如何在没有BOOT-INF的情况下打包通用utils lib

最佳答案

发生这种情况是因为您的模块将在其父模块中定义,因此将添加“ spring-boot-maven-plugin”。

您需要做的就是将所有内容移动到子模块中,甚至包括应用程序启动程序类。我通常如何做:


我的父母模块


我的服务模块
我的通用模块
我的网络模块(或非网络应用中的我的运行时模块)



my-parent-module的父级将具有“ spring-boot-starter-parent”,但由于所有内容都移入了子模块,因此它将没有src文件夹。

my-web-module将依赖于其他模块,并将具有“ spring-boot-maven-plugin”。您将可以在my-web-module文件夹中使用“ mvn spring-boot:run”运行该应用程序。

10-02 00:09
查看更多