2014-09-03 62 views
2

我有一個註釋處理器,我需要給予一些配置以告訴它有關我如何生成源代碼的一些細節。我花了很多時間試圖瞭解爲什麼在構建之後文件位於target/classes中,但是在註釋處理期間我發現異常,說明該文件實際上不存在。需要來自src/main/resources中的文件註釋處理器配置的生成源階段配置

多挖後,我終於想通了,爲什麼文件(存儲在src/main/resources/config)不獲取複製到target/classes/config我的註釋處理器讀取 - generate-sources在構建生命週期process-resources之前發生,所以該文件沒有得到及時複製註釋處理器以在運行期間查看它。 (Maven構建生命週期參考:http://maven.apache.org/ref/3.2.2/maven-core/lifecycles.html

這裏是我想要做的一個高度概括:

我有我建立的,在處理註釋和生成接口類從信息的罐子基於客戶端API的註釋。這個想法是,將該jar作爲編譯時依賴項應該爲使用這些註釋的任何項目自動生成此代碼(在客戶端項目的pom.xml中儘可能少地添加其他配置)。

我如何去了解或者:

  1. 獲取(至少)處理資源的config.xml中部分之前發生的產生來源
  2. 添加文件到註解的類路徑處理器(我們不需要這個文件在輸出檔案中,所以這可能會更好)
  3. 我也打開其他(清潔)的方式來獲取配置信息到註釋處理器,如果有更好的我沒有想到的方式

如果可能,我寧願不寫一個完整的maven插件。

編輯: 這裏是我的客戶POM的每個請求的<build>部分的相關部分:

<build> 
    <finalName>OurApp</finalName> 
    <resources> 
     <resource> 
      <!-- My config.xml file is located here --> 
      <directory>src/main/resources</directory> 
     </resource> 
     <resource> 
      <directory>src/main/webapp</directory> 
      <includes> 
       <include>*.*</include> 
      </includes> 
      <excludes><exclude>${project.build.directory}/generated-sources/**</exclude></excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <!-- Omit Annotation Processor lib from the compilation phase because the code generated is destined for another, separate jar --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>${maven-compiler-plugin.version}</version> 
      <executions> 
       <execution> 
       <id>annotation-processing</id> 
       <phase>generate-sources</phase> 
       <goals> 
        <goal>compile</goal> 
       </goals> 
       <configuration> 
        <proc>only</proc> 
       </configuration> 
       </execution> 
       <!-- Compile the rest of the code in the normal compile phase --> 
       <execution> 
       <id>compile-without-generated-source</id> 
       <phase>compile</phase> 
       <goals> 
        <goal>compile</goal> 
       </goals> 
       <configuration> 
        <excludes><exclude>${project.build.directory}/generated-sources/**</exclude></excludes> 
        <proc>none</proc> 
        <!-- http://jira.codehaus.org/browse/MCOMPILER-230 because this doesn't 
         work in the opposite direction (setting failOnError in the other execution 
         and leaving the default top-level value alone) --> 
        <failOnError>true</failOnError> 
       </configuration> 
       </execution> 
      </executions> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
       <proc>only</proc> 
       <failOnError>false</failOnError> 
      </configuration> 
     </plugin> 
     <!-- package generated client into its own SOURCE jar --> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.4.1</version> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>generated-client-source</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      <dependencies> 
       <dependency> 
        <groupId>com.package</groupId> 
        <artifactId>our-api</artifactId> 
        <version>${our-api.version}</version> 
       </dependency> 
      </dependencies> 
      <executions> 
       <execution> 
        <id>client-source</id> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
+0

向您展示pom文件,否則很難猜測發生了什麼問題? – khmarbaise 2014-09-03 14:33:44

+0

查看編輯,添加pom.xml的相關部分和一些其他文檔 – StormeHawke 2014-09-03 15:06:35

+0

如果我理解你的請求正確,你有一個生成的部分和一個像往常一樣構建的其他部分。在這種情況下,您應該使用生成的代碼創建一個單獨的模塊,並使用包含通常部分的第二個模塊。這樣的事情的後果是創建一個多模塊構建。這使生活更輕鬆。除此之外,爲什麼你添加已經是默認的東西,如'src/main/resources'文件夾? 'src/main/webapp'不應該是資源的一部分,所以你有一個戰爭包裝和其他一些東西結合在一起。你違反了關注點的分離。 – khmarbaise 2014-09-04 07:44:23

回答

1

所以因爲這個文件只需要在編譯的時候,由於任何企圖直接從類路徑中直接繪製失敗,我決定將它放在項目根目錄下,然後在Maven中添加一個編譯器參數以指向該文件。

<compilerArgs> 
     <compilerArg>-AconfigFilePath=${project.basedir}/config.xml</compilerArg> 
    </compilerArgs> 

不太一樣優雅爲自動獲取其關閉的類路徑,但仍比提供所有配置元素作爲單獨的屬性更好。

感謝您的建議

相關問題