为maven-processor-plugin编写注释处理器

Ral*_*lph 8 java maven-2 maven-3 maven annotation-processing

我有兴趣为maven-processor-plugin编写注释处理器.我对Maven比较新.

在项目路径中应该处理器Java源代码的位置(例如:src/main/java/...)以便它被适当地编译,但不会最终作为我的工件JAR文件的一部分?

Sea*_*oyd 9

最简单的方法是将注释处理器保存在作为依赖项包含的单独项目中.

如果这对您不起作用,请使用此配置

编译器插件:

<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>
Run Code Online (Sandbox Code Playgroud)

处理器插件:

<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>
Run Code Online (Sandbox Code Playgroud)

(请注意,这必须在两次编译运行之间执行,因此在上面的maven-compiler-plugin配置之后将此代码放在pom.xml中是很重要的)

Jar插件:

<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>
Run Code Online (Sandbox Code Playgroud)