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

标签 java maven-2 aop aspectj

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

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

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

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

最佳答案

我显然是唯一可以回答我自己的问题的人。

我已使用 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类。这是我的 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 integration ,结合 Maven、Groovy 和 Ant 的强大功能,很容易组装参数。

关于java - APT和AOP在同一个项目中,使用Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3660547/

相关文章:

c# - 使用 C# 5 Caller Information 的解决方案示例

java - 无效的plugin.yml bukkit插件错误

java - 在 JodaTime 中使用 Hibernate 注释的问题

java - 添加附加路径到 exec-maven-plugin

java - 如何使用外部域jar打包EJB3

c++ - C++ 中的面向方面编程 - 当前支持的替代方案

java - 仅使用 AspectJ 将切面应用于带注释的方法(不使用 Spring)

java - 通过其余高级客户端在 Elasticsearch 中创建类型内部索引

java - 返回 'data' 可能会暴露内部数组?

java - 如何使用 Lombok + Gson 创建 JSON 对象数组?