ANT:复制文本文件的某些部分
ANT: copy certain parts of a text file
我有一个包含多个属性的 属性 文件。多个对多个(我们的)产品有效,有些仅对一个产品有效(无法通过 属性 名称区分)。因此,在一个产品的基于 ANT 的构建过程中,我想将包含所有属性的原始文件复制到产品特定文件中,跳过适用于其他产品的部分。我可以想象使用一些开始和结束标记,例如
foo.bar=hello
# begin-product1
foo.bazz=world
# end-product1
# begin-product2
woohoo.bart=bla-bla
# end-product2
对于产品 1 我想获取文件
foo.bar=hello
foo.bazz=world
以及产品 2
foo.bar=hello
woohoo.bart=bla-bla
ANT 可以实现类似的功能吗?还是我应该编写自己的 Java 助手 class?
您可以以此作为 "vanilla Ant" 起点并根据您的需要进行调整。
此处假设您希望一次处理一个产品,并根据产品编号将该产品的属性加载到当前版本中。
方法是实现一个 macro 来执行此操作。提供的属性是 属性 文件名和产品编号。宏读取文件两次,分别提取公共部分和产品特定部分,然后将这些部分连接起来并作为 Ant 属性加载。
您可以调整此宏,例如,获取产品所需的文件片段并写出特定于产品的 属性 文件(使用 Ant <echo>
任务)。如果需要,还可以将分隔各个部分的字符串抽象为宏的属性或参数。在示例中,我在传递给 <loadproperties>
任务的特定于产品的字符串中包含了 begin/end 标记。
<macrodef name="loadProductProperties">
<attribute name="propertiesFile" />
<attribute name="product" />
<sequential>
<local name="config.common" />
<local name="config.product" />
<loadfile property="config.common" srcFile="@{propertiesFile}">
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex pattern="^(.*?)# begin-product.*" replace="" flags="s" />
</tokenfilter>
</filterchain>
</loadfile>
<loadfile property="config.product" srcFile="props.txt">
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex
pattern="^.*(# begin-product@{product}\b.*?# end-product@{product}\b).*"
replace="" flags="s" />
</tokenfilter>
</filterchain>
</loadfile>
<loadproperties>
<string value="${config.common}${config.product}" />
</loadproperties>
</sequential>
</macrodef>
<loadProductProperties propertiesFile="props.txt" product="2" />
我有一个包含多个属性的 属性 文件。多个对多个(我们的)产品有效,有些仅对一个产品有效(无法通过 属性 名称区分)。因此,在一个产品的基于 ANT 的构建过程中,我想将包含所有属性的原始文件复制到产品特定文件中,跳过适用于其他产品的部分。我可以想象使用一些开始和结束标记,例如
foo.bar=hello
# begin-product1
foo.bazz=world
# end-product1
# begin-product2
woohoo.bart=bla-bla
# end-product2
对于产品 1 我想获取文件
foo.bar=hello
foo.bazz=world
以及产品 2
foo.bar=hello
woohoo.bart=bla-bla
ANT 可以实现类似的功能吗?还是我应该编写自己的 Java 助手 class?
您可以以此作为 "vanilla Ant" 起点并根据您的需要进行调整。
此处假设您希望一次处理一个产品,并根据产品编号将该产品的属性加载到当前版本中。
方法是实现一个 macro 来执行此操作。提供的属性是 属性 文件名和产品编号。宏读取文件两次,分别提取公共部分和产品特定部分,然后将这些部分连接起来并作为 Ant 属性加载。
您可以调整此宏,例如,获取产品所需的文件片段并写出特定于产品的 属性 文件(使用 Ant <echo>
任务)。如果需要,还可以将分隔各个部分的字符串抽象为宏的属性或参数。在示例中,我在传递给 <loadproperties>
任务的特定于产品的字符串中包含了 begin/end 标记。
<macrodef name="loadProductProperties">
<attribute name="propertiesFile" />
<attribute name="product" />
<sequential>
<local name="config.common" />
<local name="config.product" />
<loadfile property="config.common" srcFile="@{propertiesFile}">
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex pattern="^(.*?)# begin-product.*" replace="" flags="s" />
</tokenfilter>
</filterchain>
</loadfile>
<loadfile property="config.product" srcFile="props.txt">
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex
pattern="^.*(# begin-product@{product}\b.*?# end-product@{product}\b).*"
replace="" flags="s" />
</tokenfilter>
</filterchain>
</loadfile>
<loadproperties>
<string value="${config.common}${config.product}" />
</loadproperties>
</sequential>
</macrodef>
<loadProductProperties propertiesFile="props.txt" product="2" />