加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:53:01 所属栏目:Java 来源:网络整理
导读:我必须在同一个Maven项目中使用Annotation Processing(apt)和AspectJ. 两者都适合自己,但我需要根据apt创建的代码创建方面.所以我需要二进制编织(原始源文件由apt扩展).如何在maven项目中启用二进制编织? 我知道唯一的标准选项是使用weaveDependencies参数
我必须在同一个Maven项目中使用Annotation Processing(apt)和AspectJ.

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

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

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

解决方法

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

我已经使用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插件,可以在内部完成所有这些蚂蚁调用.虽然我不能在这里分享代码,但我将展示它如何简化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的功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读