views:

36

answers:

2

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.

+1  A: 

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.

Pascal Thivent
Pascal, thank you for answer. However I'm not sure I got your point. Let me try to clarify.
iYasha
You proposing to put production code into separate Maven module and don't put tests there at all. Then main project will contain tests only. At the process-test-classes (I assume this is the phase you meant) it will unpack production classes from separate module to test classes folder and weave aspects into them. Then it will run tests. Is this understanding correct?
iYasha
@iYasha I tried to clarify. Let me know if it's more clear.
Pascal Thivent
Pascal, I think now I understand. I will try suggested approach and let you know results.
iYasha
A: 

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>
Michael Rutherfurd
This will weave test classes, which is not what the OP wants. The OP wants to weave "main" classes, but only for the purpose of testing (the tracing aspect shouldn't be included in the packaged classes).
Pascal Thivent
Pascal, you're exactly right. Problem with AspectJ maven plugin is that it weaves production aspects to production classes and test aspects to test classes but I need to "mix" them.
iYasha