我有一个使用 Maven 构建的项目,需要向方法添加一些基本的性能跟踪。我决定为此使用 AspectJ。主要要求是将跟踪方面编织到生产类中,但仅适用于单元测试执行阶段。
我能够在 Maven 中配置编织,但是在执行测试后,应用了方面的相同生产类转到打包战争。
这种情况看起来很常见,但我无法在网络上找到解决方案。
您可以将方面放在测试目录中,并在测试编译配置中将 weaveMainSourceFolder 标志设置为 true
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<configuration>
<weaveMainSourceFolder>true</weaveMainSourceFolder>
</configuration>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
见http://mojo.codehaus.org/aspectj-maven-plugin/test-compile-mojo.html
我通过加载时编织解决了这个问题。这样,在运行单元测试时会发生编织(在运行单元测试时通过命令行参数),但您的方面不会编织到已发布的工件中。
例如,我想在我的单元测试中伪造系统时钟,但显然不会在实时代码中弄乱它。这是我的方面类:
@Aspect
public class TweakSystemAspects {
private static long timeOffsetMillis = 0;
public static void advanceTime(int amount, TimeUnit unit) {
timeOffsetMillis += unit.toMillis(amount);
}
@Around("call (long System.currentTimeMillis())")
public long aroundSystemTime(ProceedingJoinPoint joinPoint) throws Throwable {
return ((Long) joinPoint.proceed()) + timeOffsetMillis;
}
}
显然,这是通过调用TweakSystemAspects.advanceTime()
方法来伪造系统中时间的流逝在单元测试中使用的。为了完成加载时间编织,我只需要创建一个定义我的方面的 aop.xml 文件(并且编织应该发生在所有类中):
<aspectj>
<aspects>
<aspect name="com.mypackage.TweakSystemAspects"/>
</aspects>
<weaver options="-nowarn -Xlint:ignore"/>
<!-- During testing this was useful, but I didn't want all that output normally. -->
<!--<weaver options="-verbose -showWeaveInfo"/>-->
</aspectj>
最后,我在我的 pom 文件中进行了更改,以声明 AspectJ 运行时依赖项并告诉surefire 进行运行时编织。
<project ...>
...
<properties>
...
<version.aspectj>1.8.10</version.aspectj>
<properties>
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${version.aspectj}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- For Load Time Weaving of our AspectJ helper code -->
<argLine>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${version.aspectj}/aspectjweaver-${version.aspectj}.jar</argLine>
...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
我会在一个专用模块中执行此操作,使用 Maven 依赖插件在该generate-test-sources
阶段解压缩“正在测试”的工件,然后编织类并最终运行测试。
让我试着说明我的意思。让我们想象一下以下项目结构:
. |-- pom.xml `-- some-module // 这是我们要编织的模块 |-- pom.xml // 但仅用于测试目的 `-- ...
所以我的建议是做这样的事情:
. |-- pom.xml |-- 一些模块 | |-- pom.xml | `-- ... `-- test-module // 我们将在这里编织类,因为我们不想要 |-- pom.xml // 要打包在“生产”jar 中的跟踪方面 `-- ...
这个想法是有一个额外的“测试模块”,我们将在其中解压缩我们想要测试的工件,以便我们可以编织它的类而不影响“真正的”生产 jar。
为此,在调用 AspectJ 插件以编织“主”类之前,声明对被测模块的依赖项并用于dependency:unpack
解压类。target/classes
基于AspectJ 编译器 Maven 插件中提供的示例 - 使用类似以下内容应该可以工作:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
<build>
...
</project>