Passing property files in a loop to Ant script - ant

I have a directory with a list of property files for different environment(DEV/STG/QA etc.,)
I want to call an Ant target in a loop with each of this file. How do I do this. I downloaded ant-contrib and tried using the foreach but I couldn't find any example where I can read property files one at a time and call the target. Any suggestions?
I have been looking at a lot of samples on this site and online, nothing seem to match my requirement.

As per apacche documentation : https://ant.apache.org/manual/Tasks/property.html
<property file="foo.properties"/>
reads a set of properties from a file called "foo.properties".
so, if you have my.var=25 , then ${my.var} will get you its value 25. For your requirement you can iterate through the files based on 'env' name & do required tasks.

Related

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

How do I find a particular sub directory using Ant and then use it to create a symlink?

I need to create a symlink to a sub-directory using Ant. The issue is that I don't know where the target sub-directory is.
To create a symlink with ant I do this:
<symlink link="${parent.dir}/FOO/linkname" resource="${parent.dir}/BAR/target"/>
But I don't know what BAR is called in advance so I need to do a search for "target" under parent.dir and then pass the one result into the resource.
Is this possible using fileset? Or another way?
It might be possible to use a fileset but that might give you several symlinks or none.
A much better approach is to define the path to BAR in a property. If there is a dynamic part in this path, change the code so that Ant evaluates the dynamic part and everyone else uses Ant's value.
The typical example here is that the path contains a version or timestamp. Define those in your build file so you can use them everywhere. If a Java process needs the values, pass them to the process as a system property (-D...).

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 .

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.

Custom Ant Task Needed?

I use the Ant cvs and sql tasks to check out and deploy a full code set of database objects. I also use the Ant cvschangelog task to generate a list of what has changed between two tags. It seems that there should be some way of defining a target process that would iterate over the list of elements from the generated changelog xml file and only execute files that have changed between two tags (all of the files use "CREATE or REPLACE" syntax and only replaceable objects are of interest here).
Is there any native Ant solution for this or will a custom task to parse the xml file be necessary?
You might investigate the ant-contrib tasks for looping constructs. The for or foreach task may be just what you need.
Here is an example :
http://franckbehaghel.free.fr/antTask/
Last time I checked Ant didn't support any sort of looping constructs, so you'll have to write your own custom task> However, there is an xslt task that lets you apply stylesheets to xml files. It will depends on what you want to do with the xml itself.

Resources