java 我为 AspectJ 使用哪个 Maven 插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3651690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:50:56  来源:igfitidea点击:

Which maven plugin do I use for AspectJ?

javamaven-2pluginsnetbeansaspectj

提问by Kristofer

I am trying to add aspectj to a maven project using java 6.0. Browsing around I found 2 maven plugins, none of which works the way I would expect.

我正在尝试使用 java 6.0 将 aspectj 添加到 Maven 项目中。浏览我发现了 2 个 maven 插件,没有一个像我期望的那样工作。

The first one http://mojo.codehaus.org/aspectj-maven-plugindid not work at first through netbeans because I could not get the code to compile 5.0 or later source (it complained about annotations etc.) After trying from command line which worked and comparing the commands executed it seems that its mavens install goal that is not compatible with the plugin and java 5+ code while the compile goal works fine. Although it may be possible to work around this it is annoying and brings me to the question: is the aspectj-maven-plugin still being developed? Should I still use it?

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

The second one is apaches's own which seems more active and more likely to work. I can however not find any complete examples and I am unable to run it. I keep getting an exception from maven:

第二个是 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.

The jar file is there, intact and it also doesn't matter which version of the plugin I use, it always throws the same exception. Any ideas on what the problem might be?

jar 文件在那里,完好无损,而且我使用哪个版本的插件也无关紧要,它总是抛出相同的异常。关于问题可能是什么的任何想法?

In short, which plugin and how should I use it?

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

Thanks

谢谢

采纳答案by Pascal Thivent

Here is a setup that works for me (using the under documented aspectj-maven-plugin).

这是一个对我有用的设置(使用记录不足的aspectj-maven-plugin)。

The project structure is as follow:

项目结构如下:

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

With the following little demo aspect:

使用以下小演示方面:

public aspect DontWriteToTheConsole {

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

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

}

And the pom.xml is configured like this:

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>

The key parts are:

关键部分是:

  • to configure the maven-compiler-plugin for 1.6 source level (this is done using the properties)
  • to configure the aspectj-maven-plugin for 1.6 source level (and I reused the propertiesused to configure the maven-compiler-plugin here)
  • 为 1.6 源代码级别配置 maven-compiler-plugin(这是使用 完成的properties
  • 为 1.6 源级别配置 aspectj-maven-plugin(我在这里重用了properties用于配置 maven-compiler-plugin 的)

The second step seems redundant but, well, that's how things are.

第二步似乎是多余的,但是,事情就是这样。

This way, I was able to weave code using annotations, etc:

通过这种方式,我能够使用注释等编织代码:

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

回答by Colin Hebert

You can use the Maven Compiler plugin and change the compiler to use AspectJ.

您可以使用 Maven Compiler 插件并将编译器更改为使用 AspectJ。

The configuration looks like this :

配置如下所示:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <compilerId>aspectj</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-aspectj</artifactId>
            <version>1.6</version>
        </dependency>
    </dependencies>
</plugin>


Resources :

资源 :

On the same topic :

在同一主题上:

回答by Ichthyo

we're using the aspectj-maven-plugin for building several large production grade J2EE systems. Lately, the development on that plugin doesn't seem to be overly active. The last relase has been last winter, and there are some serious problems with "AspectLib" and "WeaveDependencies", which got reported several times (even with attached bugfixes), without any responses from upstream.

我们正在使用 aspectj-maven-plugin 来构建几个大型的生产级 J2EE 系统。最近,该插件的开发似乎并不太活跃。最后一个版本是去年冬天,“AspectLib”和“WeaveDependencies”存在一些严重的问题,被多次报告(即使附带错误修正),上游没有任何回应。

But anyway, the basic functionality is working, and this plugin supports lots of configuration needed in real world projects.

但无论如何,基本功能都在工作,这个插件支持现实世界项目中需要的大量配置。

Pascal Thivent showed in his (very good) response above how to configure the plugin with a special dependency section. You can use this trick also to configure the actual AspectJ version used for compilation, as the version used by default by this plugin is somewhat dated....

Pascal Thivent 在上面的(非常好的)回复中展示了如何使用特殊的依赖项部分配置插件。您也可以使用此技巧来配置用于编译实际 AspectJ 版本,因为此插件默认使用的版本有些过时....

<project xmlns=....

<properties>
    <aspectjVer>1.6.9</aspectjVer>
    ....
    ....
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
....

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.3</version>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectjVer}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectjVer}</version>
                </dependency>
            </dependencies>
            <configuration>
       ....
</build>
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectjVer}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId><!-- only needed if you use Spring-AOP -->
        <version>${aspectjVer}</version>
    </dependency>
    ....
    ....

Note the factthat the plugin has an classpath environment which is independent from your project's classpath. Thus we have to add the AspectJ-Runtime explicitly to the project dependencies.

请注意,插件具有独立于项目类路径的类路径环境。因此,我们必须将 AspectJ-Runtime 显式添加到项目依赖项中。