在Maven项目中使用AspectJ注释:编织不起作用 [英] Using AspectJ annotations in maven project: weaving is not working

查看:207
本文介绍了在Maven项目中使用AspectJ注释:编织不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个不使用Spring的简单项目中使用AspectJ,虽然我已经看到了类似的问题,并且我的代码似乎是正确的,但我不明白为什么它不起作用.我正在使用Eclipse Oxygen 4.7.3(不使用AJDT工具),JDK 7,maven 3.5.2,我的代码如下:

I am trying to use AspectJ in a simple project without using Spring, and while I have seen similar questions and my code seems to be correct, I don't understand why it's not working. I'm using Eclipse Oxygen 4.7.3 (not using AJDT tools), JDK 7, maven 3.5.2, and my code is as follows:

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</groupId>
  <artifactId>aspect-tutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.8</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>

        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

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

MainApp.java

package com.pkg;

public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HelloWorld a = new HelloWorld();
        a.printHello();
    }
}

HelloWorld.java

package com.pkg;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Print Hello...");
   }

}

TestAspect.java

package com.pkg;

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


@Aspect
public class TestAspect {       

    @Before("execution(* com.pkg.HelloWorld.printHello(..))")
    public void testBefore2(){
        System.out.println("Yeeha");
    }


}

运行mvn全新安装成功,但是输出仅打印"Print Hello ..."部分.我应该使用其他方法吗? (也许可以改用.aj文件,或尝试进行加载时编织)任何帮助.

Running mvn clean install is successful, but the output only prints the "Print Hello..." part. Should I use a different approach? (Maybe use a .aj file instead, or try load-time-weaving) Any help appreciated.

推荐答案

问题是AspectJ Maven和Maven编译器的配置.我的AspectJ的POM通常看起来与您的POM有所不同(还有一些设置),但是为了使它正常工作,以下是您的POM:

The problem is the configuration of both AspectJ Maven and Maven Compiler. My POMs for AspectJ usually look a bit different than yours (a few more settings), but here is yours with minimal changes in order to make it work:

<?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</groupId>
  <artifactId>aspect-tutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
  </properties>

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

  <build>
    <plugins>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.11</version>
        <configuration>
          <complianceLevel>1.7</complianceLevel>
          <source>1.7</source>
          <target>1.7</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.7</source>
          <target>1.7</target>
          <!-- IMPORTANT -->
          <useIncrementalCompilation>false</useIncrementalCompilation>
        </configuration>
      </plugin>

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

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

看看我如何将Maven编译器的增量编译设置为false?这是由于旧的错误(仍未修复)实际上会使开关反转,因此,为了使增量编译工作有效,您必须停用"它.很奇怪.

See how I set incremental compilation to false for Maven compiler? This is due to an old bug (still unfixed) which actually inverts the switch, so in order to make incremental compilation work you have to "deactivate" it. Very weird.

您还需要为AspectJ Maven的process-sources阶段定义执行.

You also need to define executions for the process-sources phase for AspectJ Maven.

此外,我升级到AspectJ Maven 1.11,因此也升级到AspectJ运行时1.8.13.

Besides, I upgraded to AspectJ Maven 1.11 and thus also to AspectJ runtime 1.8.13.

我还添加了Maven Exec插件,以轻松证明它现在可以正常工作.只需调用mvn clean compile exec:java并检查输出即可:

I also added Maven Exec plugin in order to easily prove that it is working now. Just call mvn clean compile exec:java and check the output:

(...)
[INFO] --- aspectj-maven-plugin:1.11:compile (default) @ aspect-tutorial ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ aspect-tutorial ---
[INFO] Nothing to compile - all classes are up to date
(...)
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ aspect-tutorial ---
Yeeha
Print Hello...
(...)

否则,我会支持Nándor所说的话:如果您还想从IDE中运行增强了方面的Java代码,请确保为Eclipse或IDEA使用AspectJ插件.

Otherwise I support what Nándor said: Make sure you use AspectJ plugins for Eclipse or IDEA if you also want to run your aspect-enhanced Java code from an IDE.

这篇关于在Maven项目中使用AspectJ注释:编织不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆