gMa*_*ale 20 java aop maven-2 aspectj
我想做的事情相当容易.或者你会想.但是,没有什么工作正常.
要求: 使用maven,使用AspectJ编译器编译Java 1.6项目.
注意: 我们的代码无法使用javac进行编译.也就是说,如果未编入方面,则编译失败(因为我们有方面可以软化异常).
<failOnError>false</failOnError>
到编译器插件(感谢
Pascal Thivent)<phase>process-sources</phase>
到aspectj编译器插件(感谢Andrew Swan)有关这些解决方案的更多信息,请参阅答案部分.我认为解决方案#2是更好的方法.
问题(基于以下失败的尝试):
<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>
Run Code Online (Sandbox Code Playgroud)
这失败并出现错误:
org.codehaus.plexus.compiler.CompilerException: The source version was not recognized: 1.6
无论我使用的是什么版本的plexus编译器(1.8,1.6,1.3等),这都行不通.我实际上阅读了源代码,发现这个编译器不喜欢Java 1.5以上的源代码.
尝试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>
Run Code Online (Sandbox Code Playgroud)
运行时,这会失败:
mvn clean test-compile
mvn clean compile
因为它试图在运行aspectj:compile之前执行compile:compile.如上所述,我们的代码不能使用javac编译 - 这些方面是必需的.所以mvn需要跳过编译:完全编译目标并且只运行aspectj:compile.
尝试3(工作但不可接受):
使用上面相同的配置,而是运行:
mvn clean aspectj:compile
这是有效的,因为它构建成功,但是我们需要能够直接运行编译目标和测试编译目标(m2eclipse自动构建取决于这些目标),这是不可接受的.而且,以这种方式运行它需要我们在整个过程中拼出我们想要的每个目标(例如,我们需要分配资源并运行测试和部署测试资源等)
And*_*wan 16
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21155 次 |
| 最近记录: |