将 AspectJ 添加到 pom.xml 使用 Maven 更改了 Java 版本,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21548548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding AspectJ to pom.xml changed Java version with Maven, why?
提问by Sanyifej?
UPDATE:here is my maven-compiler-plugin configuration:
更新:这是我的 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>
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)
我在使用 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>
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:
不知何故,这种修改覆盖了我使用的 Java 版本。如果我运行 build 我会收到多个这样的错误:
Syntax error, annotations are only available if source level is 1.5 or greater
语法错误,注释仅在源级别为 1.5 或更高时可用
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.
这让我怀疑我的 Java 版本(最初是 1.6)以某种方式恢复到 1.4。我没有做任何可能影响 Java 版本的事情——至少不是故意的——所以我怀疑上面提到的 AspectJ 相关代码是造成变化的原因。
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?
我的问题是 AspectJ 如何更改 Java 版本以及我应该如何解决此问题。还是我完全误解了某些东西并且我走错了路?
采纳答案by DB5
I think the problem is with the default source, targetand complianceLevelsettings 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:
我认为问题在于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>
回答by Cristina Galán
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.
如果您没有定义版本,编译器插件会假定您的 Java 源代码符合 Java 1.3 并且您的目标是 Java 1.1 JVM。
Maybe, you should define it:
也许,你应该定义它:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
回答by Oscar H
I was missing
我失踪了
<complianceLevel>${java.level}</complianceLevel>
in my pom.xml
在我的 pom.xml 中
回答by egemen
You can find last versions of dependecy and plugin for aspectj.
回答by Lucky
I was missing the java version with jdk version at the top of my pom properties.
我的 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>
and at the top of my pom.xml I had set jdk.version property like,
在我的 pom.xml 顶部,我设置了 jdk.version 属性,例如,
<properties>
<jdk.version>1.7</jdk.version>
</properties>