插件主页: https://code.google.com/p/maven-replacer-plugin/
插件介绍:
maven-replacer-plugin:replacer is a build plugin to replace tokens within a file with a given value and fully supports regular expressions.
插件配置和使用示例: https://code.google.com/p/maven-replacer-plugin/wiki/UsageGuide
写这篇文章时的最新版本为:1.5.3
<groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.3</version>在pom.xml中添加此插件:
<build> <plugins> ... <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.3</version> <executions> ... </executions> <configuration> ... </configuration> </plugin> </plugins> </build>
然后就是增加 executions 和 configuration 配置。
先看executions。在executions里最主要的就是配置 phase 和 goal 了。
phase可以是任意一个phase,典型的有compile, test, package, prepare-resources等等;goal 在replacer插件里只有一个固定的值 replace 。
添加完 executions 后,插件定义看起来如下:
<build> <plugins> <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> ... </configuration> </plugin> </plugins> </build>
下面定义configuration部分。
根据不同的意图,configuration部分需要不同的定义,下面将一个一个举例。
单个文件输入输出、单个替换
例如有一个文件 a.txt ,里面有内容: This book names {book.name}
想要用真实的内容替换 {book.name},configuration的配置如下:
<configuration> <file>src/test/resources/a.txt</file> <outputFile>src/main/resources/a.txt</outputFile> <regex>false</regex> <token>{book.name}</token> <value>Thinkin in Java</value> </configuration>
参数说明如下:
- file: 指定输入文件
- outputFile: 指定输出文件
- regex: 指定token是否是正则表达式,默认值为true (但是我发现如果不指定regex配置,没有结果,很奇怪,所以必须显式的指定regex)
- token: 要替换的值
- value: 替换后的值