2010-09-07 17 views
3

我必須在同一個Maven項目中使用Annotation Processing(apt)和AspectJ。APT和AOP在同一個項目中使用Maven

兩者都適用於自己,但我需要根據apt創建的代碼創建方面。所以我需要二進制編織(原始源文件由apt擴展)。如何在Maven項目中啓用二進制編織?

我知道唯一的標準選擇是使用參數weaveDependencies來提供依賴關係,但這很糟糕。有沒有其他方法?

好的,我可以使用Maven Antrun Plugin嵌入AspectJ ant tasks,但我不願訴諸於此。

+1

我在[com.jcabi:parent](http://www.jcabi.com/parent/index.html)中實現了你的想法,並做了一些小而重要的改進:https://github.com/yegor256/jcabi /blob/jcabi-0.7.9/parent/pom.xml#L538-681。謝謝! – yegor256 2013-02-27 17:24:03

+0

@ yegor256看起來非常好! – 2013-02-28 08:19:02

回答

8

我顯然是唯一能回答我自己的問題的人。

我使用Maven Antrun Plugin通過ant編譯AspectJ。這裏是我的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> 

我編譯Java類第一(讓APT做的東西),使用編譯的類作爲AspectJ的二進制輸入,編譯的AspectJ到一個新的文件夾和移動的機織類的原始編譯目錄,覆蓋非aspectj類。這是我的螞蟻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> 

在我現在已經創建了一個Maven插件,做這一切的螞蟻下一步內部呼叫。雖然我不能在這裏分享代碼,我會告訴它簡化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> 

使用ANT/GMaven integration,很容易組裝相結合的Maven,Groovy和螞蟻的權力參數。

+1

'我顯然是唯一一個能夠回答我自己的問題的人'那麼,這就是你用像這樣的高級用例得到的東西+1 +1 +1。 – 2010-09-11 04:57:40

+0

您可能想要檢查我在類似情況下遇到的[MCOMPILER-157](http://jira.codehaus.org/browse/MCOMPILER-157)問題。 – 2011-07-19 13:33:59

4

啓發由肖恩·帕特里克·弗洛伊德上面提出的解決方案,我創建了一個Maven plugin,做所有的開箱即用:

<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的目標文件是在com.jcabi:jcabi-maven-plugin:ajc使用頁。

+0

太棒了!我會在3年前讚賞:-) – 2013-05-07 12:02:44

+0

@ yegor256如何將這項工作用於java獨立應用程序? – 2018-02-09 05:38:21

相關問題