Reputation: 2941
I have a project with Maven build and need to add some basic performance tracing to methods. I decided to use AspectJ for this. 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.
The case looks like pretty common nevertheless I wasn't able to find solution for it in web.
Upvotes: 3
Views: 3129
Reputation: 574
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. 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):
<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.
<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>
Upvotes: 1
Reputation: 21
You can put your aspects in the test directory and set the weaveMainSourceFolder flag to true in the test-compile configuration
<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
Upvotes: 2
Reputation: 570545
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.
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.
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.
Upvotes: 0
Reputation: 14065
Based on the sample provided in AspectJ compiler Maven Plugin - Usage something like the following should work:
<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>
Upvotes: -1