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.
I would do that in a dedicated module, use the Maven Dependency Plugin to unpack the artifact “under test” during the
generate-test-sourcesphase, 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:unpackto unpack the classes intotarget/classesbefore invoking the AspectJ plugin to weave the “main” classes.