如何通过maven 3.3运行注释处理? [英] How do I run annotation processing via maven 3.3?

查看:156
本文介绍了如何通过maven 3.3运行注释处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,我们一直在运行maven-processor-plugin作为一个单独的目标(在maven-compiler-plugin上使用 proc:none )。我们最终从maven 3.0.5升级到最新的3.3.3,我发现maven-processor-plugin基本上看起来已经死了。据我所知,它还没有从谷歌代码中迁移出来。

For years, we've been running the maven-processor-plugin as a separate goal (using proc:none on maven-compiler-plugin). We are finally upgrading from maven 3.0.5 to the latest 3.3.3, and I see that the maven-processor-plugin basically looks dead. As far I a can tell, it has not been migrated out of google code.

我们主要使用注释处理来生成匕首类。我不记得原因,但当时(在dagger-1中),我们得到的印象是在 generate-sources 和<$ c期间更好$ c> generate-test-sources 阶段而不是编译 test-compile ,这就是我们开始使用maven-processor-plugin的原因。请注意,我们希望所有这些都能在eclipse / m2e中很好地发挥。

We're using annotation processing primarily to generate dagger classes. I can't remember the reasons, but at the time (in dagger-1), we got the impression it is better to do this during generate-sources and generate-test-sources phases rather than during the compile and test-compile, which is why we used the maven-processor-plugin to begin with. Note that we want it all to play nicely in eclipse/m2e as well.

是否有一种新的更好的方法可以从对eclipse友好的maven运行注释处理?

Is there a new, better way to run annotation processing from maven that is eclipse-friendly?

推荐答案

您可以使用 maven-compiler-plugin 进行注释处理因为该功能存在于 javac 中。要在不同的阶段进行注释处理和常规编译,您可以对插件执行多次执行,一次使用注释处理开启,另一个关闭它。其配置如下:

You can use the maven-compiler-plugin for annotation processing because the functionality exists in javac. To do annotation processing and regular compilation in separate phases, you can do multiple executions of the plugin, one with annotation processing turned on, the other with it turned off. The configuration for that is as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <executions>
        <execution>
            <id>process-annotations</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <compilerArgs>
                    <arg>-proc:only</arg>
                    <arg>-processor</arg>
                    <arg>MyAnnotationProcessor</arg>
                </compilerArgs>
            </configuration>
        </execution>
        <execution>
            <id>default-compile</id> <!-- using an id of default-compile will override the default execution -->
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <compilerArgs>
                    <arg>-proc:none</arg>
                </compilerArgs>
            </configuration>
        </execution>
    </executions>
</plugin>

这篇关于如何通过maven 3.3运行注释处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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