将 AspectJ 添加到 pom.xml 使用 Maven 更改了 Java 版本,为什么? [英] Adding AspectJ to pom.xml changed Java version with Maven, why?

查看:40
本文介绍了将 AspectJ 添加到 pom.xml 使用 Maven 更改了 Java 版本,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是我的 maven-compiler-plugin 配置:

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>

我在使用 Maven 构建一个多项目应用程序.我们决定添加AspectJ,所以我在顶级项目的pom.xml中添加了以下代码:(来自官方文档)

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>

不知何故,此修改覆盖了我使用的 Java 版本.如果我运行 build 我会收到多个这样的错误:

Somehow this modification has overridden the Java version I use. If I run build I get multiple errors like this:

语法错误,注释仅在源级别为 1.5 或更高版本时可用

Syntax error, annotations are only available if source level is 1.5 or greater

这让我怀疑我的 Java 版本(最初是 1.6)不知何故恢复到 1.4.我没有做任何可能影响 Java 版本的事情 - 至少不是故意的 - 所以我怀疑上面提到的 AspectJ 相关代码是造成变化的原因.

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.

我的问题是 AspectJ 如何更改 Java 版本以及我应该如何解决此问题.还是我完全误解了某些东西并且我走错了路?

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?

推荐答案

我认为问题在于默认的 目标complianceLevel aspectj-maven-plugin 的设置(根据之前链接的文档,分别为 1.4、1.2 和 1.4).你应该在你的父 pom 中明确设置这些:

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>

这篇关于将 AspectJ 添加到 pom.xml 使用 Maven 更改了 Java 版本,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆