我想在编译前替换源文件(在我的例子中是*.java)中的令牌@NAME@。
我尝试使用谷歌替换插件,但我是开放的任何东西,这将有助于我。
pom.xml1.文件看起来像这样
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>src/main/java/com/test/sample/File.java</include>
</includes>
<replacements>
<replacement>
<token>@NAME@</token>
<value>New content</value>
</replacement>
</replacements>
</configuration>
</plugin>
但是当我运行mvn package后,输出是:
——replacer:1.5.3:替换(默认)@ MyProject——[INFO]替换运行0文件。
因为没有错误,我不知道我做错了什么。也许:
- 定义的相位错误
- 定义的include是错误的
- …
问候!
我认为有两个选择。
如果你继续使用插件,我认为你需要在include语句中添加${basedir}:
<include>${basedir}/src/main/java/com/test/sample/File.java</include>
如果你不想修改src/main中的文件,但要过滤文件并添加到构建中,你可以使用标准资源过滤和buildhelper插件将这些"生成的源"添加到构建中。
那么第一步将使用资源过滤来复制文件:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
然后使用http://www.mojohaus.org/build-helper-maven-plugin/将这些源代码添加到构建中。
如果你一直使用该文件夹,一些ide (IntelliJ)会自动识别/target/genereated-sources
(这不是标准的,但很常见)。如果你搜索"maven"one_answers"generated-sources",你会找到相当多的教程。
希望这对你有帮助
虽然这通常是您不应该首先做的事情,但有时您别无选择(在我的情况下,它是通过尽可能少地更改代码将旧项目"转换"为Maven)。上面的方法在某种程度上不起作用(虽然我可以替换源文件中的占位符并添加要编译的生成源文件夹,但它抱怨源文件重复)。然后我发现了一个更简单的方法,使用这里描述的template -maven-plugin http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html:
-
将带有占位符的文件放到/src/main/java-templates文件夹中。节选自我的源代码:
public static final String APPLICATION_VERSION = "r${project.version}";
-
将以下内容添加到您的插件部分:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>templating-maven-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <id>filter-src</id> <goals> <goal>filter-sources</goal> </goals> </execution> </executions> </plugin>