Maven:在编译之前替换源文件中的标记 [英] Maven: Replace token in source file before compilation

查看:952
本文介绍了Maven:在编译之前替换源文件中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译之前在源文件(在我的情况下是* .java)中替换令牌@ NAME @。

I want to replace a token @NAME@ in a source file (in my case *.java) before compilation.

我尝试使用google replacer插件但是我愿意接受任何有助于我的事情。

I try to use google replacer plugin but I am open for anything which will help me.

1.pom.xml
pom文件看起来像这样

1.pom.xml The pom file look like this

<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包之后输出是:

But after I run mvn package the output is:


--- replacer:1.5.3:replace(默认)@ MyProject --- [INFO]替换在0档上运行。

--- replacer:1.5.3:replace (default) @ MyProject --- [INFO] Replacement run on 0 file.

因为没有错误我不知道我做错了什么。
可能:

Because there is no error I do not know what I have done wrong. Maybe:


  1. 定义阶段错误

  2. 定义包含错误

  3. ...

问候!

推荐答案

我认为有两种选择。

如果你继续使用插件,我认为你需要添加$ {basedir}到包含声明:

If you keep using the plugin I think you need to add the ${basedir} to the include statement:

<include>${basedir}/src/main/java/com/test/sample/File.java</include>

如果你不想修改src / main中的文件但过滤文件并将其添加到你可以使用标准资源过滤和buildhelper插件将这些生成的源添加到构建中。

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.

因此,第一步将使用资源过滤来复制file: http://maven.apache.org/plugins/maven -resources-plugin / examples / filter.html

So step one would be using resource filtering to copy the file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

然后使用 http://www.mojohaus.org/build-helper-maven-plugin/ 将这些源添加到构建中。

And then use the http://www.mojohaus.org/build-helper-maven-plugin/ to add those sources to the build.

如果你继续使用那个文件夹(它不是标准但很常见),某些IDE(IntelliJ)会自动识别 / target / genereated-sources 。如果你搜索maven和generated-sources,你会发现很多教程。

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.

希望这会有所帮助:)

这篇关于Maven:在编译之前替换源文件中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆