ant script to use use two .properties files? - ant

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.

Related

Passing property files in a loop to Ant script

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.

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

property file path changed during ant build

<propertyfile file="${build.dir}/MyProperties.properties">
<entry key="releaseInformation"
type="string"
value="${build.time}"/>
</propertyfile>
When Ant copies my properties file over to the bin directory there is a property in it that has something like "samplePathName=C\:\Users\SomeUser\". But the property from the original file was "samplePathName=C:\Users\SomeUser\". How would the additional backslash end up there? I don't see anything that could possibly cause this to happen. Where should I begin looking other than the build.xml which only contains (relevant) the above line?
This is a common problem - the format of property files is defined by Sun (Oracle). Ant is conforming to this, which is why the escaping happens. There's no way round this using the propertyfile task - that's the way it's intended to work. If the file is genuinely a Java property file, then it shouldn't matter - the escaping should be handled correctly when the file is read.
However if you are hoping to use propertyfile to write a name-value config file that's for something else - where the escaping is not wanted - you'll need to adopt a different approach. As mentioned in the answer to a related question - you might use the Ant replace or replaceregexp tasks for this.

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 .

Resources