问题描述
我有一个属性文件,比如说 my-file.properties.除此之外,我的应用程序有几个配置文件,其中必须填写一些有关 my-file.properties 文件内容的信息.
I have a properties file, let say my-file.properties.In addition to that, I have several configuration files for my application where some information must be filled regarding the content of my-file.properties file.
my-file.properties:
my-file.properties:
application.version=1.0
application.build=42
user.name=foo
user.password=bar
因此,在我的配置文件中,我会发现一些 ${application.version}
, ${user.name}
将被它们的值所取代属性文件...
Thus, in my configuration files, I will find some ${application.version}
, ${user.name}
that will be replaced by their value taken in the properties file...
当我使用 Maven2 构建我的应用程序时,我只需要指定属性文件并说明我的资源文件已被过滤(如 这个答案 另一个问题).但是,我需要仅使用 Ant 来实现相同的目的.
When I build my application using Maven2, I only need to specify the properties file and say that my resources files are filtered (as in this answer to another problem). However, I need to achieve the same thing by using only Ant.
我已经看到 Ant 提供了一个过滤任务.但是,它迫使我使用模式 @property.key@
(即 @user.name@
而不是 #{user.name}
) 在我的配置文件中,这在我的情况下是不可接受的.
I've seen that Ant offers a filter task. However, it forces me to use the pattern @property.key@
(i.e. @user.name@
instead of #{user.name}
) in my configuration files, which is not acceptable in my case.
我该如何解决我的问题?
How can I solve my problem?
推荐答案
我认为 expandproperties 正是您要找的.这就像 Maven2 的资源过滤器.
I think expandproperties is what you are looking for. This acts just like Maven2's resource filters.
例如,如果您有 src 目录(许多文件之一):
For instance, if you have src directory (one of many files):
<link href="${css.files.remote}/css1.css"/>
src/test.txt
在我的 ANT 构建文件中,我们有:
<project default="default">
<!-- The remote location of any CSS files -->
<property name="css.files.remote" value="/css/theCSSFiles" />
...
<target name="ExpandPropertiesTest">
<mkdir dir="./filtered"/>
<copy todir="./filtered">
<filterchain>
<expandproperties/>
</filterchain>
<fileset dir="./src" />
</copy>
</target>
</project>
build.xml
*当您运行 ExpandPropertiesTest 目标时,您的 filtered 目录中将包含以下内容:*
*When you run the ExpandPropertiesTest target you will have the following in your filtered directory: *
<link href="/css/theCSSFiles/css1.css"/>
过滤/test.txt
这篇关于使用 Ant 模拟 Maven2 过滤机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!