0

I have an aspect that I want to use in my test-classes. I don't want to add it to the main jar, as it would pull in test libraries like junit and mockito. While there's a configuration setting to add an aspectLibrary, it always adds the main jar, there's no way to specify the test-jar.

My aspectj plugin looks like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <aspectLibraries>
        <aspectLibrary>
            <groupId>aspect.test</groupId>
            <artifactId>general</artifactId>
            <type>jar</type>
        </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
        <phase>process-sources</phase>
        <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
        </goals>
        </execution>
    </executions>
</plugin>

I actually want to specify test-jar but that doesn't seem possible. Without the it defaults to the jar (obviously).

I also might have to configure aspectj-maven-plugin for the compile and test-compile goal... but first I need to know how to specify the test-jar. Any suggestions are welcome.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Mark Boon
  • 113
  • 2
  • 9
  • As I said in the [other question](http://stackoverflow.com/a/41650046/1082681) where you asked first: You need to add the library as a normal `` before you can refer to it as an ``. And maybe `test-jar` would make sense for a test JAR? – kriegaex Mar 15 '17 at 11:30
  • Maybe you could also explain why your aspects are in a test JAR. Are they not used in production? Do you really have aspects which are only being applied during test? Then maybe you are testing a different setup than in real life, which might be dangerous. The only scenario I can imagine where it would make sense is if the aspects were only woven into your tests (maybe as some kind of helpers for test execution), but **never** into production code. If you modify production code only during tests, you falsify its behaviour. – kriegaex Mar 15 '17 at 11:34
  • Yes, they are only applied on test code. I have a few mixin aspects that adds functionality to a test-class to capture logging events and provide a way for testing whether certain logging events happened. Since the aspect has references to a mocking framework, I don't want this aspect to be in the main jar. test-jar is not supported under . – Mark Boon Mar 15 '17 at 16:20

1 Answers1

0

Please read the Maven JAR Plugin documentation, chapter How to create a jar containing test classes. There are two options listed:

  • the easy way: using type "test-jar" (will not work here)
  • the preferred way: creating a normal JAR containing only test classes, then importing it with scope "test"

We will choose the preferred way because it solves your problem. So basically you do the following:

  • Create a separate module for test helper aspects/classes, put everything under src/main/java, not src/test/java. AspectJ Maven plugin should have an execution with <goal>compile</goal> for that module.
  • Add that module as a test-scoped dependency wherever you need the test aspects
  • Refer to the module as an <acpectLibrary> from AspectJ Maven plugin and also be careful to only use <goal>test-compile</goal> in your plugin execution for that module so as to avoid to have the aspects woven into production code or to get error messages because the dependency has test scope and is unavailable for normal compile.

Because I do not want to fully quote 3 POMs and several classes here, I have created a little GitHub sample project for you. Just clone it, inspect all the files and their respective locations and - be happy. ;-)

kriegaex
  • 63,017
  • 15
  • 111
  • 202