当我的 web.xml
构建在 WAR 文件中而不是源代码中时,我尝试使用 maven-replacer-plugin
来替换它,这将删除后续构建的 token 并显示文件相对于版本控制存储库的更改。
目前我只能更改源中的文件,这不符合我的要求:
<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>@@sec.level@@</token>
<value>local</value>
</replacement>
</replacements>
</configuration>
</plugin>
问题:如何运行替换程序以仅更改 WAR 包中的文件,同时在后续构建中保持源代码不变?
最佳答案
您可以使用 exploded
maven-war-plugin
的目标进入临时文件夹(实际上就像 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>@@sec.level@@</token>
<value>local</value>
</configuration>
</plugin>
注意:replacer
文档还建议使用 useCache
选项应该阻止插件覆盖 exploded
的内容之前创建的目标。但是,该选项并不真正适合此目的。
同样,根据我的测试,以下方法将有效:
- 使用
exploded
maven-war-plugin
的目标在<war_name>-tmp
中创建 future war 文件的临时副本target
下的目录:这不是问题,无论target
下的内容如何应该通过clean
被丢弃无论如何命令 - 配置
replacer
插件来替换web.xml
的副本文件 - 配置默认
war
使用其webXml
来实现目标指向该web.xml
的选项文件为最终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>@@test@@</token>
<value>local</value>
</configuration>
</plugin>
关于maven-replacer-plugin 用于替换构建中的 token 而不是源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39105557/