2016-08-23 24 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

相关问题