Writing an annotation processor for maven-processor-plugin

后端 未结 1 1764
广开言路
广开言路 2021-01-05 03:37

I am interested in writing an annotation processor for the maven-processor-plugin. I am relatively new to Maven.

Where in the project path should the processor Java

相关标签:
1条回答
  • 2021-01-05 03:52

    The easiest way is to keep your annotation processor in a separate project that you include as dependency.

    If that doesn't work for you, use this config

    Compiler Plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.5</source>
            <target>1.5</target>
        </configuration>
        <inherited>true</inherited>
        <executions>
            <execution>
                <id>default-compile</id>
                <inherited>true</inherited>
                <configuration>
                    <!-- limit first compilation run to processor -->
                    <includes>path/to/processor</includes>
                </configuration>
            </execution>
            <execution>
                <id>after-processing</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
                <inherited>false</inherited>
                <configuration>
                    <excludes>path/to/processor</excludes>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Processor Plugin:

    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <executions>
            <execution>
                <id>process</id>
                <goals>
                    <goal>process</goal>
                </goals>
                <phase>compile</phase>
                <configuration>
                    <processors>
                        <processor>com.yourcompany.YourProcessor</processor>
                    </processors>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    (Note that this must be executed between the two compile runs, so it is essential that you place this code in the pom.xml after the above maven-compiler-plugin configuration)

    Jar Plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
            <excludes>path/to/processor</excludes>
        </configuration>
        <inherited>true</inherited>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题