AspectJ 从依赖项编织不适用于项目



我有一个Java 8 Maven项目,它定义了一个自定义注释和一个方面。 在该项目本身中运行测试代码时,它会将该方面应用于带注释的类。 然后我正在打包和安装项目。

然后,我将这种依赖引入到一个新项目(非 Spring(中。 然后,新项目没有将方面应用于其类,尽管它确实引入了新的注释。

我如何使用单个JAR来定义注释和方面,并将其应用于Maven的所有项目?

您需要在pom.xml的aspectj-maven-plugin配置中将方面项目依赖项指定为方面库。假设您的方面模块具有 groupid:artifactidgroupid:aspect-module。您的pom.xml应如下所示:

<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

请注意,我正在关闭maven-compiler-plugin,因为它们往往会用aspectj-maven-plugin覆盖彼此的输出,并且 AspectJ 编译器应该能够编译普通的 java 文件并将它们编织在同一步骤中,因此使用 maven-compiler-plugin 是多余的。如果您使用的是 Eclipse + AJDT,则此 maven 配置将更好地反映您在开发过程中 IDE 中发生的情况。

最新更新