Adding AspectJ to pom.xml changed Java version with Maven, why?

Sanyifejű picture Sanyifejű · Feb 4, 2014 · Viewed 17.1k times · Source

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>

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?

Answer

DB5 picture DB5 · Feb 4, 2014

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>