[英]APT and AOP in the same project, using Maven
I have to use Annotation Processing (apt) and AspectJ in the same Maven project. 我必须在同一个Maven项目中使用Annotation Processing(apt)和AspectJ。
Both work for themselves, but I need to create aspects based on code created by apt. 两者都适合自己,但我需要根据apt创建的代码创建方面。 So I would need binary weaving (the original source files are extended by apt). 所以我需要二进制编织(原始源文件由apt扩展)。 How can I enable binary weaving within a maven project? 如何在maven项目中启用二进制编织?
I know the only standard option is to supply a dependency using the weaveDependencies parameter, but this is awful. 我知道唯一的标准选项是使用weaveDependencies参数提供依赖项 ,但这很糟糕。 Is there any other way? 还有其他方法吗?
OK, I could embed the AspectJ ant tasks using the Maven Antrun Plugin but I'd hate to resort to that. 好吧,我可以使用Maven Antrun插件嵌入AspectJ ant任务 ,但我不想诉诸于此 。
I am apparently the only one who can answer my own questions. 我显然是唯一能够回答我自己问题的人。
I have resorted to compiling AspectJ via ant using the Maven Antrun Plugin . 我已经使用Maven Antrun Plugin通过ant编译AspectJ。 Here's my pom snippet: 这是我的pom片段:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>ajc-compile</id>
<phase>process-classes</phase>
<configuration>
<tasks>
<property name="aspectj.sourcepath"
value="${project.basedir}/src/main/aspect" />
<property name="aspectj.binarypath"
value="${project.build.outputDirectory}" />
<property name="aspectj.targetpath"
value="${project.build.directory}/aspectj-classes" />
<property name="scope_classpath" refid="maven.compile.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<ant antfile="ajc-ant.xml" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>ajc-test-compile</id>
<phase>process-test-classes</phase>
<configuration>
<tasks unless="maven.test.skip">
<property name="aspectj.sourcepath"
value="${project.basedir}/src/test/aspect;${project.basedir}/src/main/aspect" />
<property name="aspectj.binarypath"
value="${project.build.testOutputDirectory}" />
<property name="aspectj.targetpath"
value="${project.build.directory}/aspectj-test-classes" />
<property name="scope_classpath" refid="maven.test.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<ant antfile="ajc-ant.xml" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I compile java classes first (and let APT do it's stuff), use the compiled classes as binary input for aspectj, compile aspectj into a new folder and move the resulting woven classes to the original compile directory, overwriting the non-aspectj classes. 我首先编译java类(让APT做它的东西),使用编译的类作为aspectj的二进制输入,将aspectj编译到一个新文件夹中,并将生成的编织类移动到原始的编译目录,覆盖非aspectj类。 Here's my ant XML file (the nice part is that I can use it for both compile and test-compile): 这是我的ant XML文件(很好的部分是我可以将它用于编译和测试编译):
<project basedir="." default="ajc">
<path id="classpath">
<pathelement path="${scope_classpath}" />
<pathelement path="${plugin_classpath}" />
</path>
<taskdef
classname="org.aspectj.tools.ant.taskdefs.AjcTask"
name="iajc" classpathref="classpath" />
<target name="ajc">
<iajc
sourceroots="${aspectj.sourcepath}"
inpath="${aspectj.binarypath}"
destdir="${aspectj.targetpath}"
classpathref="classpath"
source="1.6"
target="1.6"
/>
<move todir="${aspectj.binarypath}">
<fileset dir="${aspectj.targetpath}">
<include name="**/*.class" />
</fileset>
</move>
</target>
</project>
In the next step I have now created a Maven Plugin that does all this ant calling internally. 在下一步中,我现在已经创建了一个Maven插件,可以在内部完成所有这些蚂蚁调用。 While I can't share the code here, I'll show how it simplified POM configuration: 虽然我不能在这里分享代码,但我将展示它如何简化POM配置:
<plugin>
<groupId>com.myclient.maven.plugins</groupId>
<artifactId>maven-ajc-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>compile-ajc</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testcompile-ajc</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<aspectSourcePath>${project.basedir}/src/main/aspect</aspectSourcePath>
</configuration>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
Using ANT / GMaven integration , it was easy to assembly the parameters combining the powers of Maven, Groovy and Ant. 使用ANT / GMaven集成 ,可以很容易地组合Maven,Groovy和Ant的功能。
Inspired by the solution proposed above by Sean Patrick Floyd I created a Maven plugin that does all that out of the box: 受Sean Patrick Floyd上面提出的解决方案的启发,我创建了一个Maven插件 ,可以完成所有开箱即用的操作:
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.7.18</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
Mojo goal documentation is at com.jcabi:jcabi-maven-plugin:ajc
usage page. Mojo目标文档位于com.jcabi:jcabi-maven-plugin:ajc
用法页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.