我正在使用aspectj通过第三方注释来确定方法的目标。然而,我不能保证这个注解在类路径上是可用的。有没有一种方法可以从一个可选的依赖项中定位一个注释?
例如,我可能想要针对JUnit 5的@ParameterizedTest注释。我的.aj文件如下所示:
public aspect Example {
pointcut beforeTest(): @annotation(ParamterizedTest);
before(): beforeTest() {
System.out.println("This is a Parameterized Test!");
}
}但是,如果我的项目使用的是Maven4,或者没有包含junit-jupiter-params库,那么JUnit将无法编织,因为它找不到类:
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-params的依赖。)
发布于 2019-02-10 11:45:47
您的方面使用该类,即使您的示例代码没有显示它,在您的方面之上也必须有一个它的导入。这意味着它绝对是方面库的依赖项,而不是可选的依赖项。因此,您必须在库的Maven POM中对其进行定义。任何其他的事情都没有意义。定义它到底有什么大不了的?正确的依赖关系管理是Maven的目标。
更新:感谢您的澄清评论。
您可以使用基于注释的语法而不是本机语法,因为它不需要导入。切入点将不匹配,因为随后将显示另一个Xlint警告。
如果您可以选择使用AspectJ编译器编译其他项目,我还建议您研究一下@DeclareError和@DeclareWarning。它将帮助您强制执行在编译期间不使用参数化测试的策略,而不是在运行时抛出错误或记录某些内容。AspectJ编译器不会使用该注释编译测试。
https://stackoverflow.com/questions/54526066
复制相似问题