AspectJ with Maven (still) not working

2019-08-06 14:44发布

问题:

I am trying to create a sample maven project with aspectj in Eclipse Neon. However, aspects are not weaving/working (see output below). I tried looking into lots of causes and solutions but none worked (see related questions below). Any help/insights would be very much appreciated. Thanks!

Technologies used:

  • Java jdk-1.8
  • Eclipse (Java EE) Neon 3
  • Apache Maven 3.5.0
  • junit-4.5 (included through maven)
  • aspectjrt-1.8.9 (included through maven)

(I also installed the AJDT plugin to my Eclipse installation for a separate project -- not sure if this inhibits the maven repository version of aspectj)

Directory:

$ tree .
.
├── pom.xml
└── src
    ├── main
    |   └── java
    |       └── com
    |           └── hellomaven
    |               └── quickstart
    |                   ├── App.java
    |                   └── AppAspect.java
    └── test
        └── java
            └── com
                └── hellomaven
                    └── quickstart
                        └── AppTest.java

App.java

package com.hellomaven.quickstart;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        say("billy", "Nice to meet you");
        Dog d = new Dog();
        d.bark(2);
    }

    public static void say(String name, String word) {
        System.out.println(name.toUpperCase() + " says " + word.toLowerCase() + "!");
    }
}

class Dog {
    Dog() {
        System.out.println("..Dog init code..");
    }

    public void bark(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print("bark ");
        }
        System.out.println("!");
    }

}

AppAspect.java

package com.hellomaven.quickstart;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AppAspect {
    @Before("execution(public static * *(..))")
    public void testAspectBefore() {
        System.out.println("Before Okay");
    }

    @Pointcut("execution(* *(..))")
    public void testAspectPointcut() {
        System.out.println("Pointcut Okay");
    }

    @After("execution(* *(..))")
    public void testAspectAfter() {
        System.out.println("After Okay");
    }

    @Around("execution(* *(..))")
    public void testAspectAround() {
        System.out.println("Around Okay");
    }
}

pom.xml

<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.hellomaven</groupId>
    <artifactId>quickstart</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <name>quickstart</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java.version>1.8</java.version>
        <aspectj.version>1.8.9</aspectj.version>
        <junit.version>4.5</junit.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.9</version>
                    <configuration>
                        <showWeaveInfo>true</showWeaveInfo>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.version}</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>aspectjrt</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3</version>
                    <configuration>
                        <mainClass>com.hellomaven.quickstart</mainClass>
                    </configuration>
                </plugin>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

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

output

<!-- language: lang-none -->
Hello World!
BILLY says nice to meet you!
..Dog init code..
bark bark !

Related questions I have tried and failed:

  • AspectJ in Maven project not working/weaving
  • Not able to integrate AspectJ with Maven
  • How to build AspectJ project using Maven
  • Maven + AspectJ - All steps to configure it
  • Maven: compile AspectJ project containing Java 1.6 source

回答1:

I am not sure why but after including an aspectjweaver-1.8.9 dependency, updating to junit-4.12 and changing the aspect code, the code works fine. Here is the updated aspect code (AppAspect.java):

package com.hellomaven.quickstart;

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

@Aspect
public class AppAspect {

    @Pointcut("execution(* *(..))")
    public void defineEntryPoint() {
    }

    @Before("defineEntryPoint()")
    public void aaa(JoinPoint joinPoint) {
        System.out.println("aspect before");
    }

    @After("defineEntryPoint()")
    public void bbb(JoinPoint joinPoint) {
        System.out.println("aspect after");
    }
}

Here is the new dependencies:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <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>
</dependencies>


回答2:

Weird. I am using java 1.8.0_120, aspectj-maven-plugin 1.11, aspectjrt 1.9.2

With mvn 3.0.5 it is working Every mvn above (3.3.9 oder 3.6.0) does not work. for me, it looks like an maven issue.