【问题标题】:Maven-compiler-plugin:3.3: testCompile (default-testCompile) failing on compile Attribute$Error cannot be cast to Attribute$ClassMaven-compiler-plugin:3.3: testCompile (default-testCompile) 编译失败 Attribute$Error 不能转换为 Attribute$Class
【发布时间】:2026-02-08 09:50:01
【问题描述】:

我一直在寻找解决这个问题的方法,但我似乎无法找到正确的答案。我相信,我有一个独特的情况,因此问题可能更难以解决。
问题的核心是我在运行mvn test 命令时收到的错误消息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project DriveServiceFitnesse: Fatal error compiling: java.lang.ClassCastException: com.sun.tools.javac.code.Attribute$Error cannot be cast to com.sun.tools.javac.code.Attribute$Class -> [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

但是mvn compile 没有通过错误。 但是因为我需要 mvn test 来生成测试源,所以我的测试实际上并没有工作。我知道这一点,因为当我运行测试时,我得到了这个错误:

    Error:java: java.lang.ClassCastException: 
com.sun.tools.javac.code.Attribute$Error cannot be cast to 
com.sun.tools.javac.code.Attribute$Class

用于编译和运行的项目没有问题。当我尝试将我的项目从 Maven 转换为 Gradle 时,出现了这个问题。我遇到了类似的问题,所以我关闭了分支并拉下了我原来的工作代码/项目,现在我似乎无法修复它。

我们将非常感谢您对此事的任何帮助。如果可能有帮助,我将发布我的 pom.xml 文件。

*编辑:根据我在收到的堆栈跟踪消息中粘贴的建议:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project DriveServiceFitnesse: Fatal error compiling: java.lang.ClassCastException: com.sun.tools.javac.code.Attribute$Error cannot be cast to com.sun.tools.javac.code.Attribute$Class -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project DriveServiceFitnesse: Fatal error compiling
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
        at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:836)
        at org.apache.maven.plugin.compiler.TestCompilerMojo.execute(TestCompilerMojo.java:153)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)

【问题讨论】:

  • 尝试 mvn test -X 并查看确切的堆栈跟踪。
  • 哦,我做到了,堆栈跟踪也没有多大帮助。在另一个回复中粘贴堆栈跟踪
  • 您运行的 Maven 和 Java 版本是什么?
  • maven 是 3.3.3,java 是 1.7.0.71

标签: java maven maven-compiler-plugin


【解决方案1】:

对我来说,这个问题是由于我试图在我的 pom.xml 中使用“默认 maven 配置文件”以及另一个 e2e 测试配置文件引起的。我切换回普通的 pom,它现在可以工作了。仍然不确定真正的原因。

【讨论】:

    【解决方案2】:

    我尝试了多种解决方案来查找问题的根本原因,并观察到我的机器编译类的时间比平时要长,因此我们尝试增加配置中的 maxmem 选项。

    我已将编译器插件的 maxmem 选项从 1024m 更改为 2048m,这解决了问题

    【讨论】:

      【解决方案3】:

      修复了这个错误。是我的疏忽。问题是由于某种原因,我使用“项目结构”(在 intellij 中)导入的本地 jar 不知何故已过期,即该库仍在列出,但没有被正确拉入项目。我删除了引用并重新导入,似乎一切正常......叹息。

      【讨论】:

        最近更新 更多