Running JDK8 for aspectj - java

I am trying to run aspectj-maven plugin with JDK8. But it is giving errors like
"The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files"
Any help on how to resolve, or if the aspectj-maven-plugin supports JDK8. I am using 1.6 version of aspectj--maven-plugin.

I had to achieve the same and I drove crazy trying to figure out this, fortunately I could solve it and here I give you what I did:
To use aspectj-maven-plugin with Java 8 you need 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>1.8.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
And the most important thing I've struggled was that you need to install the aspectj-maven-plugin 1.7 jar manually into your pom.xml since this jar aren't on maven repo yet.
You can get it from Haus Jira (look at the Attachment section):
https://jira.codehaus.org/browse/MASPECTJ-131
Btw, once you download it an copy it to your repo you need to create your own aspectj-maven-plugin-1.7-SNAPSHOT.pom file within the corresponding directory. You can copy it from version 1.6 BUT ensure you 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.

You don't need 1.7-SNAPSHOT for that. I have this snippent in POM and everything works:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>

Another way to solve this problem is to downgrade JDK level to 1.7. JDK 1.7 works well with aspectj-maven-plugin of versions 1.4 - 1.6.
Here is a screenshot showing how to change JDK level in IntelliJ IDEA project:
Set Project SDK to 1.7. See documentation for more details.

Related

AspectJ weaving from dependency not applying to project

I have a Java 8 Maven project that defines a custom annotation and an aspect. When running test code in that project itself, it is applying the aspect to the annotated classes. I am then packaging and installing project.
I then bring in that dependency into a new project (non-Spring). The new project is then not having the aspect applied to it's classes, though it does bring in the new annotation.
How do I have a single JAR to define an annotation and aspect and have it applied to all of my projects with Maven?
You need to specify your aspect project dependency as an aspect library in your aspectj-maven-plugin configuration in your pom.xml. Let's suppose your aspect module has the groupid:artifactid groupid:aspect-module. Your pom.xml should look similar to this:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that I'm switching off the maven-compiler-plugin because they tend do overwrite each other's output with the aspectj-maven-plugin, and the AspectJ compiler should be able to compile normal java files and weave them in the same step anyway, so using the maven-compiler-plugin is redundant. If you are using Eclipse + AJDT, this maven configuration will much better reflect what happens in your IDE while you're developing.

Aspectj bad version number warning in maven build

I am getting a warning during a maven build, that I would like to fix.
The warning generated during a maven build:
[INFO] --- aspectj-maven-plugin:1.4:compile (default) # core ---
[WARNING] bad version number found in C:\Users\DR25687.m2\repository\org\aspectj\aspectjrt\1.7.1\aspectjrt-1.7.1.jar expected 1.6.11 found 1.7.1
The pom file
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.7</source>
<target>1.7</target>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<complianceLevel>1.7</complianceLevel>
Parent POM
<org.aspectj.version>1.7.1</org.aspectj.version>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj.version}</version>
<scope>runtime</scope>
</dependency>
Set up aspectjrt version in plugin configuration
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${version-plugin-aspectj}</version>
<configuration>
<source>${targetJdk}</source>
<target>${targetJdk}</target>
<verbose>true</verbose>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- Ensure aspectj tools version used by compiler is the same version used as dependency. Avoids warning
-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.12</version>
</dependency>
</dependencies>
</plugin>
See https://bugs.eclipse.org/bugs/show_bug.cgi?id=368190
you have 1.7.1 in POM. but your local maven repository is having an older version. Try a mvn clean install.
it will download the 1.7.1 version jar.

Migration from java 7 to java 8 [duplicate]

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

Migrate from java 7 to Java 8 [duplicate]

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

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

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

Categories

Resources