lundi 7 avril 2014

Ajout d'AspectJ à pom.xml changé la version de Java avec Maven, pourquoi ? -Débordement de pile


UPDATE!: here is my maven compiler plugin configuration.


         <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
/plugin>

I work on a multi-project application that I build with Maven. We decided to add Aspectj so I added the following code to pom.xml in the top level project: (from the official documentation)


<project>
...
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</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>

and the following fragments to each subordinate projects:


</project>
...
<build>
....
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
....
</dependencies>
...
</project>

Somehow this modification has overridden the Java version I use. If I run build I get multiple errors like this:Syntax error, annotations are only available if source level is 1.5 or greater That gives me the suspicion that my Java version(originally 1.6) was somehow reverted to 1.4. I did nothing -at least not knowingly- that could influence the Java version, so I suspect that the above mentioned aspectJ related code is responsible for the change. My question is how can aspectj change the Java version and what should I do to fix this problem. Or do I misunderstand something completely and am I on the wrong track?




I think the problem is with the default source, target and complianceLevel settings of the aspectj-maven-plugin (according to the documentation linked previously, 1.4, 1.2 and 1.4 respectively). You should set these explicitly in your parent pom:


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<!-- new configuration is here -->
<configuration>
<complianceLevel>1.6</complianceLevel>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
<build>



If you don't have define the version, the compiler plugin assumes that your Java source conforms to Java 1.3 and that you are targeting a Java 1.1 JVM.


Maybe, you should define it:


http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html



UPDATE!: here is my maven compiler plugin configuration.


         <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
/plugin>

I work on a multi-project application that I build with Maven. We decided to add Aspectj so I added the following code to pom.xml in the top level project: (from the official documentation)


<project>
...
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</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>

and the following fragments to each subordinate projects:


</project>
...
<build>
....
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
....
</dependencies>
...
</project>

Somehow this modification has overridden the Java version I use. If I run build I get multiple errors like this:Syntax error, annotations are only available if source level is 1.5 or greater That gives me the suspicion that my Java version(originally 1.6) was somehow reverted to 1.4. I did nothing -at least not knowingly- that could influence the Java version, so I suspect that the above mentioned aspectJ related code is responsible for the change. My question is how can aspectj change the Java version and what should I do to fix this problem. Or do I misunderstand something completely and am I on the wrong track?



I think the problem is with the default source, target and complianceLevel settings of the aspectj-maven-plugin (according to the documentation linked previously, 1.4, 1.2 and 1.4 respectively). You should set these explicitly in your parent pom:


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<!-- new configuration is here -->
<configuration>
<complianceLevel>1.6</complianceLevel>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
<build>


If you don't have define the version, the compiler plugin assumes that your Java source conforms to Java 1.3 and that you are targeting a Java 1.1 JVM.


Maybe, you should define it:


http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html


0 commentaires:

Enregistrer un commentaire