Search code examples
mavenaopaspectjaspectj-maven-plugin

Using AspectJ in maven project: weaving is not working


I am trying to reproduce the example in Using AspectJ annotations in maven project: weaving is not working with the answer given there. In articular, now I am using Eclipse Photon, JDK 1.8 but with no success.

My Final POM.XML is:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>aspect-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.5</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>com.pkg.MainApp</mainClass>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
</build></project>

I have tried to run both with Eclipse (MainApp --> Run as...) and with Maven (pom.xml -->Run as --> Maven Build), but no weaving is taken place. Any idea?

Thanks a lot! Bea


Solution

  • I forked your repo and fixed your Maven POM. You actually made a Maven beginner's mistake by pre-configuring plugins in the <pluginManagement> section but never actually declaring usage of the pre-configured plugins in the <plugins> section. In that case of course Maven does not use the AspectJ Maven plugin and thus does not weave any aspects either.

    I sent you a pull request which you can just accept in order to make this work. There are several other things in your Maven settings which could use improvement, e.g.

    • if you really want to use AspectJ 1.9.5 and not the preconfigured 1.8.13 used in AspectJ Maven, there is a way to achieve that, but not the way you did it. Feel free to ask for more details if this is an issue.

    • if you want to use a JDK newer than 8 in order to run your Maven build. In that case you need to switch to a fork of AspectJ Maven because version 1.11 does not work with JDK 9+. I can also give you more details there if necessary. But with JDK 8 you should be fine.