Maven:编译包含 Java 1.6 源代码的 aspectj 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2610633/
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
Maven: compile aspectj project containing Java 1.6 source
提问by gMale
Primary Question
主要问题
What I want to do is fairly easy. Or so you would think. However, nothing is working properly.
我想做的事情很简单。或者你会这么认为。但是,没有任何东西可以正常工作。
Requirement:Using maven, compile Java 1.6 project using AspectJ compiler.
需求:使用maven,使用AspectJ编译器编译Java 1.6项目。
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).
注意:我们的代码不能用 javac 编译。也就是说,如果方面没有被编入,它编译失败(因为我们有方面可以软化异常)。
Update 2/21/2011:2011 年2 月 21 日更新:对此有两种同样可行的解决方案(两种情况都使用aspectj-maven-pluginaspectj-maven-plugin与maven-compiler-pluginMaven 编译器插件):
- Add
<failOnError>false</failOnError>
to the compiler plugin (thanks Pascal Thivent) - Add
<phase>process-sources</phase>
to the aspectj compiler plugin (thanks Andrew Swan)
- 添加
<failOnError>false</failOnError>
到编译器插件(感谢 Pascal Thivent) - 添加
<phase>process-sources</phase>
到 aspectj 编译器插件(感谢Andrew Swan)
More info on these solutions is in the answer section. I believe solution #2 is the better approach.
有关这些解决方案的更多信息,请参见答案部分。我相信解决方案#2 是更好的方法。
Related Questions
相关问题
Questions (based on failed attempts below):
问题(基于以下失败的尝试):
- How do you get maven to run the aspectj:compile goal directly, without ever running compile:compile?
- How do you ignore the failure of compile:compile?
- How do you specify a custom compilerId that points to your own ajc compiler (that is make compile:compile use an aspectj compiler other than the plexus one)?*
- 你如何让 maven 直接运行 aspectj:compile 目标,而无需运行 compile:compile?
- 你如何忽略 compile:compile 的失败?
- 您如何指定指向您自己的 ajc 编译器的自定义 compilerId(即 make compile:compile 使用除 plexus 之外的一个 aspectj 编译器)?*
Attempt 1 (fail):尝试 1(失败):指定 aspectJ 作为 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
org.codehaus.plexus.compiler.CompilerException: The source version was not recognized: 1.6
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.
无论我使用什么版本的 plexus 编译器(1.8、1.6、1.3 等),这都不起作用。实际看了源码,发现这个编译器不喜欢Java 1.5以上的源码。
Attempt 2 (fail):Use the aspectJ-maven-plugin attached to the compile and test-compile goals:
尝试 2(失败):使用附加到编译和测试编译目标的 aspectJ-maven-plugin:
<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
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.
因为它试图在运行 aspectj:compile 之前执行 compile:compile。如上所述,我们的代码不能用 javac 编译——这些方面是必需的。所以 mvn 需要完全跳过 compile:compile 目标而只运行 aspectj:compile。
Attempt 3 (works but unnacceptable):
尝试 3(有效但不可接受):
Use the same configuration above but instead run:
使用上面相同的配置,但运行:
mvn clean aspectj:compile
mvn clean aspectj:compile
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)
这是有效的,因为它成功构建,但它是不可接受的,因为我们需要能够直接运行编译目标和测试编译目标(m2eclipse 自动构建取决于这些目标)。此外,以这种方式运行它需要我们在此过程中阐明我们想要的每个目标(例如,我们需要分发资源并运行测试并部署测试资源等)
采纳答案by Andrew Swan
Version 1.3 of the AspectJ plugin deliberately changedthe 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:
AspectJ 插件的 1.3 版有意将其编译目标的默认阶段从“process-sources”更改为“compile”。要恢复之前在 javac 之前运行 ajc 的行为,只需在相关的“execution”标签中添加一个“phase”标签,如下所示:
<execution>
<phase>process-sources</phase> <!-- or any phase before compile -->
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
回答by Pascal Thivent
How do you specify a custom compilerId that points to your own ajc compiler (that is make compile:compile use an aspectj compiler other than the plexus one)?
您如何指定指向您自己的 ajc 编译器的自定义 compilerId(即 make compile:compile 使用除 plexus 编译器以外的 aspectj 编译器)?
I don't know how to specify another compilerId
than the "official"ones. Not sure it's possible.
我不知道如何指定“官方”compilerId
以外的其他内容。不确定是否有可能。
My understanding is that http://jira.codehaus.org/browse/MCOMPILER-107would solve your problem (AspectJ 1.6+ does support Java 1. 6 right?). Sadly, it's still open.
我的理解是http://jira.codehaus.org/browse/MCOMPILER-107可以解决您的问题(AspectJ 1.6+ 确实支持 Java 1. 6,对吗?)。遗憾的是,它仍然是开放的。
How do you ignore the failure of compile:compile?
你如何忽略 compile:compile 的失败?
The compiler:compile
goal of the Maven Compiler pluginhas a failOnError
optional parameter allowing to Indicate whether the build will continue even if there are compilation errors.
Maven Compiler 插件的compiler:compile
目标有一个可选参数,允许指示即使存在编译错误,构建是否会继续。failOnError
<project>
...
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<failOnError>false</failOnError>
...
</configuration>
</plugin>
</plugins>
</build>
</project>
This could be a ugly workaround to the above problem.
这可能是上述问题的丑陋解决方法。
How do you get maven to run the aspectj:compile goal directly, without ever running compile:compile?
你如何让 maven 直接运行 aspectj:compile 目标,而无需运行 compile:compile?
The problem is that compiler:compile
is bound to the compile
phase and that you can't remove a default lifecyle binding. So there is maybe another option but the only one I can think of would be to turn everything offby using a <packaging>pom<packaging>
and to rebind all the goals manually (at least for these phases: process-resources
, compile
, process-test-resources
, test-compile
, test
, package
). Schematically, something like this:
问题是它compiler:compile
绑定到compile
阶段,并且您无法删除默认的生命周期绑定。因此,有可能是另一种选择,但我唯一能想到的是把一切关闭使用<packaging>pom<packaging>
,并手动重新绑定所有的目标(至少在以下几个阶段:process-resources
,compile
,process-test-resources
,test-compile
,test
,package
)。示意性地,是这样的:
process-resources resources:resources compile aspectj:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war install install:install deploy deploy:deploy
That could be another ugly workaround. Disclaimer: not tested but should work.
这可能是另一种丑陋的解决方法。免责声明:未经测试但应该可以工作。
回答by matsev
I used this configuration in a project to compile AspectJ and Java 6 using Maven:
我在一个项目中使用了这个配置来使用 Maven 编译 AspectJ 和 Java 6:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Ref: aspectj-maven-plugin
回答by Tomasz Domzal
How about telling maven-compiler-plugin to skip all *.java files and let aspectj-maven-plugin do the job ?
如何告诉 maven-compiler-plugin 跳过所有 *.java 文件并让 aspectj-maven-plugin 完成这项工作?
...
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
<complianceLevel>1.6</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- weave main classes -->
<goal>test-compile</goal> <!-- weave test classes -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>