2011-03-14 36 views
8

我有興趣爲maven-processor-plugin編寫註釋處理器。我對Maven相對來說比較陌生。爲maven-processor-plugin編寫註釋處理器

在項目路徑中處理器Java源代碼應該去哪裏(例如:src/main/java/...),以便它得到適當編譯,但最終不會作爲我的工件JAR文件的一部分?

回答

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> 

(請注意,這必須是在兩次編譯運行之間執行,所以你必須將這段代碼放在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>