5

How do I have to setup the pom.xml so the aspectj-maven-plugin uses Java 7 to compile?

When I compile with my current configuration (see below), I always get a message complaining about my use of some Java 7 specific feature, e.g.

error: multi-catch statement is not supported in -source 1.5

I use eclipse, in the project properties -> java compiler it says 1.5 for compliance level, source, target. What am I doing wrong?

A very similar question was asked here, but the solution doesn't work for me, I'm already on version 1.7.4 of org.aspectj.

The question can also be rephrased as:

How can I make maven compile my code using Java 7 and weave the aspects in?

I don't have to use the aspectj-maven-plugin. But what other way is there that works?

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.stuff</groupId>
    <artifactId>artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Community
  • 1
  • 1
hansi
  • 2,278
  • 6
  • 34
  • 42

4 Answers4

3

I found a way to do it in this question. The problem seems to be that the java and aspectj compilers are not called in the order one would expect.

In short, adding <phase>process-sources</phase> did the job for me:

<executions>
 <execution>
  <phase>process-sources</phase>
  <goals>
   <goal>compile</goal>
   <goal>test-compile</goal>
  </goals>
 </execution>
</executions>
Community
  • 1
  • 1
hansi
  • 2,278
  • 6
  • 34
  • 42
  • While this solution works - I would recommend to use a different approach in the future that doesn't move compilation to the process-sources phase. This can be cause issues with other plugins that are bound to that phase. I am adding an answer with my solution – Dagan Sandler Jun 07 '16 at 08:23
0

Looking at the mojo configuration elements it looks like you have source and target configured as I'd expect. Have you tried setting complianceLevel to 1.7 as well?

Also - I just read the question you referenced more closely. The answer shows that there is a <dependencies> block inside the plugin configuration, which the configuration above does not have. If setting complianceLevel doesn't work, remove it, and add the plugin dependency instead.

user944849
  • 14,524
  • 2
  • 61
  • 83
0

Please make sure you add phase element as below

                <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
Gaetano Caruana
  • 141
  • 1
  • 3
0

While the accepted answer solves the issue, it may cause issues with other plugins in the process-sources phase as compilation may happen too early in the build lifecycle.

The better approach would be to disable maven-compiler-plugin altogether. This can be achieved by adding the following to maven-compiler-plugin declaration.

<executions>
    <execution>
        <id>default-compile</id>
        <phase>none</phase>
    </execution>
</executions>

It is safe to do as the code gets compiled by aspectj-maven-plugin anyway.

In any case, if using the accepted answer, one should also set the source/target java version of maven-compiler-plugin if it's not disabled

Dagan Sandler
  • 475
  • 1
  • 5
  • 16