[英]Adding AspectJ to pom.xml changed Java version with Maven, why?
更新:這是我的maven-compiler-plugin配置:
<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>
我在使用Maven構建的多項目應用程序上工作。 我們決定添加AspectJ,所以我將以下代碼添加到頂級項目中的pom.xml
:(來自官方文檔)
<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>
以及每個下屬項目的以下片段:
</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>
不知怎的,這個修改已經覆蓋了我使用的Java版本。 如果我運行構建,我會得到多個這樣的錯誤:
語法錯誤,注釋僅在源級別為1.5或更高時可用
這讓我懷疑我的Java版本(原來是1.6)以某種方式還原為1.4。 我什么都沒做 - 至少不是故意 - 可能影響Java版本,所以我懷疑上面提到的AspectJ相關代碼負責改變。
我的問題是AspectJ如何改變Java版本以及如何解決這個問題。 或者我是否完全誤解了一些事情,我是否走錯了路?
我認為問題在於aspectj-maven-plugin的默認source , target和complianceLevel設置(根據之前鏈接的文檔,分別為1.4,1.2和1.4)。 您應該在父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>
我失蹤了
<complianceLevel>${java.level}</complianceLevel>
在我的pom.xml中
如果您沒有定義版本,則編譯器插件假定您的Java源代碼符合Java 1.3並且您的目標是Java 1.1 JVM。
也許,你應該定義它:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
我錯過了我的pom屬性頂部的jdk版本的java版本。
<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>
在我的pom.xml的頂部,我設置了jdk.version屬性,如,
<properties>
<jdk.version>1.7</jdk.version>
</properties>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.