问题描述
我想做的事情很简单.否则,您会认为.但是,一切都无法正常工作.
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-plugin 与 maven-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):
- 添加
<failOnError>false</failOnError>
到编译器插件(感谢 帕斯卡·蒂芬( ) - 添加
<phase>process-sources</phase>
到AspectJ编译器插件 (感谢安德鲁·斯旺)
- Add
<failOnError>false</failOnError>
to the compiler plugin (thanks Pascal Thivent) - Add
<phase>process-sources</phase>
to the aspectj compiler plugin (thanks Andrew Swan)
有关这些解决方案的更多信息,请参见答案部分.我相信解决方案2是更好的方法.
More info on these solutions is in the answer section. I believe solution #2 is the better approach.
问题(基于以下失败的尝试):
- 如何让maven直接运行Aspectj:compile目标,而无需运行compile:compile?
- 您如何忽略compile:compile的失败?
- 如何指定一个指向您自己的ajc编译器(即make compile:compile使用非plexus编译器的aspectj编译器)的自定义CompilerId?*
感谢您提出的所有建议.这些是我尝试过导致我的问题的问题:
尝试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>
此操作失败,并显示以下错误:
This fails with the error:
org.codehaus.plexus.compiler.CompilerException: The source version was not recognized: 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>
在以下任一情况下运行均失败:
This fails when running either:
mvn clean test-compile
mvn clean compile
mvn clean test-compile
mvn clean compile
因为它尝试在运行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版 故意将其编译目标的默认阶段从过程源"更改为编译".要恢复在javac之前运行ajc的先前行为,您只需向相关的"execution"标记中添加一个"phase"标记,如下所示:
Version 1.3 of the AspectJ plugin deliberately changed the default phase of its compile goal from "process-sources" to "compile". 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项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!