2014-03-04 56 views
2

是否有方法可以跳過Maven中的生成源?如何跳過Maven中的生成源

通過命令行選項

+0

爲什麼你想這樣做?這個階段有什麼約束?顯示你的pom文件? – khmarbaise

+0

我不想重新創建源文件,因爲文件中沒有模式更改 – kuhajeyan

+0

只是一個示例,您可以使用類似的機制... –

回答

4

我已經場景,我產生CXF類時曾經我有在WSDL或WADL變化這樣做。因此,我會在需要時明確生成它。因此,我創建了一個獨立的配置文件一個新的配置文件cxf-gen以及我通常的dev,uat,syst。它有插件來生成類。總之,當我需要重新生成類時,我切換到配置文件並運行generate-sources。這裏是我使用的樣本配置文件。

<profiles> 
    <profile> 
     <id>dev</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <properties> 
      <envName>dev</envName> 
     </properties> 
    </profile> 
    <profile> 
     <id>uat</id> 
     <properties> 
      <envName>uat</envName> 
     </properties> 
    </profile> 

    <profile> 
     <id>jaxB-gen</id> 
     <properties> 
      <envName>dev</envName> 
     </properties> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.codehaus.mojo</groupId> 
        <artifactId>jaxb2-maven-plugin</artifactId> 
        <version>1.5</version> 
        <configuration> 
         <!-- CONFIGS -> 
        </configuration> 
        <executions> 
         <execution> 
          <id>xjc</id> 
          <goals> 
           <goal>xjc</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>code-gen</id> 
     <properties> 
      <envName>dev</envName> 
     </properties> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.cxf</groupId> 
        <artifactId>cxf-codegen-plugin</artifactId> 
        <version>${cxf.version}</version> 
        <executions> 
         <execution> 
          <id>generate-sources</id> 
          <phase>generate-sources</phase> 
          <configuration> 
           <!-- CONFIGS -> 
          </configuration> 
          <goals> 
           <goal>wsdl2java</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

       <!-- wadl2java Required only when JAXRS classes are to be generated --> 
       <plugin> 
        <groupId>org.apache.cxf</groupId> 
        <artifactId>cxf-wadl2java-plugin</artifactId> 
        <version>2.7.6</version> 
        <executions> 
         <execution> 
          <id>generate-sources</id> 
          <phase>generate-sources</phase> 
          <configuration> 
           <!-- CONFIGS -> 
          </configuration> 
          <goals> 
           <goal>wadl2java</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

       <plugin> 
        <groupId>com.googlecode.jsonschema2pojo</groupId> 
        <artifactId>jsonschema2pojo-maven-plugin</artifactId> 
        <version>0.3.7</version> 
        <configuration> 
         <!-- CONFIGS -> 
        </configuration> 
        <executions> 
         <execution> 
          <goals> 
           <goal>generate</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

      </plugins> 
     </build> 
    </profile> 
</profiles> 
+0

這幾乎是一個很好的答案。問題是它應該默認生成源代碼,而不是按需生成。否則,一個天真的清潔構建(mvn編譯)將被打破默認。好的文檔可以解決這個問題,但更好的解決它在POM中。 – marcv81

+0

答案雖然符合問題:「有沒有辦法在Maven中跳過生成源代碼?通過命令行選項執行它」。默認情況下它會被跳過,當你想通過命令行來做時,你可以像「mvn jaxb2:xjc -P jaxB-gen」 – Tristan

+0

@Tristan那樣在插件中有一個字段,你可以將它設置爲 $ {cxf .skip}然後從命令行中可以傳遞-Dcxf.skip = true –

1

這是一個古老的問題,雖然有些答案會以某種方式工作,但都不是理想的。

這個答案並不打破乾淨的構建:調用「mvn <目標>」仍然產生預期的和向後兼容的結果,這對於持續集成是有利的。此外,這個答案不依賴於將生成的代碼提交到版本控制,這是一個壞主意,因爲它可能會與源不同步。

我假設生成源階段綁定到插件目標。

答案是創建一個名爲「clean-build」的配置文件,默認情況下它是活動的幷包含您的插件綁定。當開發人員相信他們可以安全地跳過生成源時,他們可能會運行以下操作。

mvn -P !clean-build <goal> 

或者如果感嘆號需要轉義。

mvn -P \!clean-build <goal> 

這是pom.xml的樣子。

<profiles> 
    <profile> 
    <id>clean-build</id> 
    <activation> 
     <activeByDefault>true</activeByDefault> 
    </activation> 
    <build> 
     <plugins> 
     <plugin> 
      ... 
      <executions> 
      <execution> 
       ... 
       <phase>generate-sources</phase> 
       ... 
      </execution> 
      </executions> 
      ... 
     </plugin> 
     </plugins> 
    </build> 
    </profile> 
</profiles> 

此答案需要Maven 2.0.10+。