Java AspectJ Maven插件无法编译我的项目

Java AspectJ Maven插件无法编译我的项目,java,maven,jakarta-ee,aspectj,aspectj-maven-plugin,Java,Maven,Jakarta Ee,Aspectj,Aspectj Maven Plugin,我尝试将aspectj maven插件用于带有aspectj编译器的编译项目,然后尝试将类打包到“war”文件中。不幸的是,它不适用于以下配置(pom.xml): 有人能给我建议一些解决办法吗?这似乎是一个已知的问题 您可以通过将以下内容添加到pom文件来修复它 <complianceLevel>1.6</complianceLevel> 1.6 将插件配置更改为以下内容后,它将正常工作: <plugin> <groupId>org.co

我尝试将aspectj maven插件用于带有aspectj编译器的编译项目,然后尝试将类打包到“war”文件中。不幸的是,它不适用于以下配置(pom.xml):


有人能给我建议一些解决办法吗?

这似乎是一个已知的问题

您可以通过将以下内容添加到pom文件来修复它

<complianceLevel>1.6</complianceLevel>
1.6

将插件配置更改为以下内容后,它将正常工作:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <complianceLevel>1.7</complianceLevel>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </execution>
    </executions>
</plugin>

org.codehaus.mojo
aspectj maven插件
1.6
1.7
1.7
1.7
UTF-8
过程源
编译
1.7
1.7
1.7
但在此之后,我得到了许多不同的编译错误:

[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.6:compile (default) on project tvbs-portlet: Compiler errors:
[ERROR] error at Entitle.class, entitleId, LockModeType.PESSIMISTIC_WRITE);
[ERROR]
[ERROR] /Users/<...>/ejb/BillingEJB.java:43:0::0 PESSIMISTIC_WRITE cannot be resolved or is not a field
[ERROR] error at .createQuery("select e from Entitle e " +
[ERROR]
[ERROR] /Users/<...>/ejb/EntitleEJB.java:62:0::0 The method createQuery(String) in the type EntityManager is not applicable for the arguments (String, Class<Entitle>)
[ERROR] error at return entityManager.createQuery(
[ERROR] ^^
[ERROR]无法在tvbs项目portlet上执行目标org.codehaus.mojo:aspectj maven插件:1.6:compile(默认):编译器错误:
[错误]在authorize.class、authorizeId、LockModeType.悲观写入)处出错;
[错误]
[错误]/Users//ejb/BillingEJB.java:43:0::0无法解析悲观写入,或者它不是字段
[ERROR]在.createQuery处出错(“从授权e中选择e”+
[错误]
[错误]/Users//ejb/authenticejb.java:62:0::0类型EntityManager中的方法createQuery(String)不适用于参数(String,Class)
[错误]返回entityManager.createQuery时出错(
[错误]^^

可能的原因是aspectj插件参数不正确?

更新:虽然我在这个答案中所说的关于aspectj Maven配置的内容都是正确的,但我的答案中描述了当前具体问题的根本原因——Maven依赖关系管理不好。如果这是公认的答案而不是这个答案,那就更好了


  • 用户codelion的提示很有意义,请将您的
    标记(打字?)更改为
  • 没有必要降级到插件版本1.6,您可以保留1.7
  • 也不需要在
    部分中再次指定配置,插件级别的配置就足够了
  • 请注意,插件1.7中默认的AspectJ版本是1.8.2,因此您对1.7.4的运行时依赖可能会起作用,但如果我是您,我也会升级该版本,与插件版本保持最佳同步。这不是硬性要求,但我认为这是有意义的
  • 可能您甚至想在插件和运行时中升级到当前版本AspectJ 1.8.4。这也可以通过在插件配置中添加所需AspectJ工具版本的依赖项来实现:

UTF-8
1.8
1.8.4
org.codehaus.mojo
aspectj maven插件
1.7
真的
${java.source-target.version}
${java.source-target.version}
忽视
${java.source-target.version}
UTF-8
真的
过程源
编译
测试编译
org.aspectj
aspectjtools
${aspectj.version}
org.codehaus.mojo
aspectj maven插件
org.aspectj
aspectjrt
${aspectj.version}
运行时
org.aspectj
aspectjrt

看过你的Maven项目后,我发现

  • 这个问题与AspectJ Maven插件完全无关
  • Maven编译器插件和
  • 问题的根本原因就是糟糕的依赖关系管理
以下是IntelliJ IDEA的“查找类”截图(全尺寸):

如您所见,类
LockModeType
存在于3个(三个!)依赖项中,其中一个包含不包含预期枚举值的类版本。如果删除此依赖项,代码将编译


org.hibernate
ejb3持久性
1.0.2.GA

也许你应该清理你的依赖项。你可以使用Maven依赖项插件,目标是
Dependency:analyze
Dependency:tree

确保模块有源代码,如*.java等。
当我在4.0.6版上编译CAS时,发生了此错误,我发现CAS服务器uber webapp在src文件夹中没有任何源代码。只需从父pom.xml中删除该模块。

谢谢您的回答。我尝试使用您的配置,但仍然得到了以前的结果(“[错误]悲观写入无法解决或不是字段”,“[错误]EntityManager类型中的方法createQuery(String)不适用于参数(String,Class)”和其他类似错误。也许aspectj编译器不同于javac,并且在枚举等方面存在一些问题。你能让我通过GitHub或类似的方式访问你的项目吗?我已经用AJMaven插件ans AJ本身做了很多事情,所以当我看到实际的代码时,也许我可以更好地帮助你。如果无法公开回购,我们也可以私下安排。我在,但即使在移除母公司POM(whic
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <complianceLevel>1.7</complianceLevel>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </execution>
    </executions>
</plugin>
[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.6:compile (default) on project tvbs-portlet: Compiler errors:
[ERROR] error at Entitle.class, entitleId, LockModeType.PESSIMISTIC_WRITE);
[ERROR]
[ERROR] /Users/<...>/ejb/BillingEJB.java:43:0::0 PESSIMISTIC_WRITE cannot be resolved or is not a field
[ERROR] error at .createQuery("select e from Entitle e " +
[ERROR]
[ERROR] /Users/<...>/ejb/EntitleEJB.java:62:0::0 The method createQuery(String) in the type EntityManager is not applicable for the arguments (String, Class<Entitle>)
[ERROR] error at return entityManager.createQuery(
[ERROR] ^^