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

发布于 2024-09-17 17:15:33 字数 575 浏览 6 评论 0原文

我必须在同一个 Maven 项目中使用注释处理 (apt) 和 AspectJ。

两者都为自己工作,但我需要根据 apt 创建的代码创建方面。所以我需要二进制编织(原始源文件由apt扩展)。如何在 Maven 项目中启用二进制编织?

我知道唯一的标准选项是使用 weaveDependency 提供依赖项 参数,但这很糟糕。还有其他办法吗?

好的,我可以使用 AspectJ ant 任务 嵌入 < a href="http://maven.apache.org/plugins/maven-antrun-plugin/" rel="nofollow noreferrer">Maven Antrun 插件 但我不想诉诸于此。

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 weaving (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在我安 2024-09-24 17:15:33

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

我已经通过 ant 使用 Maven Antrun 插件 来编译 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 类。这是我的 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>

在下一步中,我现在创建了一个 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 集成,很容易组合 Maven、Groovy 和 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. 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.

终陌 2024-09-24 17:15:33

受到 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 目标文档位于 < code>com.jcabi:jcabi-maven-plugin:ajc 使用页面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文