Dynamic property names in ant

2019-08-01 04:07发布

问题:

I am reading a file in ant and loading the properties through loadproperties. I am interested in using the value of a specific property, whose name is not known. I know that it follows a pattern because that is how I load the property.

I can echoproperties and see that it is being loaded.

But I dont know how to access its value, given that its name is actually a pattern rather that hardcoded.

How can I access this property's value to do some processing.

I hope this is clear. Please ask if I need to clarify some more.

回答1:

Take a look at ant-contrib package. Its propertycopy task will do what you need. If you need to resolve an arbitrary number of properties following an established pattern, you would use ant-contrib's propertycopy in conjunction with ant-contribs "for" task.

http://ant-contrib.sourceforge.net/tasks/tasks/index.html



回答2:

You should use Ant's script task.

I suggest using the beanshell script since it is pure java. For example, to print all properties for your project, use the following:

 <target name="echoprops">
    <script language="beanshell">
        System.out.println("All Properties: " + project.getProperties().keySet());
    </script>
 </target>

It should be easy to modify the above script to get the property you want.

To use this task, you will need to run the following in $ANT_HOME first:

ant -f fetch.xml script -Ddest=user

That will download all required optional jars to ~/.ant/lib .