How can I reverse the elements of a Path in Ant? - ant

I have a Path collection in Ant, and I need to loop through it in reverse order.
I use AntContrib's for loop, like this:
<for param="foo">
<path refid="bar" />
<sequential> ... </sequantial>
</for>
I need to loop through the elements of bar in reverse order. I can't change how the Path got created in the first place. I could always write a custom Ant task in Java, but my build currently runs without any custom tasks, and I'd rather avoid that for such a seemingly simple task.
Would Ant JavaScript be able to do this ? (and if so, how?)
Any help would be appreciated!

I think a "pure Ant" solution might be a bit contorted, but you could use a script task as you suggest.
This will set a property baz that contains the reverse of the path with reference id bar.
<script language="javascript"><![CDATA[
project.setProperty( "baz", project.getReference( "bar" )
.toString().split( ":" ).reverse( ).join( ":" ) );
]]></script>
Hopefully can be adapted to your precise needs.

Create a variable ("var" type in Ant Contrib) to store the path elements in the reverse order.
Then, use a for loop with your path as a nested path, use "equals" in a condition to check whether your variable is empty or not.
After that, if it is, just copy the value of the for loop parameter into your variable, otherwise copy the value of the for loop parameter into your variable followed by the delimiter and the value of variable (i.e make a concatenation).
Finally, you can just use the created variable in a for loop by using the "list" attribute and the "delimiter" attribute.
You can see what I mean in my own source code here.

Related

ANT Reading property from property files, using an ANT property

simple question for you..
I have a property file with a value like this
CommercialManager=MOT
CommercialUser=AT
CommercialAdmin=POT
I'm calling an Ant Script from Jenkins, passing some variables..
some of these variables are used to get a dynamic property from the property file..
I'm saying that if i select into the jenkins job the CommercialAdmin variable from a select list i want to get the property with that name.
The value selected into the Jenkins JOB is set inside a variable ROLE, that is passed to my ANT script..
Below my code:
<property file="Profiles.properties" prefix="profiles"/>
<echo>${profiles.CommercialManager}</echo>
Doing like this everything works fine, it prints out
MOT
But as you can see the value is not dynamic, is not the one taken from jenkins job..
So i should do something like this:
<echo>${ROLE}</echo>
But if I do something like this, the print returns the value of the property ROLE that is:
profiles.CommercialManager
and not the value taken from the properties file..
How can i manage this? I think its easy but, its late, and i swimming into a sea of confusion..
Thanks a lot!
There are a number of ways to dynamically get a property value from a variable described in other threads:
In Ant, how can I dynamically build a property that references a property file?
Dynamic property names in ant
Personally, I would use javascript:
<property file="Profiles.properties" prefix="profiles"/>
<script language="javascript"><![CDATA[
project.setProperty("CommercialManager", project.getProperty("${Role}"))
]]>
</script>
<echo>${CommercialManager}</echo>

Ant possible to insert cmd line input key-value pairs to match a property file?

I have a property file includes key-value pairs:
key1=value1
key2=value2
...
I have an Ant target called "compute". I would like to override the values by referring to the key like this:
ant compute -Dkey1=this is my value
How would this be possible to do? Thanks.
Try:
ant compute -Dkey1="this is my value"
There are many ways to achieve this (Asking for user input with <input/> task, setting the var in the Global Properties (under Preferences->Ant), etc), but i know only those on Eclipse :S
By the way, if what you want is to change multiple variables-per-setting (like environments settings), you can use different property files, like dev.properties, test.properties and prod.properties, and then hard-code the variable values into those files.
Then you will only need ONE parameter at the beginning, specifying which "set" (file) of variables you wanna pick...
And your parameter will not override any other, it will be straight in the command line, like
ant compute -Denv="test"
or
ant compute -Denv=test
, that must work (unless Ant guide is wrong, and i don't think so).

Dynamic property names in ant

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.
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
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 .

How can I use an Ant foreach iteration with values from a file?

In our Ant build environment, I have to do the same task for a number of items. The AntContrib foreach task is useful for that. However, the list is in a parameter, where I actually have the list in a file. How can I iterate over items in a file in an foreach-like way in Ant? Something like (pseudo-code):
<foreach target="compile-module" listFromFile="$fileWithModules"/>
I'm happy to write a custom Task, and welcome any suggestion on possible solutions.
I tried to load the file into a property and iterate over it, worked fine for me:
<loadfile property="file-content" srcFile="${fileWithModules}"/>
<foreach
target="compile-module"
list="${file-content}"
delimiter="${line.separator}"
param="your-param-used-in-target"/>

ant script to use use two .properties files?

I want to know if it is possible to get an ant script to reference 2 different .properties files at once, and if so, how to accomplish this.
Assume that the properties contained within the two .properties files are mutually exclusive, i.e. the same property will not appear twice.
Thanks!
In addition to Ash answer.
You can use a different prefix attribute of property task, e.g
<property file="file1.properties" prefix="file1"/>
<property file="file2.properties" prefix="file2"/>
This way you can find out if both files have same properties and differentiate between them in your build script. For example if both files have property test, then after they are loaded with the above commands you will end up with properties named file1.test and file2.test.
You should be able to import any number of properties files with multiple <property file="..."> entries in your ant script (unless there's some subtlety to your question that I've missed?). Duplicate properties are OK, since in ant properties are immutable and whoever sets the property first "wins".
See http://ant.apache.org/manual/Tasks/property.html for more details.

Resources