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

时间:2011-03-14 15:47:30

标签: java maven-2 maven maven-3 annotation-processing

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

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

1 个答案:

答案 0 :(得分: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>

处理器插件:

<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>

(请注意,这必须在两次编译运行之间执行,因此在上面的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>