Java 蚂蚁过滤 - 如果未设置属性则失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/226683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ant filtering - fail if property not set
提问by James Cooper
I've got a ant build.xml
that uses the <copy>
task to copy a variety of xml files. It uses filtering to merge in properties from a build.properties
file. Each environment (dev, stage, prod) has a different build.properties
that stores configuration for that environment.
我有一只蚂蚁build.xml
,它使用该<copy>
任务复制各种 xml 文件。它使用过滤来合并build.properties
文件中的属性。每个环境(dev、stage、prod)都有一个不同的build.properties
环境来存储该环境的配置。
Sometimes we add new properties to the Spring XML or other config files that requires updating the build.properties
file.
有时我们会向 Spring XML 或其他需要更新build.properties
文件的配置文件添加新属性。
I want ant to fail fast if there are properties missing from build.properties
. That is, if any raw @...@
tokens make it into the generated files, I want the build to die so that the user knows they need to add one or more properties to their local build.properties.
如果 .git 缺少属性,我希望蚂蚁快速失败build.properties
。也就是说,如果任何原始@...@
令牌进入生成的文件,我希望构建终止,以便用户知道他们需要向其本地 build.properties 添加一个或多个属性。
Is this possible with the built in tasks? I couldn't find anything in the docs. I'm about to write a custom ant task, but maybe I can spare myself the effort.
这可以通过内置任务实现吗?我在文档中找不到任何内容。我即将编写一个自定义的 ant 任务,但也许我可以省点力气。
Thanks
谢谢
采纳答案by Jason Day
You can do it in ant 1.7, using a combination of the LoadFile
task and the match
condition.
您可以在 ant 1.7 中使用LoadFile
任务和match
条件的组合来完成。
<loadfile property="all-build-properties" srcFile="build.properties"/>
<condition property="missing-properties">
<matches pattern="@[^@]*@" string="${all-build-properties}"/>
</condition>
<fail message="Some properties not set!" if="missing-properties"/>
回答by matt b
I was going to suggest that you attempt to use <property file="${filter.file}" prefix="filter">
to actually load the properties into Ant, and then fail
if any of them are not set, but I think I was interpreting your problem wrong (that you wanted to fail if a specified property was not set in the properties file).
我打算建议您尝试使用<property file="${filter.file}" prefix="filter">
将属性实际加载到 Ant 中,然后fail
如果其中任何一个未设置,但我认为我对您的问题的解释是错误的(如果未设置指定的属性,您希望失败在属性文件中)。
I think your best bet might be to use <exec>
to (depending on your dev platform) do a grep for the "@" character, and then set a property to the number of occurences found. Not sure of exact syntax but...
我认为您最好的选择可能是使用<exec>
(取决于您的开发平台)对“@”字符执行 grep,然后将属性设置为找到的出现次数。不确定确切的语法,但是...
<exec command="grep \"@\" ${build.dir} | wc -l" outputproperty="token.count"/>
<condition property="token.found">
<not>
<equals arg1="${token.count}" arg2="0"/>
</not>
</condition>
<fail if="token.found" message="Found token @ in files"/>
回答by matt b
if exec command is deprecated in your version of ant you can use redirectors, something like:
如果 exec 命令在您的 ant 版本中被弃用,您可以使用重定向器,例如:
<exec executable="grep">
<arg line="@ ${build.dir}"/>
<redirector outputproperty="grep.out"/>
</exec>
<exec executable="wc" inputstring="${grep.out}">
<arg line="-l"/>
<redirector outputproperty="token.found"/>
</exec>
to create the token.found property
创建 token.found 属性
<condition property="token.found">
<not>
<equals arg1="${token.count}" arg2="0"/>
</not>
</condition>
<fail if="token.found" message="Found token @ in files"/>
for the conditonal
对于有条件的
回答by dovetalk
If you are looking for a specific property, you can just use the fail task with the unless attribute, e.g.:
如果你正在寻找一个特定的属性,你可以使用带有除非属性的失败任务,例如:
<fail unless="my.property">Computer says no. You forgot to set 'my.property'!</fail>
<fail unless="my.property">Computer says no. You forgot to set 'my.property'!</fail>
Refer to the documentation for Ant's fail taskfor more detail.
有关更多详细信息,请参阅Ant 的失败任务的文档。
回答by Vadzim
Since Ant 1.6.2 condition
can also be nested inside fail
.
由于 Ant 1.6.2condition
也可以嵌套在fail
.
The following macro makes it easy to conditionally check multiple properties.
以下宏可以轻松地有条件地检查多个属性。
<macrodef name="required-property">
<attribute name="name"/>
<attribute name="prop" default="@{name}"/>
<attribute name="if" default="___"/>
<attribute name="unless" default="___"/>
<sequential>
<fail message="You must set property '@{name}'">
<condition>
<and>
<not><isset property="@{prop}"/></not>
<or>
<equals arg1="@{if}" arg2="___"/>
<isset property="@{if}"/>
</or>
<or>
<equals arg1="@{unless}" arg2="___"/>
<not><isset property="@{unless}"/></not>
</or>
</and>
</condition>
</fail>
</sequential>
</macrodef>
<target name="required-property.test">
<property name="prop" value=""/>
<property name="cond" value="set"/>
<required-property name="prop"/>
<required-property name="prop" if="cond"/>
<required-property name="prop" unless="cond"/>
<required-property name="prop" if="cond2"/>
<required-property name="prop" unless="cond2"/>
<required-property name="prop" if="cond" unless="cond"/>
<required-property name="prop" if="cond" unless="cond2"/>
<required-property name="prop" if="cond2" unless="cond"/>
<required-property name="prop" if="cond2" unless="cond2"/>
<required-property name="prop2" unless="cond"/>
<required-property name="prop2" if="cond2"/>
<required-property name="prop2" if="cond2" unless="cond"/>
<required-property name="prop2" if="cond" unless="cond"/>
<required-property name="prop2" if="cond2" unless="cond2"/>
<required-property name="success"/>
</target>