我想在编译之前在源文件(在我的情况下是*.java)中替换令牌@ NAME @.
我尝试使用谷歌替换插件,但我愿意接受任何有助于我的东西.
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>
Run Code Online (Sandbox Code Playgroud)
但是在我运行mvn包后输出是:
--- replacer:1.5.3:replace(默认)@MyProject --- [INFO]替换在0文件上运行.
因为没有错误我不知道我做错了什么.也许:
问候!
我认为有两种选择.
如果你继续使用插件,我认为你需要将$ {basedir}添加到include语句中:
<include>${basedir}/src/main/java/com/test/sample/File.java</include>
Run Code Online (Sandbox Code Playgroud)
如果您不想修改src/main中的文件但过滤该文件并将其添加到构建中,则可以使用标准资源过滤和buildhelper插件将这些"生成的源"添加到构建中.
因此,第一步将使用资源过滤来复制文件:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
然后使用http://www.mojohaus.org/build-helper-maven-plugin/将这些源添加到构建中.
/target/genereated-sources如果您继续使用该文件夹,某些IDE(IntelliJ)将自动识别(它不是标准但很常见).如果你搜索"maven"和"generated-sources",你会发现很多教程.
希望这可以帮助 :)
虽然这是您通常不应该首先做的事情,但有时您别无选择(在我的例子中,它是将旧项目“转换”为 Maven,并尽可能少地更改代码)。上面的方法在某种程度上不起作用(虽然我可以替换源文件中的占位符并添加要编译的生成源文件夹,但它抱怨重复的源文件)。然后我找到了一种更简单的方法,使用 templatating-maven-plugin,如下所述http://www.mojohaus.org/templated-maven-plugin/examples/source-filtering.html:
将带有占位符的文件放入文件夹 /src/main/java-templates 中。摘自我的源代码:
public static final String APPLICATION_VERSION = "r${project.version}";
Run Code Online (Sandbox Code Playgroud)将以下内容添加到 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>
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
15767 次 |
| 最近记录: |