简体   繁体   中英

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

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?

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>

I was missing

<complianceLevel>${java.level}</complianceLevel>

in my pom.xml

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

您可以找到aspectj的最新版本的dependecy和plugin

I was missing the java version with jdk version at the top of my pom properties.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.2</version> <!-- 1.5 dint work for me -->
    <dependencies>
        <!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>process-sources</phase> <!-- or any phase before compile -->
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outxml>true</outxml>
        <source>${jdk.version}</source>  <!-- I was missing this -->
        <target>${jdk.version}</target>  <!-- jdk.version property -->
    </configuration>
</plugin>

and at the top of my pom.xml I had set jdk.version property like,

<properties>
    <jdk.version>1.7</jdk.version>
</properties>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM