Maven 相关错误总结-2【持续更新】

2022年04月26日 15:55 · 阅读(5252) ·

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2021.2.1
Maven Maven 3.6.3

相关内容

Maven 相关错误总结-1【持续更新】

错误-the Java Runtime only recognizes class file versions up to 52.0

1.问题描述

编译一个 Java 项目时,报下面的错误

  1. [INFO] --- spring-boot-maven-plugin:3.0.0-M2:build-info (default) @ tdms-cloud-service ---
  2. [WARNING] Error injecting: org.springframework.boot.maven.BuildInfoMojo
  3. java.lang.TypeNotPresentException: Type org.springframework.boot.maven.BuildInfoMojo not present
  4. [WARNING] Error injecting: org.springframework.boot.maven.BuildInfoMojo
  5. java.lang.TypeNotPresentException: Type org.springframework.boot.maven.BuildInfoMojo not present
  6. at org.eclipse.sisu.space.URLClassSpace.loadClass (URLClassSpace.java:147)
  7. Caused by: java.lang.UnsupportedClassVersionError: org/springframework/boot/maven/BuildInfoMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
  8. at java.lang.ClassLoader.defineClass1 (Native Method)

大致是说 lang.UnsupportedClassVersionError:org/springframework/boot/maven/BuildInfoMojo 是由 Java 运行时的最新版本(类文件版本 61.0)编译的,该版本的 Java 运行时只识别 52.0 以下的类文件版本

2.问题分析

根据 Spring Boot with spring version 2.5.7 fails repackage with jdk 1.8 这篇文章中下面的说法

Version of ‘spring-boot-maven-plugin’ plugin used in you pom.xml is 3.0.0-M1, which can be used only with Java 17 or newer. Switch to 2.5.7, if it’s really version which you want to use for spring boot project.
在你的 pom 中使用的“spring boot maven插件”版本。xml 是 3.0.0-M1,只能与 Java 17 或更高版本一起使用。切换到 2.5.7,如果它确实是您想要用于 spring boot 项目的版本。

Well i had to explicitly specify the version of the plugin to resolve this issue. The version was not managed by the spring-boot parent added as part of my managed-dependencies. Appreciate the support rufanov and @khmarbaise

为了解决这个问题,我必须明确指定插件的版本。该版本不是由作为托管依赖项的一部分添加的 spring boot 父项管理的。感谢鲁法诺夫和@khmarbaise的支持

似乎是 spring-boot-maven-plugin 没有指定版本导致的。编译内容如下

pom.xml 相关内容如下

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

根据他的说法,要指定这个插件的版本

3.问题解决

于是我修改了项目中所有 pom.xml 文件相关的部分,修改如下

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <version>2.2.6.RELEASE</version>
  7. </plugin>
  8. </plugins>
  9. </build>

重新编译,编译通过,编译内容如下,spring-boot-maven-plugin 插件版本已经变成了我们指定的

至于我这里为什么指定了 2.2.6.RELEASE,可以参考 找不到插件 ‘org.springframework.boot:spring-boot-maven-plugin:’

错误-找不到依赖项 ‘commons-lang:commons-lang3:3.7’

问题描述及解决方案

以前的一个老项目,之前编译是没问题的,最近编译项目,报错

  1. import org.apache.commons.lang.StringUtils;
  2. 无法解析符号 'StringUtils'

查看了对应的 pom.xml 文件

父项目 pom.xml

  1. <properties>
  2. <commons-lang3.version>3.7</commons-lang3.version>
  3. </properties>
  4. <dependencyManagement>
  5. <dependency>
  6. <groupId>commons-lang</groupId>
  7. <artifactId>commons-lang3</artifactId>
  8. <version>${commons-lang3.version}</version>
  9. </dependency>
  10. </dependencyManagement>

提示,找不到依赖项 ‘commons-lang:commons-lang3:3.7’

我在报错的子模块 pom.xml 中,加入下面的引用

  1. <dependencies>
  2. <dependency>
  3. <groupId>commons-lang</groupId>
  4. <artifactId>commons-lang3</artifactId>
  5. </dependency>
  6. </dependencies>

重新加载了所有 Maven 项目,提示

  1. Could not find artifact commons-lang:commons-lang3:pom:3.7 in thirdarty (https://mirrors.luoma.com/repository/maven/thirdparty)

说明 commons-lang 仓库找不到了,使用了下面的引用来进行替换

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.7</version>
  5. </dependency>

报错文件中使用 lang3.StringUtils 替代了 lang.StringUtils,问题解决

  1. //import org.apache.commons.lang.StringUtils;
  2. import org.apache.commons.lang3.StringUtils;

问题分析

为什么使用 org.apache.commons 替换了 commons-lang 问题就解决了?
难道是 commons-lang 已经失效了?

使用百度搜索 maven 中 commons-lang 已经过时了嘛
其中这篇 What’s new in Commons Lang 3.0? 是这样回复的

Last Published: 27 November 2023
上次修改时间: 2023 年 11 月 27 日

The big story

Lang is now Java 5 based. We’ve generified the API, moved certain APIs to support varargs and thrown out any features that are now supported by Java itself. We’ve removed the deprecated parts of the API and have also removed some features that were deemed weak or unnecessary. All of this means that Lang 3.0 is not backwards compatible.
Lang 现在基于 Java 5。我们已经生成了 API,移动了某些 API 来支持 varargs ,并抛弃了现在由 Java 本身支持的任何功能。我们删除了 API 中已弃用的部分,并删除了一些被认为较弱或不必要的功能。所有这些都意味着 Lang 3.0 不向后兼容。

To that end we have changed the package name, allowing Lang 3.0 to sit side-by-side with your previous version of Lang without any bad side effects. The new package name is the exciting and original org.apache.commons.lang3. This also forces you to recompile your code, making sure the compiler can let you know if a backwards incompatibility affects you.
为此,我们更改了软件包名称,允许 Lang 3.0 与您以前的 Lang 版本并排放置,而不会产生任何不良副作用。新的包名是令人兴奋和原创 org.apache.commons.lang3 的。这也迫使你重新编译代码,确保编译器可以让你知道向后不兼容是否影响了你。

As you’d expect, there are also new features, enhancements and bugs fixed.
如您所料,还修复了新功能、增强功能和错误。

Migrating from 2.x

Java code
Despite the label of backwards incompatibility, in the vast majority of cases the simple addition of a ‘3’ to an import statement will suffice for your migration.

Change: import org.apache.commons.lang -> import org.apache.commons.lang3

Maven
groupId: commons-lang -> org.apache.commons
artifactId: commons-lang -> commons-lang3

错误: 程序包javax.annotation不存在

参考:java 程序包 javax.annotation 不可见,【解决方法】

问题描述

之前打包项目一直正常,今天打包项目突然报错

  1. 错误: 程序包javax.annotation不存在
  2. 错误: 找不到符号

发现打包的时候,用的是 JDK 17

解决办法

  • 使用 JDK1.8 打包,问题解决

错误-找不到符号

  • 问题描述

  • 问题原因

当前引用的 Jar 包版本不是最新的,导致某些属性没有。

  • 问题解决

清理一下依赖,重新编译即可。
但是我使用 Maven-Clean 之后,重新加载 Maven,还是没有拉取到最新的包。

所以我使用最粗暴的方法,直接在外部库找到对应的 Jar 包-打开库设置

找到 Jar 包对应的路径

删除 Jar 包的内容

然后重新加载所有 Maven 项目,重新拉取最新的 Jar 包,问题解决。

错误-找不到符号(操作步骤不对导致)

  • 项目结构如下,receipt-server 调用 receipt-contract
  1. receipt-contract
  2. receipt-server

问题描述

  • 使用 IDEA 编译 receipt-server 项目,提示找不到符号

  • 但是点击 dto.getExportStatusDesc() 能够进入定义的方法,而且这个报错的项目也能够正常启动,就是编译报错。

  • 说明: dto.getExportStatusDesc() 此方法位于 receipt-contract 项目下。

问题分析

本地仓库生成文件对比

  • 首先想到的就是没有找到最新的仓库中的包,于是我来到 maven 仓库目录 C:\Program Files\Maven\Repository

  • 搜索 receipt-contract,当前搜索时间是 2025年4月16日,发现报错方法的包更新时间是 2024年10月,不是最新的。

  • 通过重新打 jar 包 receipt-contract 复制到对应目录进行替换后,编译成功,说明就是来这个地方找包进行编译。

receipt-contract 项目使用 DOS mvn clean install

  • 使用 DOS 构建结果,发现最终构建到了 C:\Users\luoma\.m2\repository
  1. receipt-contract>mvn clean install
  2. [INFO] Scanning for projects...
  3. [INFO] --- install:3.1.1:install (default-install) @ receipt-contract ---
  4. [INFO] Installing D:\luoma\Proj-Etms\Code\etms-receipt\receipt-contract\pom.xml to C:\Users\luoma\.m2\repository\com\oa\fm\etms\receipt\receipt-contract\1.0-SNAPSHOT\receipt-contract-1.0-SNAPSHOT.pom
  5. [INFO] Installing D:\luoma\Proj-Etms\Code\etms-receipt\receipt-contract\target\receipt-contract.jar to C:\Users\luoma\.m2\repository\com\oa\fm\etms\receipt\receipt-contract\1.0-SNAPSHOT\receipt-contract-1.0-SNAPSHOT.jar
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] BUILD SUCCESS
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] Total time: 12.279 s
  10. [INFO] Finished at: 2025-04-15T16:18:47+08:00
  11. [INFO] ------------------------------------------------------------------------

使用 DOS 查看 mvn 默认本地仓库路径

  • 输入命令:mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
  1. C:\Users\luoma\.m2\repository

修改 mvn 默认本地仓库路径

  • 修改 :C:\Program Files\apache-maven-3.9.4\conf\settings.xml
  1. <localRepository>C:\Program Files\Maven\Repository</localRepository>
  • 使用 DOS 再次查看,输入命令:mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
  1. C:\Program Files\Maven\Repository

receipt-contract 项目使用 DOS 再次 mvn clean install

  1. receipt-contract>mvn clean install
  2. [INFO] Scanning for projects...
  3. [INFO] --- install:3.1.1:install (default-install) @ receipt-contract ---
  4. [INFO] Installing D:\luoma\Proj-Etms\Code\etms-receipt\receipt-contract\pom.xml to C:\Program Files\Maven\Repository\com\oa\fm\etms\receipt\receipt-contract\1.0-SNAPSHOT\receipt-contract-1.0-SNAPSHOT.pom
  5. [INFO] Installing D:\luoma\Proj-Etms\Code\etms-receipt\receipt-contract\target\receipt-contract.jar to C:\Program Files\Maven\Repository\com\oa\fm\etms\receipt\receipt-contract\1.0-SNAPSHOT\receipt-contract-1.0-SNAPSHOT.jar
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] BUILD SUCCESS
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] Total time: 12.279 s
  10. [INFO] Finished at: 2025-04-15T16:18:47+08:00
  11. [INFO] ------------------------------------------------------------------------

IDEA 中 receipt-server 再次编译

  • 编译成功,且仓库目录对应 receipt-contract 目录也是最新的。

为什么编译默认要去 C:\Program Files\Maven\Repository 找仓库

  • 因为我这个项目的仓库目录设置就是 C:\Program Files\Maven\Repository

问题原因

(1)项目使用的仓库路径是 maven 配置中的本地仓库:C:\Program Files\Maven\Repository
(2)修改了 receipt-contract 内容后,IDEA 中 receipt-server 进行 compile ,从 C:\Program Files\Maven\Repository 找的包就未重新打包过的。
(3)修改了 receipt-contract 内容后,需要在 IDEA 中对 receipt-contract 重新 install 生成最新的包后, receipt-server 再重新 compile 即可。
(4)所以根本原因就是操作步骤不对,不是配置的问题。

其它说明:
(1)和 maven 默认配置 C:\Program Files\apache-maven-3.9.4\conf\settings.xml 无关。
(2)maven 默认配置只有使用 DOS 操作没有指定配置文件时,才会使用这个 maven 默认配置。
(3)在 IDEA 中使用 maven 插件进行 installcompile 时,使用仓库为上图中本地仓库的配置: C:\Program Files\Maven\Repository
(4)上面使用 DOS 对 receipt-contract 进行 mvn clean install,和修改了默认配置文件,只是表明了在 IDEA 中进行 compile 前,使用了 DOS 进行了打包,无形中解决了没有在 IDEA 中先进行 install 操作导致 compile 报错的问题而已。其实都在 IDEA 中进行操作即可。