java Maven:在编译前替换源文件中的令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32198345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven: Replace token in source file before compilation
提问by Michael S
I want to replace a token @NAME@ in a source file (in my case *.java) before compilation.
我想在编译之前替换源文件(在我的情况下为 *.java)中的标记 @NAME@。
I try to use google replacer plugin but I am open for anything which will help me.
我尝试使用谷歌替换插件,但我愿意接受任何对我有帮助的东西。
1.pom.xml The pom file look like this
1.pom.xml pom文件长这样
<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>
But after I run mvn package the output is:
但是在我运行 mvn package 后,输出是:
--- replacer:1.5.3:replace (default) @ MyProject --- [INFO] Replacement run on 0 file.
--- replacer:1.5.3:replace (default) @ MyProject --- [INFO] 替换在 0 文件上运行。
Because there is no error I do not know what I have done wrong. Maybe:
因为没有错误我不知道我做错了什么。或许:
- Defined phase is wrong
- Defined include is wrong
- ...
- 定义的相位错误
- 定义的包含错误
- ...
Greetings!
问候!
采纳答案by wemu
I think there are two options.
我认为有两种选择。
If you keep using the plugin I think you need to add the ${basedir} to the include statement:
如果您继续使用该插件,我认为您需要将 ${basedir} 添加到 include 语句中:
<include>${basedir}/src/main/java/com/test/sample/File.java</include>
If you dont want to modify the file in src/main but filter the file and add that one to the build you can use the standard resource filtering and the buildhelper plugin to add those "generated sources" to the build.
如果您不想修改 src/main 中的文件而是过滤文件并将该文件添加到构建中,您可以使用标准资源过滤和 buildhelper 插件将那些“生成的源”添加到构建中。
So step one would be using resource filtering to copy the file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
所以第一步是使用资源过滤来复制文件:http: //maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
And then use the http://www.mojohaus.org/build-helper-maven-plugin/to add those sources to the build.
然后使用http://www.mojohaus.org/build-helper-maven-plugin/将这些源添加到构建中。
Some IDEs (IntelliJ) will recognize /target/genereated-sources
automatically if you keep using that folder (its not standard but very common). If you search for "maven" and "generated-sources" you will find quite some tutorials.
/target/genereated-sources
如果您继续使用该文件夹(它不是标准但很常见),某些 IDE(IntelliJ)会自动识别。如果您搜索“maven”和“generated-sources”,您会发现很多教程。
Hope this helps :)
希望这可以帮助 :)
回答by stefitz
While this is something you usually should not do in the first place, sometimes you have no choice (in my case it was "converting" an old project to Maven with changing as little of the code as possible). The above somehow did not work (while I could replace a placeholder in the source file and add the generated-sources folder to be compiled, it complained about duplicate source files). Then I found an easier way by using the templating-maven-plugin as described here http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html:
虽然这是您通常不应该首先做的事情,但有时您别无选择(在我的情况下,它是将旧项目“转换”为 Maven,并尽可能少地更改代码)。以上不知何故不起作用(虽然我可以替换源文件中的占位符并添加要编译的生成源文件夹,但它抱怨重复的源文件)。然后我通过使用http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html 中描述的 templating-maven-plugin 找到了一种更简单的方法:
Put the file with the placeholder in the folder /src/main/java-templates. Excerpt from my source code:
public static final String APPLICATION_VERSION = "r${project.version}";
Add the following to your pom's plugins section:
<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>
将带有占位符的文件放在文件夹 /src/main/java-templates 中。摘自我的源代码:
public static final String APPLICATION_VERSION = "r${project.version}";
将以下内容添加到您的 pom 插件部分:
<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>