Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

java - How do I run annotation processing via maven 3.3?

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.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...