使用aspectj-maven-plugin为java 8编织Spring + AspectJ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25209339/
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
Spring + AspectJ weaving for java 8 using aspectj-maven-plugin
提问by Federico Piazza
I'm migrating my project from java 7 to java 8 and the problem I have is related to aspectj weaving using aspectj-maven-plugin
.
我正在将我的项目从 java 7 迁移到 java 8,我遇到的问题与使用aspectj-maven-plugin
.
I could configure successfuly the weaving using this plugin running on Java 6 and 7 according to Haus documentation. But the problem is that I haven't found any way to use (and find) plugin version 7 that supports java 8. I saw herethat plugin 7 adds java 8 support but couldn't find a way to use it.
根据Haus 文档,我可以使用在 Java 6 和 7 上运行的这个插件成功配置编织。但问题是我还没有找到任何方法来使用(和找到)支持 java 8 的插件版本 7。我在这里看到插件 7 添加了 java 8 支持,但找不到使用它的方法。
This is the configuration plugin I need:
这是我需要的配置插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version> <!-- AspectJ weaver plugin 7 is for java 8 (version 1.6 is for java 7) -->
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
I confirmed that above code using version 1.6 works fine for Java 7, but had no luck trying to use version 1.7.
我确认使用 1.6 版的上述代码适用于 Java 7,但尝试使用 1.7 版时没有运气。
Do you know how to run the weaver for spring+aspectj running on Java 8?
你知道如何在Java 8上运行spring+aspectj的weaver吗?
采纳答案by Federico Piazza
Solution before the official release prior to Sep 2015
2015年9月之前正式发布之前的解决方案
After many headaches and many hours struggling against this, fortunately I could solve this problem. Here is what I did:
经过多次头痛和数小时的挣扎后,幸运的是我可以解决这个问题。这是我所做的:
To use aspectj-maven-plugin
with Java 8 I could configure version aspectj-maven-plugin 1.7 (Note that aspectj-maven-plugin 1.6 works for Java 7).
要aspectj-maven-plugin
与 Java 8 一起使用,我可以配置版本 aspectj-maven-plugin 1.7(注意 aspectj-maven-plugin 1.6 适用于 Java 7)。
So, the maven plugin configuration needs to be:
所以,maven插件配置需要:
<!-- AspectJ configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7-SNAPSHOT</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
By the way, the aspectJ jars needed are:
顺便说一下,所需的aspectJ jars是:
<!-- Spring AOP + AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
The most important thing I've struggled was that to install the aspectj-maven-plugin 1.7 jar I had to do it manually since these jar/pom files aren't on maven repo yet.
我遇到的最重要的事情是安装 aspectj-maven-plugin 1.7 jar 我必须手动完成,因为这些 jar/pom 文件还没有在 maven repo 上。
Update:So, the jar file can be downloaded from Haus Jiralink (look at the Attachment section). If Haus is not available anymore you can download it from my github:
更新:因此,可以从Haus Jira链接下载 jar 文件(查看附件部分)。如果 Haus 不再可用,您可以从我的 github 下载:
https://github.com/fedepia/aspectj-maven-plugin-1.7
https://github.com/fedepia/aspectj-maven-plugin-1.7
After download it and copy it to my local repo I needed to create my ownaspectj-maven-plugin-1.7-SNAPSHOT.pom
file within the directory:
下载并将其复制到我的本地存储库后,我需要在目录中创建自己的aspectj-maven-plugin-1.7-SNAPSHOT.pom
文件:
.m2\repository\org\codehaus\mojo\aspectj-maven-plugin.7-SNAPSHOT\aspectj-maven-plugin-1.7-SNAPSHOT.pom
I based on a copy from version 1.6 but had to modify the following content:
我基于 1.6 版的副本,但不得不修改以下内容:
<version>1.7-SNAPSHOT</version>
<properties>
<aspectjVersion>1.8.1</aspectjVersion>
<mavenVersion>2.2.1</mavenVersion>
<changesPluginVersion>2.9</changesPluginVersion>
</properties>
That's all here you go, hope to help.
就这么多了,希望能帮到你。
Update:(adding more details as Xtreme Biker asked in the comments)
更新:(在 Xtreme Biker 在评论中询问时添加更多细节)
In my context configuration I have:
在我的上下文配置中,我有:
<aop:aspectj-autoproxy />
<bean id="notificationAspect" class="com.integration.core.aspect.NotificationAspect" factory-method="aspectOf" scope="singleton"></bean>
For my java aspect I use:
对于我的 java 方面,我使用:
@Aspect
public class NotificationAspect
{
...
@AfterThrowing(pointcut="@annotation(com.integration.core.meta.NotifyOnFailure)", throwing="ex")
public void executeOnException(JoinPoint joinPoint, ExternalApiExecutionException ex) throws Throwable
{
...
Finally official plugin released since Sep 2015
终于在 2015 年 9 月发布了官方插件
This is an update to the answer with the official plugin release. In order to use Java 8 with AspectJ, the official aspectj maven plugin can be found on this link:
这是对官方插件发布的答案的更新。为了在 AspectJ 中使用 Java 8,可以在此链接上找到官方的 aspectj maven 插件:
http://www.mojohaus.org/aspectj-maven-plugin/usage.html
http://www.mojohaus.org/aspectj-maven-plugin/usage.html
Here is the link to maven repository:
这是 Maven 存储库的链接:
http://mvnrepository.com/artifact/org.codehaus.mojo/aspectj-maven-plugin/1.8
http://mvnrepository.com/artifact/org.codehaus.mojo/aspectj-maven-plugin/1.8
As the documentation stated the code to use it is:
正如文档所述,使用它的代码是:
<project>
...
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
<build>
...
</project>