在一个项目中使用Aspectj编译时编织和Java编译时注释处理 [英] Using Aspectj compile time weaving and Java compile time annotation processing in one project

查看:118
本文介绍了在一个项目中使用Aspectj编译时编织和Java编译时注释处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了广泛搜索,没有找到任何可以帮助我解决问题的信息.我正在尝试实现某些功能,并且正在创建PoC解决方案,但这是问题所在:

I have searched far and wide and have not found anything that would help me resolve my problem. I am trying to implement a certain functionality and I am creating a PoC solution, here is the issue though:

涉及使用编译时编织的AspectJ和编译时注释处理器

而且我不知道如何同时使用这两个功能.

and I have no idea how to make both of those work at the same time.

到目前为止,我只在使用Aspectj-maven-plugin的* .aj文件中使用了AspectJ方面,并且工作正常.但是,当我尝试使用maven-compiler-plugin添加注释处理时,一个问题就显而易见了:aspectj-maven-plugin在编译阶段生成的目标目录中的编织类被maven-compiler-plugin覆盖了.根据它在编译阶段生成的类.

So far I have been using just AspectJ aspects in *.aj files with the aspectj-maven-plugin, and it was working fine. As soon as I tried adding annotation processing with a maven-compiler-plugin though, an issue became aparent: the weaved classes in the target directory generated in the compilation phase by the aspectj-maven-plugin were overwritten by the maven-compiler-plugin by the classes it generated during it's compilation phase.

控制台输出显示,首先AspectJ-Maven-plugin可以正常工作并编织所有类:

The console output says that first the aspectj-maven-plugin does it's work and weaves all the classes:

[INFO] --- aspectj-maven-plugin:1.10:compile (default) @ demo ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
...and a list of all the Join Points and Intertypes done

但是随后,maven-compiler-plugin开始工作并重新编译所有内容(并使用注释处理器生成类):

But then the maven-compiler-plugin goes to work and recompiles everything (and generates classes with the annotation processor):

[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to dir\demo\target\classes
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (compile-project) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 19 source files to dir\demo\target\classes

这是pom.xml的相关部分:

Here is the relevant part of the pom.xml:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
    <source>${maven.compiler.source}</source>
    <target>${maven.compiler.target}</target>
</configuration>
<executions>
    <execution>
        <id>default-compile</id>
        <configuration>
            <compilerArgument>-proc:none</compilerArgument>
            <includes>
                <include>
                    path/to/annotation/processor/Processor.java
                </include>
            </includes>
        </configuration>
    </execution>
    <execution>
        <id>compile-project</id>
        <phase>compile</phase>
        <goals>
            <goal>compile</goal>
        </goals>
    </execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
</dependencies>
<configuration>
    <showWeaveInfo>true</showWeaveInfo>
    <complianceLevel>${maven.compiler.source}</complianceLevel>
    <source>${maven.compiler.source}</source>
    <target>${maven.compiler.target}</target>
</configuration>
<executions>        
    <execution>
        <phase>process-sources</phase>
        <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
        </goals>
    </execution>
</executions>
</plugin>

另外,方面和注释处理器都可以正常工作,因此它们并不是问题,我认为问题是构建配置.

Separately, both the aspects and the annotation processors work fine, so they specifically aren't the problem, I assume the issue is the build configuration.

我猜测要么是我弄乱了插件中的执行/阶段,要么是我什至不需要使用这两个插件,也许只配置一个就足够了,但是我没有如何将编译与ajc结合起来并运行注释处理器的想法(在两个阶段中,首先使用proc none来编译注释处理器本身,然后进行注释处理).请帮忙!

I'm guessing that either I am messing something up with the executions/phases in the plugins, or maybe I don't even need to use both of them, maybe just one properly conifgured would be enough, but I have no idea how to combine compilation with ajc and running the annotation processor (and in two phases, first with proc none to compile the Annotation Processor itself and then to do the annotation processing). Please help!

推荐答案

到目前为止,对我来说,最好的策略是在使用AspectJ编译器时完全禁用Java编译器,并让AspectJ编译器单独进行注释处理和编译.一个示例maven构建配置如下所示:

For me the best strategy so far was to disable the Java compiler altogether when using the AspectJ compiler, and let the AspectJ compiler do the annotation processing and the compilation alone. An example maven build configuration would look like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <skipMain>true</skipMain>
                <skip>true</skip>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果由于某种原因对您不起作用,您可以尝试将Java编译器仅用于注释处理,然后让AspectJ编译器完成其余的工作.

If that doesn't work for you for some reason, you could try using the Java compiler for annotation processing only and let the AspectJ compiler do the rest.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <proc>only</proc>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <proc>none</proc>
            </configuration>
        </plugin>
    </plugins>
</build>

我还看到您在构建文件中定义了太多执行(对于Java编译器为2个:default-compile + compile-project),具有非默认执行ID.我建议您只保留具有默认ID的默认执行,除非您有特定的原因要保留它们.

I also see you've got too many executions defined in your build file (2 for the java compiler: default-compile + compile-project), with non-default execution IDs. I suggest you leave only the default executions with their default ID, unless you have a specific reason for having them.

这篇关于在一个项目中使用Aspectj编译时编织和Java编译时注释处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆