AspectJ 与 Maven(仍然)不工作

AspectJ with Maven (still) not working

我正在尝试在 Eclipse Neon 中使用 aspectj 创建示例 maven 项目。但是,方面不是 weaving/working(请参阅下面的输出)。我尝试调查许多原因和解决方案,但 none 有效(请参阅下面的相关问题)。任何 help/insights 将不胜感激。谢谢!

使用的技术:

(我还在我的 Eclipse 安装中为一个单独的项目安装了 AJDT 插件——不确定这是否会抑制 aspectj 的 maven 存储库版本)

目录:

$ 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>

输出

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

相关问题我试过失败:

我不确定为什么,但是在包含 aspectjweaver-1.8.9 依赖项、更新到 junit-4.12 并更改方面代码后,代码工作正常。 这是更新的方面代码 (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");
    }
}

这是新的 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>

奇怪。我正在使用 java 1.8.0_120、aspectj-maven-plugin 1.11、aspectjrt 1.9.2

使用 mvn 3.0.5 它可以正常工作 上面的每个 mvn(3.3.9 或 3.6.0)都不起作用。对我来说,这看起来像是一个行家问题。