I have to use Annotation Processing (apt) and AspectJ in the same Maven project.
Both work for themselves, but I need to create aspects based on code created by apt. So I would need binary we开发者_运维技巧aving (the original source files are extended by apt). How can I enable binary weaving within a maven project?
I know the only standard option is to supply a dependency using the weaveDependencies parameter, but this is awful. 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.
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. Here's my pom snippet:
<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. Here's my ant XML file (the nice part is that I can use it for both compile and test-compile):
<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. While I can't share the code here, I'll show how it simplified POM configuration:
<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.
Inspired by the solution proposed above by Sean Patrick Floyd I created a Maven plugin that does all that out of the box:
<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.
精彩评论