Maven 打包出现问题解决方案

我执行 mvn install 报如下错误

可是我在 web 模块中能正确引用到 common 的类,于是我把 web 引用到的 common 中的类先移动到 web 模块中,然后把 common 模块的类都删掉,然后再次执行 mvn install,结果报错如下:

bash 复制代码
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.1.12:repackage (repackage) on project common: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:3.1.12:repackage failed: Unable to find main class -> [Help 1]
[ERROR]

这个问题的原因:

Spring Boot 的 spring-boot-maven-plugin 插件的 repackage 目标需要在构建时指定一个主类(main class),用于创建可执行的 JAR 或 WAR 文件。如果你的 common 模块不是一个 Web 或 Spring Boot 应用,它应该不需要这个插件的 repackage 目标,因为它并没有主类(main class)可供启动。

我的项目结构

父模块 - tylerpro

tylerpro 的 pom.xml 内容

bash 复制代码
<?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>3.1.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.tylerpro</groupId>
    <artifactId>tylerpro</artifactId>
    <version>0.0.1</version>
    <name>tylerpro</name>
    <description>tylerpro</description>

    <packaging>pom</packaging>

    <modules>
        <module>web</module>
        <module>common</module>
    </modules>

    <properties>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

子模块 - common

bash 复制代码
<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.tylerpro</groupId>
        <artifactId>tylerpro</artifactId>
        <version>0.0.1</version>
    </parent>

    <groupId>com.tylerpro</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

子模块 - web

bash 复制代码
<?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.tylerpro</groupId>
        <artifactId>tylerpro</artifactId>
        <version>0.0.1</version>
    </parent>

    <groupId>com.tylerpro</groupId>
    <artifactId>web</artifactId>
    <name>web</name>
    <description>web</description>

    <properties>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.yxai</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

原因是 因为 common 模块中继承了父模块的打包插件,而 common 模块只是作为一个普通的库使用,它不是一个 springboot 项目。在父模块中使用了 spring-boot-maven-plugin 打包,所以 common 打包出错了。

解决方案

因为 web 中已经使用了 spring-boot-maven-plugin 打包插件,所以直接删除父模块中的打包插件就行。

bash 复制代码
<?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>3.1.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.tylerpro</groupId>
    <artifactId>tylerpro</artifactId>
    <version>0.0.1</version>
    <name>tylerpro</name>
    <description>tylerpro</description>

    <packaging>pom</packaging>

    <modules>
        <module>web</module>
        <module>common</module>
    </modules>

    <properties>
    </properties>

扩展:

maven 打包的几种方式:

SpringBoot 使用 maven 创建一个可执行的 jar 包 - SpringBoot官方文档-Creating an Executable Jar
apache maven plugin 打包插件
apache maven plugin 打包插件的属性配置
maven 高级视频教程

相关推荐
用户84913717547161 小时前
JDK 17 实战系列(第4期):安全性与稳定性增强详解
java·后端·性能优化
自由的疯1 小时前
java程序员怎么从Python小白变成Python大拿?(三)
java·后端·trae
用户84913717547161 小时前
JustAuth实战系列(第4期):模板方法模式实战 - AuthDefaultRequest源码剖析
java·后端·架构
weixin_411191841 小时前
安卓Handler和Looper的学习记录
android·java
创创ccccc2 小时前
十三、抽象队列同步器AQS
java·并发编程·juc·aqs
鼠鼠我捏,要死了捏2 小时前
生产环境中JVM内存泄漏定位与解决实践
java·jvm·内存泄漏
小宋10212 小时前
pg序列实现字段的自增
java·数据库
悦人楼3 小时前
深入理解Java集合框架:核心接口、实现类与实战选择
java·windows·python
花开富贵ii3 小时前
代码随想录算法训练营第三十八天、三十九天|动态规划part11、12
java·数据结构·算法·leetcode·动态规划
##学无止境##4 小时前
深入剖析Java线程:从基础到实战(上)
java·开发语言·redis