Spring + AspectJ weaving for java 8 using aspectj-maven-plugin
Asked Answered
P

1

18

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.

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 here that plugin 7 adds java 8 support but couldn't find a way to use it.

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.

Do you know how to run the weaver for spring+aspectj running on Java 8?

Parsimonious answered 8/8, 2014 at 17:49 Comment(3)
@kriegaex thanks for ask. I could resolve the issue that was really hard. I posted the answer maybe can help someone else.Parsimonious
The two links in the question are broken also. Since you have most of the data on github, it would be nice to fix 'em. :)Haith
@VictorStafusa obrigado my friend for the comment. Unfortunately, CodeHaus hasn't migrated aspectj plugin documentation yet. I'll keep checking this to update it. Thank you for bringing up this.Parsimonious
P
36

Solution before the official release prior to Sep 2015

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

So, the maven plugin configuration needs to be:

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

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

Update: So, the jar file can be downloaded from Haus Jira link (look at the Attachment section). If Haus is not available anymore you can download it from my github:

https://github.com/fedepia/aspectj-maven-plugin-1.7

After download it and copy it to my local repo I needed to create my own aspectj-maven-plugin-1.7-SNAPSHOT.pom file within the directory:

.m2\repository\org\codehaus\mojo\aspectj-maven-plugin\1.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:

<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)

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:

@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

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:

http://www.mojohaus.org/aspectj-maven-plugin/usage.html

Here is the link to maven repository:

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>
Parsimonious answered 11/8, 2014 at 17:13 Comment(11)
+1, saved me some time. You've got a typo there on your spring-aop version, though.Iodism
Thanks @drewmoore for the +1. Btw, can you point me what the typo is?Parsimonious
I don't think there was a spring-aop version 1.8.1, and if there was it'd be decidedly obsolete today... (current version is 4.1.0)Iodism
@drewmoore perfect! Yes, you are totally right. I'll fix it nowParsimonious
Could you give more detail about your application context configuration? And how did you configure your @Aspect? Many thanks!Attainder
@XtremeBiker thanks! If you find it useful feel free to upvote :)Parsimonious
@Fede Since codehaus closed, the link to MASPECTJ-131 is now dead. I am trying to find a new link to the issue, but no luck until now.Haith
@VictorStafusa nice catch up. I have the files, I'm uploading to github them :). Thanks for point this.Parsimonious
@nterry glad to help, this was insanely annoying to figure it out, so glad to hear this is helpingParsimonious
pastebin.com/ejytis3G I am getting workflow execution failed for a simple workflow using an asynchronous method (example and error message in pastebin). Any help is appreciated.Meunier
If you've come here with @KumarDeepak 's same problem, then check out this repository for your salvation. github.com/pedropaulovc/aws-flow-maven-eclipse-samplesTanishatanitansy

© 2022 - 2024 — McMap. All rights reserved.