首页 文章

spring boot多模块包

提问于
浏览
2

我正在尝试使用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,它就像一个runnable 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

1 回答

  • 5

    发生这种情况是因为你的模块会添加'spring-boot-maven-plugin',因为它是在父模块中定义的 .

    您需要做的是将所有内容移动到子模块,甚至是应用程序入门类 . 我通常如何做到这一点:

    • my-parent-module

    • my-service-module

    • my-common-module

    • my-web-module(或非web应用程序中的my-runtime-module)

    my-parent-module将具有'spring-boot-starter-parent'作为其父级,但它不会有src文件夹,因为所有内容都被移动到子模块中 .

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

相关问题