使用 ANT 替换基于属性文件的所有令牌

发布于 2024-10-08 21:39:58 字数 603 浏览 0 评论 0原文

我很确定这是一个简单的问题,而且我之前也见过这个问题,但没有可靠的答案。

我有几个用于不同环境的属性文件,即 xxxx-dev、xxxx-test、xxxx-live

属性文件包含类似以下内容:

server.name=dummy_server_name
server.ip=127.0.0.1

我使用的模板文件看起来像:

<...>
   <server name="@server.name@" ip="@server.ip@"/>
</...>

上面是一个非常原始的示例,但我想知道是否有一种方法可以告诉 ANT 根据属性文件替换所有令牌,而不是必须为每个令牌行硬编码...即

<replacetokens>
   <token key="server.name" value="${server.name}"/>
   <token key="server.ip" value="${server.ip}"/>
</replacetokens>

任何帮助都会很棒!

I'm pretty sure this is a simple question to answer and ive seen it asked before just no solid answers.

I have several properties files that are used for different environments, i.e xxxx-dev, xxxx-test, xxxx-live

The properties files contain something like:

server.name=dummy_server_name
server.ip=127.0.0.1

The template files im using look something like:

<...>
   <server name="@server.name@" ip="@server.ip@"/>
</...>

The above is a really primitive example, but im wondering if there is a way to just tell ANT to replace all tokens based on the properties file, rather than having to hardcode a token line for each... i.e

<replacetokens>
   <token key="server.name" value="${server.name}"/>
   <token key="server.ip" value="${server.ip}"/>
</replacetokens>

Any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-10-15 21:39:58

您可以指定属性文件,从中读取 'replace' 任务< /a> using replacefilterfile

<replace file="input.txt" replacefilterfile="properties.txt"/>

同样,在过滤器链中,您可以使用 “替换令牌”propertyfile

这将处理每个属性文件
Sample.properties 中的条目作为
令牌/密钥对:

<loadfile srcfile="${src.file}" property="${src.file.replaced}">
  <filterchain>
    <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
      <param type="propertiesfile" value="sample.properties"/>
    </filterreader>
  </filterchain>
</loadfile>

You can specify the properties file from which to read the list of tokens for the 'replace' task using replacefilterfile:

<replace file="input.txt" replacefilterfile="properties.txt"/>

Similarly, in a filter chain, you can use 'replacetokens' propertyfile:

This will treat each properties file
entry in sample.properties as a
token/key pair:

<loadfile srcfile="${src.file}" property="${src.file.replaced}">
  <filterchain>
    <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
      <param type="propertiesfile" value="sample.properties"/>
    </filterreader>
  </filterchain>
</loadfile>
阿楠 2024-10-15 21:39:58

通过替换任务本身,我错过了标记周围的 @ 分隔符,因此我提出了以下解决方案。您可以将模板文件中的任何 ant 属性

<project name="replace" default="replace">

<property file="build.properties" />
<target name="replace">

    <!-- create temp file with properties -->
    <tempfile property="temp.replace" suffix=".properties"/>
    <echoproperties destfile="${temp.replace}" />
    <!-- replace name=value with @name@=value -->
    <replaceregexp file="${temp.replace}" match="([^=]*)=" replace="@\1@=" byline="true" />

    <!-- copy template and replace properties -->
    <copy file="template.txt" tofile="replaced.txt" />
    <replace file="replaced.txt" replacefilterfile="${temp.replace}" />

</target>

与模板

ANT home @ant.home@
ANT version @ant.java.version@
server name @server.name@ ip @server.ip@

一起使用,这会导致

ANT home /usr/share/ant
ANT version 1.7
server name dummy_server_name ip 127.0.0.1

With the replace task by itself I missed the @ delimiters around tokens so I came up with the following solution. You can use any ant property in the template file

<project name="replace" default="replace">

<property file="build.properties" />
<target name="replace">

    <!-- create temp file with properties -->
    <tempfile property="temp.replace" suffix=".properties"/>
    <echoproperties destfile="${temp.replace}" />
    <!-- replace name=value with @name@=value -->
    <replaceregexp file="${temp.replace}" match="([^=]*)=" replace="@\1@=" byline="true" />

    <!-- copy template and replace properties -->
    <copy file="template.txt" tofile="replaced.txt" />
    <replace file="replaced.txt" replacefilterfile="${temp.replace}" />

</target>

with a template

ANT home @ant.home@
ANT version @ant.java.version@
server name @server.name@ ip @server.ip@

this results in

ANT home /usr/share/ant
ANT version 1.7
server name dummy_server_name ip 127.0.0.1
我为君王 2024-10-15 21:39:58

使用文件集表单ant-contrib 您可以读取令牌表单属性文件并替换多个文件中的多个令牌。

<project name="MyProject" default="replaceToklens" basedir=".">
    <property name="profilesProperties" value="${basedir}/environment.properties" />
    <property name="build.dir" location="build"/>

    <!-- File to Load/ Accessable -->
    <property file="${profilesProperties}" /> 
    <target name="replaceToklens">
        <taskdef resource="net/sf/antcontrib/antlib.xml">
            <classpath>
                <pathelement location="${basedir}/ant-contrib-1.0b3.jar" />
            </classpath>
        </taskdef>
            <mkdir dir="${build.dir}"/>
            <filter filtersfile="${profilesProperties}" />
            <copy todir="${build.dir}" filtering="true" overwrite="true">
                <fileset dir="${basedir}"> <!-- target/MyProject -->
                    <include name="*.xml" />
                    <exclude name="build.xml" />
                </fileset>
            </copy>
    </target>
</project>

文件夹结构:

ANT
 \_ build.xml
 \_ environment.properties
 \_ server.xml
 \_ build
        \_ server.xml   [replaced with token value]

为了替换单个令牌,请使用以下内容:

<replace file="build/server.xml" token="@keyName@" value="${keyValue}" />

Using fileset form ant-contrib you can read the tokens form properties file and replace multiple tokens over multiple files.

<project name="MyProject" default="replaceToklens" basedir=".">
    <property name="profilesProperties" value="${basedir}/environment.properties" />
    <property name="build.dir" location="build"/>

    <!-- File to Load/ Accessable -->
    <property file="${profilesProperties}" /> 
    <target name="replaceToklens">
        <taskdef resource="net/sf/antcontrib/antlib.xml">
            <classpath>
                <pathelement location="${basedir}/ant-contrib-1.0b3.jar" />
            </classpath>
        </taskdef>
            <mkdir dir="${build.dir}"/>
            <filter filtersfile="${profilesProperties}" />
            <copy todir="${build.dir}" filtering="true" overwrite="true">
                <fileset dir="${basedir}"> <!-- target/MyProject -->
                    <include name="*.xml" />
                    <exclude name="build.xml" />
                </fileset>
            </copy>
    </target>
</project>

folder structure:

ANT
 \_ build.xml
 \_ environment.properties
 \_ server.xml
 \_ build
        \_ server.xml   [replaced with token value]

In order to replace single toke use the following:

<replace file="build/server.xml" token="@keyName@" value="${keyValue}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文