java - 使用 AspectJ 编译器而不是 Javac 编译时出错

标签 java hibernate maven jakarta-ee aspectj

我有一个多模块项目。该方面目前已添加到“核心”项目中。在此处执行 mvn clean install 时它有效。然而,尝试在父项目上执行 mvn clean install 在编译其他项目之一时失败并出现此错误:

The type org.hibernate.annotations.CacheConcurrencyStrategy cannot be resolved. It is indirectly referenced from required .class files

如果我在该项目中添加 Hibernate 核心依赖项,它也可以工作,但是向不应具有依赖项的项目添加依赖项没有意义 - 因此这不是解决方案。当使用 javac 编译时,它工作正常。

这是什么原因?我该如何修复它,以便我可以使用 AspectJ 编译器而不会将依赖项泄漏到不应该具有它的项目中?

我在父 POM 中有这个配置:

 <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.5</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

更新

我才知道。每次运行 mvn clean install 都会失败。但是,运行 mvn [clean] install 一次失败。然后在没有 clean 的情况下运行 mvn install 就可以了。我看到目标文件夹中的 builddef.lst 是它工作和失败的原因,具体取决于您是否运行干净。所以现在我的问题是:如何自动生成这个文件?

父 POM 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>core-lib</artifactId>
    <name>core-lib</name>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <complianceLevel>1.6</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

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

    <modules>
        <module>core-xyz</module>
        <module>core-xyz2</module>
    </modules>
</project>

最佳答案

在 Maven 调用上启用调试以深入挖掘。您应该观察到 aspectj 编译仅在第一次使用 clean 的 maven 调用期间被调用。由于 builddef.lst 在第一次调用后已经存在,因此不使用 clean 调用会跳过 aspectj 编译。

此 aspectj 编译插件行为之前已观察到并在此处进行了描述:

http://out-println.blogspot.com/2007/08/compile-time-checks-with-aspectj-part-2.html?m=1

您需要更深入地研究以解决根本问题,但正如一位评论者已经建议的那样,aspectj 编译器应该只在需要它的模块中启用。

否则,正如您已经观察到的那样,aspectj 编译需要额外的依赖项。通过将 aspectj 编译限制为仅需要它的模块,我已经毫无问题地将 aspectj compile 合并到我自己的工作中。

关于java - 使用 AspectJ 编译器而不是 Javac 编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24206634/

相关文章:

java - 两个或多个参数的 setParameter 在 Jpa 中不起作用(Mysql 注入(inject))

java - Spring Data JPA (Hibernate) 与动态条件的一对多关系

java - JSP Out.Println() 无法解析,使用 Maven 的 WebApp 原型(prototype)创建的项目

maven - 无法让 Sonar 处理多模块 Maven 项目的 jacoco exec 文件

java - ChromeDriver 未通过 Java ZAP API 代理本地流量

java - 如何将 Gradle + Google Sheets API + Java 制作成单个可下载程序?

java - 我可以在可区分实体层次结构的子类上使用 CompositeId 吗?

groovy - 如何在 Maven 配置文件中将 ${basedir} 属性的反斜杠替换为斜杠

java - 是否可以在没有变量的情况下计算方法执行时间?

java - 如何在 IntelliJ 增量构建中获取所有带有注释的元素?