gpt4 book ai didi

spring boot 多模块包

转载 作者:行者123 更新时间:2023-12-04 18:28:44 24 4
gpt4 key购买 nike

我正在尝试使用 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>

我的公共(public) 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>

和我的数据服务 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

异常抛出

无法执行目标 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 不存在;

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

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

最佳答案

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

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

  • 我的父模块
    • 我的服务模块
    • 我的公共(public)模块
    • my-web-module(或非网络应用程序中的 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”运行该应用程序。

关于spring boot 多模块包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41445793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com