我正在使用aspectj以使用第三方注释的方法为目标。然而,我不能保证这个注释在类路径上是可用的。有没有一种方法可以从可选依赖项中定位注释?
举个例子,我可能想针对JUnit5
@ParameterizedTest
注释。我的aj文件看起来像这样:
public aspect Example {
pointcut beforeTest(): @annotation(ParamterizedTest);
before(): beforeTest() {
System.out.println("This is a Parameterized Test!");
}
}
但是,如果我的项目使用JUnit 4,或者不包括JUnit jupiter params库,那么Maven将无法编织,因为它找不到类:
2019-02-04 16:37:37.649 [ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.11:test-compile (default) on project ExampleProject: AJC compiler errors:
2019-02-04 16:37:37.650 [ERROR] error at (no source information available)
2019-02-04 16:37:37.656 [ERROR] /jenkins/workspace/exampleProject/src/test/java/com/example/ExampleTest.java:0::0 can't determine annotations of missing type org.junit.jupiter.params.ParameterizedTest
2019-02-04 16:37:37.657 [ERROR] when weaving type com.example.ExampleTest
2019-02-04 16:37:37.657 [ERROR] when weaving classes
2019-02-04 16:37:37.657 [ERROR] when weaving
2019-02-04 16:37:37.658 [ERROR] when batch building BuildConfig[null] #Files=21 AopXmls=#0
2019-02-04 16:37:37.658 [ERROR] [Xlint:cantFindType]
我试着把这个库添加到
aspectj-maven-plugin
是的
<dependencies>
像这样的部分:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>com.example</groupId>
<artifactId>example-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.1.1</version>
</dependency>
</dependencies>
</plugin>
... 但这没什么区别。
有没有一种方法可以在不需要添加依赖项的情况下实现这一点?如果有使用第三方注释(如果存在)注释的方法,我非常希望切入点起作用,否则就被忽略。
(为了实现junit示例的目的,我构建了这个示例,以确认它的工作原理与我真正的问题相同,我的方面库确实声明了对junit jupiter参数的依赖。)