11

I want to upgrade my project to spring 4, however, I get a missing dependency for aspectweaver-1.8.0.M1.jar. When I take a look at the dependency hierarchy, I see that this file is needed by spring-aspects-4.0.0, which has the following in its own pom.xml

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.0.M1</version>
  <scope>compile</scope>
</dependency>

After checking maven repository, I wasn't able to find this artifact since the latest stable version of aspectweaver is 1.7.4.

Now I can't understand two things:

  1. Since the scope of this dependency is compile why does my application need it ? I don't want to compile the spring-aspects.jar !!

  2. Why is spring-aspects-4.0.0.jar (which is stable) using a non-stable version of a component ? Wouldn't that make spring-aspects-4.0.0 also non-stable ?

Thanks

Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • 2
    You may need to add the appropriate milestone repository; I don't know which ones it is. Good questions about the `spring-aspects` dependency, and I'd file a bug against it. – chrylis -cautiouslyoptimistic- Dec 20 '13 at 07:26
  • I checked again the dependencies of spring-aspects and couldn't find any milestone repositories ! From where does it download the aspectjweaver 1.8.0.M1 ?? – Serafeim Dec 20 '13 at 07:46
  • It seems that is using gradle in dependency management. In the file https://github.com/spring-projects/spring-framework/blob/v4.0.0.RELEASE/build.gradle I can see that ``ext.aspectjVersion = "1.8.0.M1"``. – Serafeim Dec 20 '13 at 07:52

2 Answers2

8

According to a reported issue at springsource, aspectjweaver is "basically identical to AspectJ 1.7" except that it has early support for Java 8.

As I don't need Java 8 support, I basically added a compile dependency to the latest release version of aspectweaver:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.7.4</version>
</dependency>

This ensures that the 1.7.4 is used instead of the milestone release, and is an acceptable workaround for me, for the time being.

reidarok
  • 361
  • 2
  • 6
  • Could you also answer to my question 1: "Since the scope of this dependency is compile why does my application need it" ? Also, in the jira of springsource it is mentioned that "If that still takes a while, we might have to officially document how to replace that dependency with AspectJ 1.7 for anyone concerned there". Could you also include this to your answer (replace the dependency of 1.8.0.M1 with 1.7.4 for aspectjweaver) ? – Serafeim Dec 23 '13 at 10:23
  • 1
    A dependency with the scope "compile" does not mean that you will actually compile it. It only means that the artifact is required for you to compile your project. It is also included in the war file if you do a "mvn package". The compile scope is the default scope, by the way. – reidarok Dec 23 '13 at 13:26
6

To resolve the aspectjweaver dependency, add this repository:

<repository>
    <id>spring-milestones</id>
    <url>http://repo.spring.io/milestone</url>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

This will give you access to version 1.8.0.M1 of aspectjrt, aspectjweaver and aspectjtools.

However, version 1.6 of the aspectj-maven-plugin does not support Java 8.

Yet spring-aspects-4.0.0 has a dependency on aspectjweaver 1.8.0.M1 for Java 8 support.

If you're using Spring 3.2.6 and Java 7, this aspectj-maven-plugin config works:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <complianceLevel>1.7</complianceLevel>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

You might be able to insert an exclusion on aspectjweaver 1.8.0.M1 within the spring-aspects 4.0.0 dependency. And that might allow you to use Spring 4 with Java 7 and all of the 1.7.4 aspectj* dependencies.

In an effort to apply compile-time weaving (CTW) in a Spring 4/Java 8 project, I came across the following posts:

  1. Maven, Scala, Spring, AspectJ

  2. https://weblogs.java.net/blog/fabriziogiudici/archive/2011/07/19/making-lombok-aspectj-and-maven-co-exist

And here is my solution: https://github.com/javawerks/homeschool/blob/master/pom.xml

It works! Note that this solution uses version 1.8.0.M1 of the 3 aspectj* amigos, which can be found here: http://repo.spring.io/milestone/org/aspectj/

Warning: If your local maven repository does not yet have the maven-antrun-plugin dependencies, you will get " can't find * dependency in maven central " errors. To resolve this issue, copy the dependencies to your main dependencies element; mvn clean compile; remove copied dependencies; and maven-antrun-plugin should find the aspectj* dependencies. Then execute 'mvn clean install' and verify the ajc logging.

Finally,in Intellij you will see a ' cannot resolve symbol ' error for this iajc attribute:

aspectPath="${org.springframework:spring-aspects:jar}"

No worries, it works as expected. It's a maven-antrun-plugin naming convention.

Someday, the aspectj-maven-plugin will be updated to support Java 8, and then we can return to normalcy. That said, the showWeaveInfo="true" logging is much nicer.

Hope this helps. Like so many before me, it took a lot of beer to figure this one out.;)

Community
  • 1
  • 1
javawerks
  • 61
  • 2
  • +1 This is a great detailed explanation. If you have time can you please add an example to support the following statement you made. _"You might be able to insert an exclusion on aspectjweaver 1.8.0.M1 within the spring-aspects 4.0.0 dependency. And that might allow you to use Spring 4 with Java 7 and all of the 1.7.4 aspectj* dependencies."_ – Joshua Wilson Jan 24 '14 at 17:46
  • FWIW, I've updated the pom.xml to reflect the latest version of Spring and Aspectj. The aspectj-maven-plugin has still not been updated for Java 8. – javawerks Mar 28 '14 at 19:48
  • The pom.xml contains detailed comments relative to the original post, hopefully making things a bit clearer. – javawerks Mar 30 '14 at 20:41