2016-08-23 22 views
2

我試圖用maven-replacer-plugin來替換我的web.xml中的標記,當它在WAR文件中生成但不在源文件中時,它會刪除後續標記構建並顯示相對於版本控制存儲庫已更改的文件。maven-replacer-plugin替換構建中的標記而不是源

目前,我只能夠更改源文件,這不符合我的要求:

<plugin> 
    <groupId>com.google.code.maven-replacer-plugin</groupId> 
    <artifactId>replacer</artifactId> 
    <version>1.5.2</version> 
    <executions> 
     <execution> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>replace</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <file>${project.basedir}/src/main/webapp/WEB-INF/web.xml</file> 
     <replacements> 
      <replacement> 
       <token>@@[email protected]@</token> 
       <value>local</value> 
      </replacement> 
     </replacements> 
    </configuration> 
</plugin> 

問題:我怎樣才能運行代用品只更改文件中的WAR封裝,同時將源保持不變,以供後續構建使用?

回答

3

您可以使用maven-war-pluginexploded目標得到一個臨時文件夾(如target實際上下創建的任何)什麼將在後面作最後war文件的一部分分解的版本,然後執行對這個replacer插件文件(一個安全的副本,與其他使用該文件的插件不衝突)。

這種做法實際上也是由official replacer plugin doc

有書面文件,具有類似配置:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.6</version> 
    <configuration> 
     <useCache>true</useCache> 
    </configuration> 
    <executions> 
     <execution> 
      <id>prepare-war</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>exploded</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

<plugin> 
    <groupId>com.google.code.maven-replacer-plugin</groupId> 
    <artifactId>replacer</artifactId> 
    <version>1.5.2</version> 
    <executions> 
     <execution> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>replace</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <file>${project.build.directory}/${project.build.finalName}/WEB-INF/web.xml</file> 
     <token>@@[email protected]@</token> 
     <value>local</value> 
    </configuration> 
</plugin> 

注:replacer文件還建議使用useCache選項,它應防止插件覆蓋以前創建的exploded目標。但是,這個選項並不適合這個目的。


同樣,下面的方法將代替根據我的測試工作:

  • 使用maven-war-pluginexploded目標target下,共創未來戰爭文件的一個<war_name>-tmp目錄的臨時副本:這不是一個問題,無論如何target應該通過clean指令無論如何被丟棄
  • 配置replacer插件,以取代0的副本文件
  • 配置使用其webXml選項指向web.xml文件的最後war文件的默認war目標

下將適用上述方法:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.6</version> 
    <executions> 
     <execution> 
      <!-- explode the future war content for pre-package processing --> 
      <id>prepare-war</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>exploded</goal> 
      </goals> 
      <configuration> 
       <webappDirectory>${project.build.directory}/${project.build.finalName}-tmp</webappDirectory> 
      </configuration> 
     </execution> 
     <execution> 
      <!-- use the same execution id to further configure the default binding and execution --> 
      <id>default-war</id> 
      <phase>package</phase> 
      <goals> 
       <goal>war</goal> 
      </goals> 
      <configuration> 
       <!-- during the package phase, use the processed web.xml file --> 
       <webXml>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</webXml> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

<plugin> 
    <groupId>com.google.code.maven-replacer-plugin</groupId> 
    <artifactId>replacer</artifactId> 
    <version>1.5.2</version> 
    <executions> 
     <execution> 
      <!-- apply pre-package processing on web resources --> 
      <id>process-web-resources</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>replace</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <file>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</file> 
     <token>@@[email protected]@</token> 
     <value>local</value> 
    </configuration> 
</plugin> 
+0

我已經有我的構建聲明中的'maven-war-plugin'的插件聲明。我應該修改現有的添加執行還是需要一個全新的聲明? – amphibient

+0

你可以在同一個插件聲明中添加額外的執行器,也請注意我剛添加的最新更新的全局配置 –

+0

我的'$ {project.build.directory}/$ {project.build.finalName}/WEB-INF/web .xml'不存在。我的'target'目錄只包含'classes','generated-sources'和'test-classes'。 IOW,我無法在目標中找到web.xml – amphibient

相關問題