aspectjaspectj-maven-plugin

Basic implementation of AspectJ not working


I have written a simple module in IntelliJ (Java 11 + Maven 3.6) to test AspectJ functionality. However, the Aspects are not working as expected. Can someone tell what I am doing wrong? Here is my code:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.aspect.test</groupId>
    <artifactId>aspect-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <aspectj.version>1.9.7</aspectj.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>11</source>
                    <target>11</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>11</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>

</project>

Greeter.java

public class Greeter {
    public void greet(String name) {
        System.out.println("Hello, " + name);
    }
}

LoggingAspect.java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

    @Before("execution(* *(..))")
    public void log(JoinPoint joinPoint) {
        System.out.println("LoggingAspect called..!");
    }
}

App.java

public class App {
    public static void main( String[] args ) {
        var greeter = new Greeter();
        String name = "Test";
        if(args.length > 0) {
            name = args[0];
        }
        greeter.greet(name);
    }
}

I am using IntelliJ run configuration to execute the main method in App.java.

My execution does not print the println statement in the Aspect,


Solution

  • First, I had to fix the problems in your sample:

    Now, the application compiles both in Maven and in IntelliJ IDEA. When running it, the aspect also gets applied. If it does not for you, either you forgot to install and activate AspectJ support in IDEA Ultimate or you use IDEA Community, which does not allow the AspectJ plugin to run, because it is a premium feature.

    But even in IDEA Community, the Maven build should work. You could configure your run configuration to delegate the build to Maven instead of using IDEA's builder.