【Maven_bugs】The project main artifact does not exist

背景:我想使用 maven-shade-plugin 打一个 fat jar 时报了标题中的错误,使用的命令是:org.apache.maven.plugins:maven-shade-plugin:shade -pl :shade-project。项目结构如下图,我想把子模块 shade-project 打成一个 fat jar:

执行命令后控制台打印如下:

Bash 复制代码
xxx@xxxdeMacBook-Air maven-package-demo % mvn org.apache.maven.plugins:maven-shade-plugin:shade -pl :shade-project
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.myphoenix:shade-project >---------------------
[INFO] Building shade-project 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mysql:mysql-connector-j:jar:8.0.33: MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates.
[INFO] 
[INFO] --- shade:3.6.0:shade (default-cli) @ shade-project ---
[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR]   supported. Please add the goal to the default lifecycle via an
[ERROR]   <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR]   remove this binding from your POM such that the goal will be run in
[ERROR]   the proper phase.
[ERROR] - You removed the configuration of the maven-jar-plugin that produces the main artifact.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.229 s
[INFO] Finished at: 2024-12-24T21:38:23+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.6.0:shade (default-cli) on project shade-project: Failed to create shaded artifact, project main artifact does not exist. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

可以看到控制台也打印了 3 个原因,如下所示:

  • You have invoked the goal directly from the command line. This is not supported. Please add the goal to the default lifecycle via an element in your POM and use "mvn package" to have it run.
  • You have bound the goal to a lifecycle phase before "package". Please remove this binding from your POM such that the goal will be run in the proper phase.
  • You removed the configuration of the maven-jar-plugin that produces the main artifact.

总结来说,maven-shade-plugin 的 shade 插件需要借助 maven-jar-plugin 插件的 jar 目标打的 jar 包。官方还推荐了,将 shade 目标绑定到 package 及 package 以后的阶段,然后直接使用 mvn package 命令运行 shade 插件。

然后,我将 shade 目标绑定到了 package 阶段,pom 如下所示:

XML 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行 mvn package -pl :shade-project,但还有报错如下:

Bash 复制代码
xxx@xxxdeMacBook-Air maven-package-demo % mvn package -pl :shade-project
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.myphoenix:shade-project >---------------------
[INFO] Building shade-project 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mysql:mysql-connector-j:jar:8.0.33: MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates.
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ shade-project ---
[INFO] Copying 0 resource from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.13.0:compile (default-compile) @ shade-project ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug target 8] to target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.340 s
[INFO] Finished at: 2024-12-24T21:46:59+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project shade-project: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

报错原因:环境没有提供编译器,可能编译过程运行在 JRE 上,而不是 JDK 上。

解决方案https://www.cnblogs.com/wxmdevelop/p/18342861

查看 Maven 版本信息,貌似路径有些问题,如下所示,Maven 没有找到 JDK

Bash 复制代码
xxx@xxxdeMacBook-Air ~ % mvn -v
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /Users/xxx/softwares/developer/apache-maven-3.9.9
Java version: 1.8.0_421, vendor: Oracle Corporation, runtime: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "15.1", arch: "aarch64", family: "mac"

重新配置 JDK 和 Maven 环境变量:

Bash 复制代码
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home
M2_HOME=/Users/xxx/softwares/developer/apache-maven-3.9.9
PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export JAVA_HOME
export PATH
export CLASSPATH
export M2_HOME

再次查看 Maven 的版本信息,路径正确:

Bash 复制代码
xxx@xxxdeMacBook-Air ~ % mvn -v
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /Users/xxx/softwares/developer/apache-maven-3.9.9
Java version: 1.8.0_421, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "15.1", arch: "aarch64", family: "mac"

重新执行 mvn package -pl :shade-project,成功:

Bash 复制代码
xxx@xxxdeMacBook-Air maven-package-demo % mvn package -pl :shade-project
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for cn.myphoenix:shade-project:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for cn.myphoenix:big-marketing-wheel:jar should use a variable instead of a hard-coded path /Users/xxx/desktop/big-marketing-wheel-1.0-SNAPSHOT.jar @ line 55, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------------< cn.myphoenix:shade-project >---------------------
[INFO] Building shade-project 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mysql:mysql-connector-j:jar:8.0.33: MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates.
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ shade-project ---
[INFO] Copying 0 resource from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.13.0:compile (default-compile) @ shade-project ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug target 8] to target/classes
[INFO] 
[INFO] --- resources:3.3.1:testResources (default-testResources) @ shade-project ---
[INFO] skip non existing resourceDirectory /Users/xxx/Code_Workspace/IDEA_Workspace/maven_project/maven-package-demo/shade-project/src/test/resources
[INFO] 
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ shade-project ---
[INFO] Nothing to compile - all classes are up to date.
[INFO] 
[INFO] --- surefire:3.2.5:test (default-test) @ shade-project ---
[INFO] No tests to run.
[INFO] 
[INFO] --- jar:3.4.1:jar (default-jar) @ shade-project ---
[INFO] Building jar: /Users/xxx/Code_Workspace/IDEA_Workspace/maven_project/maven-package-demo/shade-project/target/shade-project-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- shade:3.6.0:shade (default) @ shade-project ---
[INFO] Including org.mybatis:mybatis:jar:3.5.16 in the shaded jar.
[INFO] Including com.mysql:mysql-connector-j:jar:8.0.33 in the shaded jar.
[INFO] Including com.google.protobuf:protobuf-java:jar:3.21.9 in the shaded jar.
[INFO] Dependency-reduced POM written at: /Users/xxx/Code_Workspace/IDEA_Workspace/maven_project/maven-package-demo/shade-project/dependency-reduced-pom.xml
[WARNING] mybatis-3.5.16.jar, mysql-connector-j-8.0.33.jar, protobuf-java-3.21.9.jar, shade-project-1.0-SNAPSHOT.jar define 1 overlapping resource: 
[WARNING]   - META-INF/MANIFEST.MF
[WARNING] maven-shade-plugin has detected that some files are
[WARNING] present in two or more JARs. When this happens, only one
[WARNING] single version of the file is copied to the uber jar.
[WARNING] Usually this is not harmful and you can skip these warnings,
[WARNING] otherwise try to manually exclude artifacts based on
[WARNING] mvn dependency:tree -Ddetail=true and the above output.
[WARNING] See https://maven.apache.org/plugins/maven-shade-plugin/
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/xxx/Code_Workspace/IDEA_Workspace/maven_project/maven-package-demo/shade-project/target/shade-project-1.0-SNAPSHOT.jar with /Users/xxx/Code_Workspace/IDEA_Workspace/maven_project/maven-package-demo/shade-project/target/shade-project-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.203 s
[INFO] Finished at: 2024-12-24T22:09:13+08:00
[INFO] ------------------------------------------------------------------------
相关推荐
神奇小梵21 小时前
《javaweb基础》后端篇————maven补充(用maven单元测试,maven常见问题)
单元测试·maven
开发者联盟league1 天前
maven核心插件介绍
java·maven
编程令我快乐1 天前
maven中deploy 的流程
java·maven
huaweichenai2 天前
使用maven搭建sprint-boot
java·maven·sprint
paopaokaka_luck2 天前
基于springboot3+vue3的美妆商城平台(AI问答、协同过滤算法、支付宝沙盒支付、物流快递api、Echarts图形化分析)
java·前端·spring boot·学习·maven·echarts
老马历写记4 天前
Maven POM 依赖管理总结
java·maven·system·pom·optional
sugar__salt6 天前
Spring、Spring Boot 与配置文件核心知识点详解
java·spring boot·后端·spring·java-ee·maven·intellij-idea
Architect_Lee8 天前
mac安装不同版本的maven
java·maven
BerryS3N8 天前
Java全平台开发环境配置与极速搭建指南(含JDK 8/17/21多版本管理、环境变量深层原理解析、Maven/Gradle构建工具及IDE集成)
java·ide·maven
sugar__salt9 天前
Spring 三层架构与 IoC、依赖注入完全指南
java·spring boot·后端·spring·架构·maven·intellij-idea