Woven production classes with AspectJ aspects in Maven to run tests only

I have a Maven build project and need to add some basic performance traces to the methods. I decided to use AspectJ for this. The main requirement is to drag the trace aspect into production classes , but only for the unit test execution stage .

I managed to set up weaving in Maven, however after running the tests the same production classes with the applied aspect go into a packaged war.

The case looks quite often, but I could not find a solution for it on the Internet.

+3
source share
4

, Maven Dependency Plugin, " " generate-test-sources, , , .


, . :

.
|-- pom.xml
`-- some-module    // this is the module that we want to weave 
    |-- pom.xml    // but only for testing purpose
    `-- ...

, - - :

.
|-- 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
    `-- ...

, " ", , , , "" .

, dependency:unpack, target/classes, AspectJ, "" .

0

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>

. http://mojo.codehaus.org/aspectj-maven-plugin/test-compile-mojo.html

+2

. , , - ( ), .

, , , , . :

@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;
    }
}

, , mehtod TweakSystemAspects.advanceTime(), . , 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>

, 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>
0
source

Based on the sample provided in AspectJ by the Maven Plugin compiler - Usage , 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>
-1
source

Source: https://habr.com/ru/post/1766781/


All Articles