How to change ant property location attribute with .properties file - ant

Is it possible to change ant property location attribute with .properties file like
property value attribute?
ant.xml
<property name="images" location="some_location" />
ant.properties
name=D:\images

See ant manual propertyfile => there is no attribute called location.
The location attribute from property task is only a 'special' case of a value, that knows how to deal with absolute and relative pathes.
If you need to edit | overwrite existing property values(locations) use either :
ant script task (groovy or groovy task recommended)
or some Ant addon like Flaka or Antcontrib, providing tasks for that purpose.

Declaring a property within the build file will override the same value imported from a properties file.
The only way to do this is setting the property value on the command-line as follows:
ant -Dimages=D:\images

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>

Set Ant project name

I am trying to change the ant.project.name property after the project declaration. I know this is not advisable but it's the only way I can fix a big problem with my project.
I found some interesting posts like this one:
Ant - How to set ${ant.project.name} to project folder name?
Instead of using:
<project basedir="." default="package">
<basename property="ant.project.name"
file="${basedir}"/>
</project>
I'd like to directly set the ant.project.name using a "value" instead of a property "file".
Do you have any ideas or suggestions? Or alternative ways?
Thank you!
As others already mentioned, it's not recommended to change values of standard ant properties.
Also properties once set are immutable in ant by design and for good reasons. Overriding properties should be used wisely and rarely.
The property ant.project.name is usually set via name attribute of project =>
<project name="whatever"> but it's not mandatory, means <project> ... </project> is sufficient to make your xml a valid antscript.
In your case <echo>${ant.project.name}</echo> would echo ${ant.project.name}, as property is not set, so you may create it with property task in your script : <property name="ant.project.name" value="whatever"/>. But using a propertyname that is normally used for 'ant internals' seems not the best choice.
If property is set within project tag it's possible to overwrite the value via script task, using builtin javascript engine and ant api, f.e. :
<project name="foo">
<property name="bla" value="foobar"/>
<echo>1. $${ant.project.name} => ${ant.project.name}</echo>
<script language="javascript">
project.setUserProperty('ant.project.name', project.getProperty('bla'));
</script>
<echo>2. $${ant.project.name} => ${ant.project.name}</echo>
</project>
output :
[echo] 1. ${ant.project.name} => foo
[echo] 2. ${ant.project.name} => foobar
Notice : as ant.project.name is not a 'normal' property (those properties declared via property task within ant script), you have to use the method project.setUserProperty(String, String) instead of project.setProperty(String, String). Userproperties are properties defined via -Dkey=value commandline argument and enjoy a special protection.
Ant also provides a bunch of builtin properties

Can you set ant flags in the build properties file?

I want to run ant -emacs for only a certain target
can i add something like
<property name="build.compiler.emacs" value="true" />
to do this?
You can define a property like that anywhere in your build file. But once it is set, e.g. when containing target is invoked, it remains set.
Yes, either to your ant buildfile or to a build.properties file in this Java properties format, according to the these rules specified by ant.

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.

How to store Apache Ant property value in file

I need to modify a (xml-)file from Apache Ant. "loadfile" task allows to load the file's content in a property. But how to store the property's value back to a file after its (property) modification?
Of course I could write custom task to perform this operation but I would like to know if there's some existing implementation.
You can use the echo task.
<echo file="${fileName}" message="${xmlProperty}"/>
The echoxml task might be of interest to you as well.
Use propertyfile task. An example taken from ant manual:
<propertyfile file="my.properties">
<entry key="abc" value="${abc}"/>
</propertyfile>
This may be better than echo as it updates the properties file with a given value, while echo appends to or overwrites the whole file.

Resources