解决springboot maven多模块项目打包的时候某个被依赖的模块报错找不到main class

本文介绍了解决Spring Boot多模块项目中特定模块无需Main-Class属性导致打包失败的问题。通过调整父POM文件中的配置,避免了spring-boot-maven-plugin对不需要Main-Class的模块进行错误的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot maven 多模块项目打包的时候某个被依赖的模块报错

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:
repackage (repackage) on project **-client: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:repackage failed:
Unable to find main class -> [Help 1]

这个被依赖的**-client模块并不需要有main类,解决方法是修改父pom.xml

原来的父pom.xml中的标签内容为

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

将父pom.xml中的标签内容改为

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <fork>true</fork>
                    <verbose>true</verbose>
                    <encoding>UTF-8</encoding>
                    <meminitial>256m</meminitial>
                    <maxmem>1024m</maxmem>
                </configuration>
            </plugin>
        </plugins>
    </build>

原因可以参考springboot官网
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-maven-packaging

Once spring-boot-maven-plugin has been included in your pom.xml,
it automatically tries to rewrite archives to make them executable by using the spring-boot:repackage goal.

### 问题分析与解决方案 在使用 Maven 打包 Spring Boot 项目时,常见的错误包括插件版本不兼容、不到主类(main class)、以及打包后的 JAR 包结构异常(如包含 BOOT-INF 文件夹)等问题。以下是具体的解决方法: #### 1. 插件版本不兼容问题 如果在打包过程中遇到类似于 `spring-boot-maven-plugin:3.2.0:resources` 的错误,这通常与插件版本有关。Spring Boot 的某些版本与特定的 Maven 插件版本可能存在兼容性问题。可以通过以下方式解决: - **降低 Maven Resources 插件版本**:尝试使用更稳定的版本,如 `3.1.0`,以避免新版本中可能存在的 bug。 - **降低 Spring Boot 版本**:如果当前使用的 Spring Boot 版本较新,可以考虑降级到一个更稳定的版本,以确保与现有插件兼容[^1]。 #### 2. 不到主类(Main Class) 当项目中使用了 `spring-boot-maven-plugin` 插件但打包时提示 `Unable to find main class`,这通常是因为插件无法到主类。解决方法包括: - **确保主类存在并正确配置**:检查项目的主类是否包含 `public static void main(String[] args)` 方法,并且该类被正确标注为 Spring Boot 应用程序的入口点。 - **在插件配置中明确指定主类**:在 `pom.xml` 文件中,为 `spring-boot-maven-plugin` 插件添加 `<mainClass>` 配置项,指定主类的全限定名。 ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.MainApplication</mainClass> </configuration> </plugin> ``` - **移除不必要的插件**:如果项目不是 Spring Boot 项目,则不需要 `spring-boot-maven-plugin` 插件。如果项目是一个多模块项目,确保只有需要的模块包含该插件,避免父模块中错误地引入插件导致子模块打包失败[^2]。 #### 3. 打包后的 JAR 包包含 BOOT-INF 文件夹 当打包的 JAR 包中出现 `BOOT-INF` 文件夹时,这通常是由于 `spring-boot-maven-plugin` 插件的作用。此插件会将依赖项打包到 `BOOT-INF/lib` 目录下,并将应用程序代码打包到 `BOOT-INF/classes` 目录下。如果你希望生成一个普通的 JAR 包而不包含 `BOOT-INF` 文件夹,可以考虑以下方法: - **使用 `maven-jar-plugin` 替代 `spring-boot-maven-plugin`**:如果你不需要 Spring Boot 的可执行 JAR 包特性,可以在 `pom.xml` 文件中使用 `maven-jar-plugin` 插件来生成标准的 JAR 包。 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainApplication</mainClass> </manifest> </archive> </configuration> </plugin> ``` - **自定义 `spring-boot-maven-plugin` 配置**:如果你仍然希望使用 `spring-boot-maven-plugin` 插件,但不想生成 `BOOT-INF` 文件夹,可以通过自定义插件配置来调整打包行为[^3]。 #### 4. 忽略测试类执行 在打包过程中,Maven 默认会执行所有测试类。如果你希望跳过测试类的执行,可以在打包命令中添加 `-Dmaven.test.skip=true` 参数: ```bash mvn clean package -Dmaven.test.skip=true ``` 此命令将跳过测试类的编译和执行,从而加快打包过程。 ###
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值