开发者

Valid <property> names in Ant

I'd like to set some properties in my ant build file, the names of which, are based on ant's build in properties. In particular, I'd like to set a property like:

<property name="${ant.project.name}.compiled" value="true" />

However, when I tried this the ${ant.project.ho开发者_JAVA技巧me} portion was not expanded.

Is it possible to use the value of properties as the names of other properties, and if so, how?


<property name="name.holder" value="iamholder" />
<property name="${name.holder}.flag" value="true" />
<echoproperties></echoproperties>

result:

[echoproperties] iamholder.flag=true

this is definitely valid ant code and the property iamholder.flag gets the value of true. If ${name.holder} does not get expanded, it means it has not been set yet (like if the first line in my sample was missing). Anyways, this still does not quite solve your problem, as you have pretty much no means of getting the value of this property as you don't know it's name and you can't do a nested resolve in pure ant. Depending on what you are trying to do it could still be useful to you though. This one would work (keep in mind, that until 1.8 the value is irrelevant as long as the property is set):

<target name="compile_stuff" unless="${name.holder}.flag">
  <echo>compiling...</echo>
</target>

To really get the value of such a property you have to use ant-contrib's propertycopy as suggested in one of the answers. That way you can get the value in a property whose name you know. Just make sure to do the trick just before use and set the override parameter to true (your post implies that you would be setting more properties like these, but without override your final property could not be changed). Another option for working with such properties is to use ant macros.


I think the only way is to echo your values to a .properties file and then load them back. However, you should ask yourself if you really need it; when I last used ant I tried to do the same thing but concluded I didn't really need to.

Is

$ant.project.home.compiled

not just as useful?


It can be done, a bit ugly, though. You need the < propertycopy > task from ant-contrib for this. The following shows an example

<property name="projectNameCompiled" value="${ant.project.name}.compiled" /> 
<property name="${projectNameCompiled}" value="true" /> 
<propertycopy property="final" from="${ant.project.name}.compiled" /> 

The property final contains the value true.


There are several ways to achieve that, see Ant FAQ
One possible solution via macrodef simulates the antcontrib / propertycopy task but doesn't need any external library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜