当前位置: 首页 > news >正文

Maven 打包出现问题解决方案

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

[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 内容

<?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

<?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

<?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 打包插件,所以直接删除父模块中的打包插件就行。

<?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 高级视频教程

http://www.lryc.cn/news/496440.html

相关文章:

  • 第四话:JS中的eval函数
  • 歇一歇,写写段子
  • TypeScript (一)运行环境配置,数据类型,可选类型,联合类型,type与interface,交叉类型,断言as,字面量类型,类型缩小
  • Linux Find 命令详情解释
  • 2024年认证杯SPSSPRO杯数学建模A题(第一阶段)保暖纤维的保暖能力全过程文档及程序
  • Milvus python库 pymilvus 常用操作详解之Collection(下)
  • 李飞飞:Agent AI 多模态交互的前沿探索
  • [October 2019]Twice SQL Injection
  • Python爬虫——城市数据分析与市场潜能计算(Pandas库)
  • 如何搭建JMeter分布式集群环境来进行性能测试
  • 【Halcon】 derivate_gauss
  • stm32中systick时钟pinlv和系统节拍频率有什么区别,二者有无影响?
  • 柔性数组详解+代码展示
  • 前端入门指南:Webpack插件机制详解及应用实例
  • C++备忘录模式
  • 【Electron学习笔记(四)】进程通信(IPC)
  • Java 中的 remove 方法深度解析
  • 企业品牌曝光的新策略:短视频矩阵系统
  • 【初阶数据结构与算法】二叉树顺序结构---堆的应用之堆排、Top-K问题
  • vue3 + ts 使用 el-tree
  • Create Stunning Word Clouds with Ease!
  • html+css网页设计 旅游 马林旅行社5个页面
  • python selenium(4+)+chromedriver最新版 定位爬取嵌套shadow-root(open)中内容
  • React基础教程(11):useCallback记忆函数的使用
  • arp-scan 移植到嵌入式 Linux 系统是一个涉及多个步骤的过程
  • 【Linux】常用命令一
  • 在鲲鹏麒麟服务器上部署MySQL主从集群
  • Siknhorn算法介绍
  • 群控系统服务端开发模式-应用开发-邮箱短信通道功能开发
  • [docker中首次配置git环境]