代码之家  ›  专栏  ›  技术社区  ›  Sean Patrick Floyd

APT和AOP在同一个项目中,使用Maven

  •  3
  • Sean Patrick Floyd  · 技术社区  · 14 年前

    我知道唯一的标准选择是使用 weaveDependencies

    好的,我可以嵌入 AspectJ ant tasks Maven Antrun Plugin 但我不想诉诸于此。

    2 回复  |  直到 14 年前
        1
  •  9
  •   Sean Patrick Floyd    14 年前

    我显然是唯一一个能回答我自己问题的人。

    Maven Antrun Plugin . 以下是我的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>
    

    <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插件,在内部执行所有这些ant调用。虽然我不能在这里共享代码,但我将展示它如何简化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和Ant的强大功能,可以方便地组装参数。

        2
  •  4
  •   yegor256    11 年前

    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 用法页。