AspectJ与Maven(仍然)无法正常工作

时间:2017-06-12 05:22:21

标签: java eclipse maven aspectj

我正在尝试在Eclipse Neon中使用maven创建一个示例aspectj项目。但是,方面不是编织/工作(参见下面的输出)。我尝试了很多原因和解决方案,但都没有成功(参见下面的相关问题)。任何帮助/见解将非常感谢。谢谢!

使用的技术

  • Java jdk-1.8
  • Eclipse(Java EE)Neon 3
  • Apache Maven 3.5.0
  • junit-4.5(包含在maven中)
  • aspectjrt-1.8.9(包含在maven中)

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

目录

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

相关问题我尝试过但失败了

2 个答案:

答案 0 :(得分:1)

我不确定为什么但是在包含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>

答案 1 :(得分:-1)

很奇怪。我正在使用Java 1.8.0_120,aspectj-maven-plugin 1.11,aspectjrt 1.9.2

使用mvn 3.0.5可以正常工作 高于(3.3.9或以上3.6.0)的每个mvn均无效。对我来说,这似乎是一个专家问题。