[英]Weaving production classes with AspectJ aspects in Maven for tests execution only
I have a project with Maven build and need to add some basic performance tracing to methods. 我有一个Maven构建的项目,需要为方法添加一些基本的性能跟踪。 I decided to use AspectJ for this.
我决定使用AspectJ。 Main requirement is to weave tracing aspect into production classes but only for unit tests execution phase .
主要要求是将跟踪方面编织到生产类中, 但仅用于单元测试执行阶段 。
I was able to configure weaving in Maven however after execution of tests same production classes with aspect applied go to packaged war. 我能够在Maven中配置编织,但是在执行测试后,应用了方面的相同生产类转到打包战争。
The case looks like pretty common nevertheless I wasn't able to find solution for it in web. 这个案子看起来很常见,但我无法在网上找到解决方案。
You can put your aspects in the test directory and set the weaveMainSourceFolder flag to true in the test-compile configuration 您可以将方面放在测试目录中,并在test-compile配置中将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>
See http://mojo.codehaus.org/aspectj-maven-plugin/test-compile-mojo.html 请参阅http://mojo.codehaus.org/aspectj-maven-plugin/test-compile-mojo.html
I solved this with load-time weaving. 我用加载时编织解决了这个问题。 That way the weaving happens when your unit-tests are run (via a command line argument when running unit-tests), but your aspects are not woven into the published artifacts.
这样就可以在运行单元测试时(在运行单元测试时通过命令行参数)进行编织,但是您的方面不会编织到已发布的工件中。
For example, I wanted to fake out the system clock in my unit-tests, but obviously not mess with it in the live code. 例如,我想在我的单元测试中伪造系统时钟,但显然不会在实时代码中弄乱它。 Here's my aspect class:
这是我的方面类:
@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;
}
}
Obviously, this is used in unit-tests by calling the TweakSystemAspects.advanceTime()
mehtod to fake the passage of time in the system. 显然,这是通过调用
TweakSystemAspects.advanceTime()
方法在单元测试中使用来伪造系统中的时间流逝。 To accomplish the load time weaving, I just had to make an aop.xml file that defined my aspects (and that weaving should happen in all classes): 为了完成加载时间编织,我只需要创建一个定义我的方面的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>
Finally, I made changes in my pom file to declare the AspectJ runtime dependency and tell surefire to do run-time weaving. 最后,我在我的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>
I would do that in a dedicated module, use the Maven Dependency Plugin to unpack the artifact "under test" during the generate-test-sources
phase, then weave the classes and finally run the tests. 我会在专用模块中执行此操作,使用Maven Dependency Plugin在
generate-test-sources
阶段解压缩“正在测试”的工件,然后编织类并最终运行测试。
Let me try to illustrate what I mean. 让我试着说明一下我的意思。 Let's imagine the following project structure:
让我们假设以下项目结构:
. |-- pom.xml `-- some-module // this is the module that we want to weave |-- pom.xml // but only for testing purpose `-- ...
So my suggestion is to do something like this: 所以我的建议是做这样的事情:
. |-- pom.xml |-- some-module | |-- pom.xml | `-- ... `-- test-module // we're going to weave the classes here because we don't want |-- pom.xml // the tracing aspect to be packaged in the "production" jar `-- ...
The idea is to have an additional "test-module" where we would unpack the artifact that we want to test so that we can weave its classes without affecting the "real" production jar. 我们的想法是有一个额外的“测试模块”,我们将解压缩我们想要测试的工件,这样我们就可以编织它的类而不会影响“真正的”生产jar。
To do so, declare a dependency on the module under test and use dependency:unpack
to unpack the classes into target/classes
before invoking the AspectJ plugin to weave the "main" classes. 为此,请声明对正在测试的模块的依赖关系并使用
dependency:unpack
在调用AspectJ插件编织“主”类之前,将unpack解压缩到target/classes
类中。
Based on the sample provided in AspectJ compiler Maven Plugin - Usage something like the following should work: 基于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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.