Maven 2 用Ant模拟Maven2过滤机制
我有一个属性文件,比如my-file.properties。 除此之外,我还有几个应用程序配置文件,其中必须填写一些有关my-file.properties文件内容的信息 my-file.properties:Maven 2 用Ant模拟Maven2过滤机制,maven-2,ant,filter,Maven 2,Ant,Filter,我有一个属性文件,比如my-file.properties。 除此之外,我还有几个应用程序配置文件,其中必须填写一些有关my-file.properties文件内容的信息 my-file.properties: application.version=1.0 application.build=42 user.name=foo user.password=bar 因此,在我的配置文件中,我将找到一些${application.version},${user.name},它们将被属性文件中的值替
application.version=1.0
application.build=42
user.name=foo
user.password=bar
因此,在我的配置文件中,我将找到一些${application.version}
,${user.name}
,它们将被属性文件中的值替换
当我使用Maven2构建我的应用程序时,我只需要指定属性文件,并说我的资源文件已被过滤(如针对另一个问题)。然而,我需要通过只使用Ant来实现同样的目标
我看到蚂蚁提供了一个新的选择。但是,它迫使我在配置文件中使用模式@property.key@
(即@user.name@
,而不是{user.name}
),这在我的情况下是不可接受的
如何解决我的问题?Ant知道一个名为的概念,在这里很有用。使用-filter并将begintoken和endtoken指定为空(通常为“@”)。这应该可以解决问题。一个可行的方法是使用以下代码,该方法受Mnementh解决方案的启发:
<!-- Read the property file -->
<property file="my-file.properties"/>
<copy todir="${dist-files}" overwrite="true">
<fileset dir="${src-files}">
<include name="*.properties"/>
</fileset>
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<!-- Define the begin and end tokens -->
<param type="tokenchar" name="begintoken" value="$"/>
<param type="tokenchar" name="endtoken" value="}"/>
<!-- Define one token per entry in the my-file.properties. Arggh -->
<param type="token" name="{application.version" value="${application.version}"/>
<param type="token" name="{user.name" value="${user.name}"/>
...
</filterreader>
</filterchain>
</copy>
...
说明:
我正在使用ReplaceTokens读取器查找所有$…}
模式。我无法搜索${…}
模式,因为begintoken是字符而不是字符串。然后,我设置以{(即我看到的是{user.name
而不是user.name
)开头的令牌列表。希望我的-file.properties中“只有”大约20行,所以我需要在Ant文件中定义“只有”20个令牌
有没有简单而愚蠢的解决方案来解决这个简单而愚蠢的问题???您可以定义一个。因此,您有两个选择:
final String replaceWith = (String) hash.get(key.toString());
通过调用getReplacement()方法:
要使用此选项,请确保已打包类并修改过滤器:
<filterreader classname="my.custom.filters.ReplaceTokens">
<!-- Define the begin and end tokens -->
<param type="tokenchar" name="begintoken" value="$"/>
<param type="tokenchar" name="endtoken" value="}"/>
<!--Can still define explicit tokens, any not
defined explicitly will be replaced by the generic rule -->
</filterreader>
我想这就是你要找的。这就像。
输入 例如,如果您有src目录(许多文件中的一个):
src/test.txt
过程 在我的ANT构建文件中,我们有:
...
build.xml
输出 *当您运行ExpandPropertieTest目标时,您的筛选目录中将包含以下内容:*
<link href="/css/theCSSFiles/css1.css"/>
filtered/test.txt此解决方案似乎不起作用,至少在我的尝试中是这样。begintoken和endtoken不能为空。我不能将begintoken设置为“${”,因为这被认为是语法错误…@romaintaz当然!我希望这会简化构建文件。我知道它确实是我的。
<link href="${css.files.remote}/css1.css"/>
<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>
<link href="/css/theCSSFiles/css1.css"/>