在Maven项目的构建过程中,对于Maven标准预定义变量,或者<properties>中定义的变量,都可以被实际的值替换。
但是,如果要在构建的过程中替换文件中的任何指定字符串(这里成为tokens),就需要使用Google为Maven提供的replacer插件。
Google的replacer插件原名为maven-replacer-plugin,2012年以后,为了遵循Maven的命名规则,被重命名为replacer,最新版本为April 16th, 2014发布的Version 1.5.3。
1.基本用法
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>说明:replacer插件只提供了一个goal,就是replace。
这里将replacer插件的执行加入到了prepare-package阶段。实际上,replacer插件默认被加入到compile阶段。当然可以配置加入到compile, test, prepare-package阶段。
这里,将index.jsp文件中的占位符%PROJECT_VERSION%,全部替换为${project.version}的实际值。
2. replacer插件的配置参数
- baseDir,
- outputDir,相对于baseDir的路径
- outpuBasedir,相对于执行目录的路径
- preserveDir,默认true
- quiet,默认false
- ignoreError,默认false
- maxReplacements,允许替换的文件的最大数量
3.配置示例
1) 替换一个文件中的一个token
<configuration>
<ignoreMissingFile>true</ignoreMissingFile>
<file>target/myapp/jsp/helloWorld.jsp</file>
<outputFile>target/myapp/jsp/helloWorld-updated.jsp</outputFile>
<regex>false</regex>
<token>$BUILD_NUMBER$</token>
<value>${buildNumber}</value>
</configuration>2) 使用正则表达式匹配一个文件中要替换的一个token
<configuration>
<file>target/someapp/jsp/helloWorld.jsp</file>
<token>ToKeN</token>
<value>value</value>
<regexFlags>
<regexFlag>CASE_INSENSITIVE</regexFlag>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
</configuration>3) 替换多个文件中的一个token
<configuration>
<includes>
<include>${project.build.directory}/myapp/target/**/replace-me.*</include>
</includes>
<excludes>
<exclude>${project.build.directory}/myapp/target/**/do-not-replace-me.*</exclude>
</excludes>
<token>TOKEN</token>
<value>VALUE</value>
</configuration>说明:<include>,<exclude>,在没有配置basedir的情况下,必须给出完整路径。
<configuration>
<basedir>${project.build.directory}/myapp/target</basedir>
<includes>
<include>**/replace-me.*</include>
</includes>
<excludes>
<exclude>**/do-not-replace-me.*</exclude>
</excludes>
<token>TOKEN</token>
<value>VALUE</value>
</configuration>4) 替换多个tokens
<configuration>
<file>target/classes/database.properties</file>
<replacements>
<replacement>
<token>token1</token>
<value>value1</value>
</replacement>
<replacement>
<token>token2</token>
<value>value2</value>
</replacement>
</replacements>
</configuration>5) 使用tokenValueMap给出一组要替换的tokens
<configuration>
<file>target/classes/database.properties</file>
<tokenValueMap>etc/${environment}/database.conf</tokenValueMap>
</configuration>4. WAR文件中的资源文件
WAR文件中的资源文件位于src/main/webapp路径下。在WAR文件的打包过程中,操作位于src/main/webapp路径下的Web resources有其特殊性。WAR文件的打包是通过maven-war-plugin实现的。但是maven-war-plugin不会将Web resources暴露给其他Maven插件的。必须明确调用maven-war-plugin的exploded,将Web resources提前复制出来,replacer插件替换后,maven-war-plugin再使用替换后的Web resources(位于target/路径下)打包WAR文件。配置如下:
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<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.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/${project.build.finalName}/WEB-INF/resources/**</file>
<token>TOKEN</token>
<value>VALUE</value>
</configuration>
</plugin>参考链接:
https://code.google.com/archive/p/maven-replacer-plugin/