【问题标题】:AspectJ Maven Plugin cannot compile my projectAspectJ Maven 插件无法编译我的项目
【发布时间】:2015-01-07 21:56:24
【问题描述】:

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.liferay.maven.plugins</groupId>
            <artifactId>liferay-maven-plugin</artifactId>
            <version>${liferay.maven.plugin.version}</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
            <configuration>
                <autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
                <appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
                <appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
                <appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
                <liferayVersion>${liferay.version}</liferayVersion>
                <pluginType>portlet</pluginType>

            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.7</source>
                <target>1.7</target>
                <showWarnings>true</showWarnings>
                <failOnError>true</failOnError>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilationLevel>1.7</compilationLevel>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.4</version>
    <type>jar</type>
</dependency>

mvn clean install 之后我看到以下异常:

[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ tvbs-portlet ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[ERROR] Missing message: configure.incompatibleComplianceForSource in: org.aspectj.ajdt.ajc.messages
    <unknown source file>:<no line information>

[ERROR] no sources specified
    <unknown source file>:<no line information>

[ERROR] AspectJ Compiler 1.8.2

    Usage: <options> <source file | @argfile>..

AspectJ-specific options:
    -inpath <list>      use classes in dirs and jars/zips in <list> as source

谁能给我一些解决方案?

【问题讨论】:

  • 你介意接受+赞成我的回答(有截图的那个)吗?我仍然认为它是正确的,只是再次偶然发现它。

标签: java maven jakarta-ee aspectj aspectj-maven-plugin


【解决方案1】:

这似乎是一个已知问题http://jira.codehaus.org/browse/MASPECTJ-125

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

<complianceLevel>1.6</complianceLevel>

【讨论】:

  • 刚刚使用合规级别 1.8 对此进行了测试,我可以验证它确实为我修复了错误。
  • @RIng 同样适用于 java 11
【解决方案2】:

将插件配置更改为以下即可:

<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] ^^

会导致aspectj插件参数不正确吗?

【讨论】:

  • 添加 忽略
【解决方案3】:

更新:虽然我在这个答案中所说的关于 AspectJ Maven 配置的内容都是正确的,但手头具体问题的根本原因 - 糟糕的 Maven 依赖管理 - 在我的 other answer 中有所描述.如果那个是公认的答案而不是这个答案会更好。


  • 用户 codelion 的提示有道理,请将您的 &lt;compilationLevel&gt; 标签(错字?)更改为 &lt;complianceLevel&gt;
  • 无需降级到插件版本 1.6,可以保留 1.7。
  • &lt;execution&gt;部分也不需要再指定配置,插件级别的就够了。
  • 请注意,插件 1.7 中的默认 AspectJ 版本是 1.8.2,因此您对 1.7.4 的运行时依赖可能有效,但如果我是您,我也会升级该版本,最好与插件版本同步。这不是硬性要求,但我认为这是有道理的。
  • 也许您甚至想在插件和运行时升级到当前版本的 AspectJ 1.8.4。这也可以通过在插件配置中添加对所需 aspectjtools 版本的依赖项来实现:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.4</aspectj.version>
    </properties>

    <build>
        <pluginManagement>
             <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <showWeaveInfo>true</showWeaveInfo>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.source-target.version}</complianceLevel>
                        <encoding>UTF-8</encoding>
                        <verbose>true</verbose>
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
    </dependencies>

【讨论】:

  • 感谢您的回答。我尝试使用您的配置,但仍然得到以前的结果(“[ERROR] PESSIMISTIC_WRITE 无法解析或不是字段”,“[ERROR] EntityManager 类型中的方法 createQuery(String) 不适用于参数(String , Class)" 和其他类似的错误)。也许 aspectj 编译器不同于 javac 并且在枚举等方面存在一些问题?
  • 你可以让我通过 GitHub 或类似的方式访问你的项目吗?我已经用 AJMaven 插件和 AJ 本身做了很多事情,所以当我看到实际代码时,也许我可以更好地帮助你。如果无法进行公开回购,我们也可以进行私人安排。
  • 我在github.com/kriegaex/test-aspectj 分叉了你的仓库,但即使在删除了父 POM(我没有)并手动将虚拟值设置为 liferay 参数后,我也无法构建,因为我无法访问你的maven.elcom.spb.ru 上的 Maven 仓库,可能是因为它是一个内部仓库。此外,您已将您的 Maven 'target' 文件夹提交给 Git,这是您不应该做的。我已经在我的叉子中删除了它。您能否为我提供一个能够重现您的问题的独立项目?我什至没有看到真正的问题。
  • 在我的 new answer 解释了为什么这样有效以及真正的错误之后,我才注意到您的最后一条评论。
【解决方案4】:

查看了您的 Maven 项目 https://github.com/dmitrievanthony/test-aspectj 我发现

  • 该问题与 AspectJ Maven 插件完全无关,
  • 同样的编译错误也出现在 Maven Compiler Plugin 和
  • 您的问题的根本原因只是糟糕的依赖管理。

这是来自 IntelliJ IDEA 的“查找类”的屏幕截图(全尺寸 here):

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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>1.0.2.GA</version>
    </dependency>

也许你应该清理你的依赖。为此,您可以使用带有 dependency:analyzedependency:tree 等目标的 Maven 依赖插件。

【讨论】:

    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 2020-12-04
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2021-07-23
      相关资源
      最近更新 更多