我为AspectJ使用哪个maven插件?

Kri*_*fer 19 java plugins maven-2 netbeans aspectj

我正在尝试使用java 6.0将aspectj添加到maven项目中.浏览我发现2个maven插件,其中没有一个按我期望的方式工作.

第一个http://mojo.codehaus.org/aspectj-maven-plugin最初没有通过netbeans工作,因为我无法获得编译5.0或更高版本的源代码(它抱怨注释等)从命令尝试后工作和比较执行的命令的行似乎它的mavens安装目标与插件和java 5+代码不兼容,而编译目标工作正常.虽然有可能解决这个问题,但这很烦人并且让我想到了一个问题:aspectj-maven-plugin还在开发中吗?我还应该用吗?

第二个是apaches自己的,它似乎更活跃,更有可能工作.但是我找不到任何完整的例子,我无法运行它.我一直从maven得到一个例外:

java.lang.IllegalStateException: The plugin descriptor for the plugin Plugin [maven:maven-aspectj-plugin] was not found. Please verify that the plugin JAR /home/kristofer/.m2/repository/maven/maven-aspectj-plugin/4.0/maven-aspectj-plugin-4.0.jar is intact.
Run Code Online (Sandbox Code Playgroud)

jar文件是完整的,并且我使用的插件版本也无关紧要,它总是抛出相同的异常.关于问题可能是什么的任何想法?

简而言之,哪个插件以及我应该如何使用它?

谢谢

Pas*_*ent 17

这是一个适合我的设置(使用下面记录的aspectj-maven-plugin).

项目结构如下:

$ tree .
.
??? pom.xml
??? src
    ??? main
    ?   ??? java
    ?       ??? com
    ?           ??? stackoverflow
    ?               ??? Q3651690
    ?                   ??? App.java
    ?                   ??? DontWriteToTheConsole.aj
    ??? test
        ??? java
            ??? com
                ??? stackoverflow
                    ??? Q3651690
                        ??? AppTest.java

通过以下小演示方面:

public aspect DontWriteToTheConsole {

    pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);

    declare error
      : sysOutOrErrAccess()
      : "Don't write to the console";

}
Run Code Online (Sandbox Code Playgroud)

并且pom.xml配置如下:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.Q3651690</groupId>
  <artifactId>Q3651690</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Q3651690</name>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.7</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal><!-- to weave all your main classes -->
              <goal>test-compile</goal><!-- to weave all your test classes -->
            </goals>
          </execution>
        </executions>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

关键部分是:

  • 为1.6源级别配置maven-compiler-plugin(这是使用完成的properties)
  • 为1.6源代码级别配置aspectj-maven-plugin(我在这里重用了properties用于配置maven-compiler-plugin的东西)

第二步似乎是多余的,但是,事情就是如此.

这样,我就能够使用注释等编写代码:

$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3651690
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q3651690/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/stackoverflow/Q3651690/target/classes
[INFO] [aspectj:compile {execution: default}]
[ERROR] Don't write to the console
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Compiler errors:
error at System.out.println( "Hello World!" );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/App.java:11:0::0 Don't write to the console
    see also: /home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/DontWriteToTheConsole.aj:8::0
...