Maven:编译包含 Java 1.6 源代码的 aspectj 项目 [英] Maven: compile aspectj project containing Java 1.6 source

查看:27
本文介绍了Maven:编译包含 Java 1.6 源代码的 aspectj 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的很简单.或者你会这么认为.但是,没有任何东西可以正常工作.

What I want to do is fairly easy. Or so you would think. However, nothing is working properly.

要求:使用 maven,使用 AspectJ 编译器编译 Java 1.6 项目.

Requirement: Using maven, compile Java 1.6 project using AspectJ compiler.

注意:我们的代码不能用 javac 编译.也就是说,如果方面没有被编入,它编译失败(因为我们有方面可以软化异常).


2011 年 2 月 21 日更新:对此有两种同样可行的解决方案(两种情况都使用 aspectj-maven-pluginmaven-compiler-plugin):

Note: Our code cannot compile with javac. That is, it fails compilation if aspects are not woven in (because we have aspects that soften exceptions).


Update 2/21/2011: There are two equally viable solutions to this (both cases use the aspectj-maven-plugin in conjuction with the maven-compiler-plugin):

  1. 添加false到编译器插件(感谢Pascal Thivent)
  2. 添加process-sources到 aspectj 编译器插件(感谢安德鲁·斯旺)

有关这些解决方案的更多信息在答案部分.我相信解决方案 #2 是更好的方法.

More info on these solutions is in the answer section. I believe solution #2 is the better approach.













问题(基于以下失败的尝试):

  1. 如何让 maven 直接运行 aspectj:compile 目标,而无需运行 compile:compile?
  2. 如何忽略 compile:compile 的失败?
  3. 如何指定指向您自己的 ajc 编译器的自定义 compilerId(即 make compile:compile 使用除 plexus 之外的 aspectj 编译器)?*

感谢您的任何和所有建议.这些是我尝试过的导致我的问题/疑问的事情:

<小时>尝试 1(失败):指定 aspectJ 作为 maven-compiler-plugin 的编译器:

Thanks for any and all suggestions. These are the things I've tried that have led to my problem/questions:


Attempt 1 (fail): Specify aspectJ as the compiler for the maven-compiler-plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
 <source>1.6</source>
 <target>1.6</target>
 <compilerId>aspectj</compilerId>
</configuration>
<dependencies>
 <dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-compiler-aspectj</artifactId>
  <version>1.8</version>
 </dependency>
</dependencies>
</plugin>

失败并出现错误:

org.codehaus.plexus.compiler.CompilerException: 无法识别源版本:1.6

无论我使用什么版本的 plexus 编译器(1.8、1.6、1.3 等),这都不起作用.实际看了一下源码,发现这个编译器不喜欢Java 1.5以上的源码.

No matter what version of the plexus compiler I use (1.8, 1.6, 1.3, etc), this doesn't work. I actually read through the source code and found that this compiler does not like source code above Java 1.5.

尝试 2(失败):使用附加到编译和测试编译目标的 aspectJ-maven-plugin:

Attempt 2 (fail): Use the aspectJ-maven-plugin attached to the compile and test-compile goals:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
 <source>1.6</source>
 <target>1.6</target>
</configuration>
<executions>
 <execution>
  <goals>
   <goal>compile</goal>      
   <goal>test-compile</goal> 
  </goals>
 </execution>
</executions>
</plugin>

这在运行时失败:

mvn clean test-compile
mvn 干净编译

因为它试图在运行 aspectj:compile 之前执行 compile:compile.如上所述,我们的代码不能用 javac 编译——这些方面是必需的.所以 mvn 需要完全跳过 compile:compile 目标而只运行 aspectj:compile.

because it attempts to execute compile:compile before running aspectj:compile. As noted above, our code doesn't compile with javac--the aspects are required. So mvn would need to skip the compile:compile goal altogether and run only aspectj:compile.

尝试 3(有效但不可接受):

使用与上面相同的配置,但运行:

Use the same configuration above but instead run:

mvn clean aspectj:compile

这是可行的,因为它可以成功构建,但不能接受,因为我们需要能够直接运行编译目标和测试编译目标(m2eclipse 自动构建取决于这些目标).此外,以这种方式运行它需要我们在此过程中阐明我们想要的每个目标(例如,我们需要分发资源和运行测试以及部署测试资源等)

This works, in that it builds successfully but it's unacceptable in that we need to be able to run the compile goal and the test-compile goal directly (m2eclipse auto-build depends on those goals). Moreover, running it this way would require that we spell out every goal we want along the way (for instance, we need resources distributed and tests to be run and test resources deployed, etc)

推荐答案

AspectJ 插件的 1.3 版有意将其编译目标的默认阶段从 process-sources 更改为 compile,参见 MASPECTJ-13.这会导致像您这样的后续问题或 MASPECTJ-92.要恢复之前在 javac 之前运行 ajc 的行为,您只需要添加一个阶段"即可.标记到相关的执行"标签,像这样:

Version 1.3 of the AspectJ plugin deliberately changed the default phase of its compile goal from process-sources to compile, see MASPECTJ-13. This causes follow-up problems like yours or MASPECTJ-92. To restore the previous behaviour of running ajc before javac, you just need to add a "phase" tag to the relevant "execution" tag, like this:

<execution>
    <phase>process-sources</phase> <!-- or any phase before compile -->
    <goals>
        <goal>compile</goal>
        <goal>test-compile</goal>
    </goals>
</execution>

这篇关于Maven:编译包含 Java 1.6 源代码的 aspectj 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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