I'm looking for a way to load properties from a file in ant script. Specifically, I want to loop through a list of properties files, and on each loop load the properties from the current file and do something with it. Something like this:
<for param="file">
<path>
<fileset containing my properties files.../>
</path>
<sequential>
<property file="#{file}" prefix="fromFile"/>
<echo message="current file: #{file}"/>
<echo message="property1 from file: ${fromFile.property1}"/>
</sequential>
</for>
The code above results in only the first properties file from being read, even though each loop does go through each properties file name. I know property is immutable, and I can get around it by using local task or variable task from ant-contrib. However, I don't know how to apply them here, or if they even contribute to a solution in this case.
Here I used Antcontrib and two property files in the same directory as the build.xml.
p1.properties:
property1=from p1
p2.properties:
property1=from p2
The trick is to use antcall inside the for loop to call another target. Properties set in the called target at not propagated back to the caller.
build.xml:
<project name="test" default="read.property.files">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="read.property.files">
<for param="file">
<path>
<fileset dir="." includes="*.properties"/>
</path>
<sequential>
<antcall target="read.one.property.file">
<param name="propfile" value="#{file}"/>
</antcall>
</sequential>
</for>
</target>
<target name="read.one.property.file">
<property file="${propfile}" />
<echo message="current file: ${propfile}" />
<echo message="property1 from file: ${property1}"/>
</target>
</project>
The output is:
Buildfile: /home/vanje/anttest/build.xml
read.property.files:
read.one.property.file:
[echo] current file: /home/vanje/anttest/p1.properties
[echo] property1 from file: from p1
read.one.property.file:
[echo] current file: /home/vanje/anttest/p2.properties
[echo] property1 from file: from p2
BUILD SUCCESSFUL
Total time: 0 seconds
In my original question I had trouble loading entire properties file from within for loop (from ant-contrib). Turns out inside for loops, ant-contrib's own var task works just the same as property task from pure ant. All I have to do is replace <property file="#{file}" prefix="fromFile"/> with <var file="#{file}"/>. The properties loaded will be overwritten with the latest values and I don't even need prefix attribute to keep track of which loop I'm currently on.
I am trying to read a property from properties file and assign to a different name but it is not working. I am new to ant so I guess I am missing something basic.
build.properties:
USERNAME=deter_dangler
build.xml:
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<property environment="env"/>
<property name="uname" value="${env.USERNAME}"/>
<target name="test">
<echo message="uname property value is ${uname}"/>
<echo message="env.USERNAME property value is ${env.USERNAME}"/>
</target>
</project>
The output when I run the build command:
javanoob#DELL:~/Desktop$ ant
Buildfile: /Desktop/build.xml
test:
[echo] uname property value is ${env.USERNAME}
[echo] env.USERNAME property value is ${env.USERNAME}
BUILD SUCCESSFUL
Total time: 0 seconds
Trying setting the environment variable as follows:
USERNAME=deter_dangler ant
Alternatively, if you want to use a properties file then simplify your ANT file as follows:
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<target name="test">
<echo message="uname property value is ${USERNAME}"/>
</target>
</project>
I believe that it could be simple incorrect environment variable being used i.e., by default USER is available on the linux machine without requiring to be explicitly defined by user and you are using USERNAME. I assume this is what user is expecting as he is trying to print value from property and another one from system defined default.
So, just replacing the above mentioned changes in the build.properties and build.xml files.
build.properties
USER=deter_dangler
build.xml
<project name="Simple Ant example" default="test" basedir=".">
<property file="build.properties"/>
<property environment="env"/>
<property name="uname" value="${USER}"/>
<target name="test">
<echo message="uname property value is ${uname}"/>
<echo message="env.USER property value is ${env.USER}"/>
</target>
</project>
Output
Buildfile: /home/apps/Documents/so/34553709/build.xml
test:
[echo] uname property value is deter_dangler
[echo] env.USER property value is apps
BUILD SUCCESSFUL
Total time: 1 second
In the above, if you notice, ${USER} is taken from property file and ${env.USER} from system logged in user.
I have a build.xml file that has taks like, release, clean, debug, compress and buildnumber,
All tasks work fine but buildnumber task is not right, I think.
This is how my buildnumber task looks like in the build.xml file:
<target name="buildnumber">
<echo>Creating buildnumber</echo>
<propertyfile file="${basedir}/build/version.properties" >
<entry key="Build" type="int" default="0" operation="+"/>
</propertyfile>
</target>
This task gets only called when I do release:
<target name="release" description="compile for deployment">
<antcall target="clean" />
<antcall target="buildnumber">
<param name="increment" value="Build"/>
</antcall>
If I call this ant from Eclipse it creates a minified js file like this
mygame-0.0.0-min.js
so the buildnumber that is the last number in my case (0.0.0) does not get incremented.
If I call ant from Webstorm I got this minified file:
mygame-0.0.71-min.js.
I mean how is this possible? Buildnumber should not increment because this task is not called by ant because I do no release just "clean" , "debug" and "compress" .
Here is how my version.properties file looks like:
## Version properties
Version=1
Patch=0
Build=0
What is going wrong with my build.xml file? What am I missing?
I am studying how ant work and I have some doubts related to it. I have an ant xml script definition file that begin in this way:
<?xml version="1.0"?>
<project name="Peacock Engine" default="default"> <!-- "default" is the default target -->
<tstamp />
<!-- ============================================ -->
<!-- Load build properties -->
<!-- ============================================ -->
<property name="project.buildfile" value="build.num" />
<property file="${project.buildfile}" />
<property file="info.properties" />
<!-- ============================================ -->
<!-- Specify the classpath -->
<!-- ============================================ -->
<path id="project.classpath">
<fileset dir="${project.libdir}">
<include name="${project.libs}" />
</fileset>
</path>
<!-- ============================================ -->
<!-- The default target -->
<!-- ============================================ -->
<target name="default" depends="jar"/>
Now help me to analyze this:
1) project tag is the root target and I use it to specify the project attributes.
2) : what exactly do this line?
3) Then I have these lines:
<property name="project.buildfile" value="build.num" />
<property file="${project.buildfile}" />
<property file="info.properties" />
What exactly do? I think that the first line create something like a variable named project.buildfile and load into it the content of a file named build.num
REgarding the following 2 lines I have few idea about what they do...can you help me?
4) Then in the ant script I find these lines:
<!-- ============================================ -->
<!-- Specify the classpath -->
<!-- ============================================ -->
<path id="project.classpath">
<fileset dir="${project.libdir}">
<include name="${project.libs}" />
</fileset>
</path>
I tryed to search on the web but I have totaly no idea about this section
5) Finnslly I have this section that is the definition of the default target that is the default action that is executed when I launch the ant script withous specify a specific task (a specific target):
<!-- ============================================ -->
<!-- The default target -->
<!-- ============================================ -->
<target name="default" depends="jar"/>
I am not totally sure about it but I think that by this line I am sayng that the default behavior of the ant script is to compile the program and that the compiled program is put it inside a Jar file.
Someone can help me to understand better this script code?
Tnx
Andrea
1) and 2) This sets the name of the project to "Peacock Engine" and sets the default task to the task named "default" (see 5)). The default task will be executed if you invoke Ant without providing a specific task:
<project name="Peacock Engine" default="default">
3) <property name="project.buildfile" value="build.num" /> creates a property which you can access anywhere in your Ant file with ${project.buildfile}. The value of the property will be build.num
<property file="${project.buildfile}" /> makes use of the above defined property. It basically loads the file "build.num" and makes all properties that are defined in this file available for further use. How does a property file work ? Have a look at the ant documentation of the property file task.
<property file="info.properties" /> loads another property file called "info.properties" and also makes all the properties in this file available to Ant.
4)
<path id="project.classpath">
<fileset dir="${project.libdir}">
<include name="${project.libs}" />
</fileset>
</path>
This tag defines a classpath. The path tag encloses a fileset. The fileset includes all libraries in ${project.libdir} that include the name ${project.libs}. Both are variables that might have been defined by including the property files above.
Effectively this tag gives you a set of libraries that can be included anywhere in the build file by referencing it's id (project.classpath).
5) <target name="default" depends="jar"/> see 1). The project tag references this target as default target when no target is supplied. This target has another target which it depends on. The target named in "depends" will be executed before "default". Again, if "jar" has another target that it depends on, this target will be executed first, and so on. It is a "call-graph". See the documentation on targets for more information on this.
You may want to have a look at the Ant documentation for writing a simple build file as a starting point to get more familiar with Ant.
1) project tag is the root target and I use it to specify the project attributes.
The project tag simply names the project, you also have the default target defined. The project tag is not, itself, a target.
<property name="project.buildfile" value="build.num" />
Creates a property named project.buildfile and sets the value to 'build.num'
<property file="${project.buildfile}" />
reads a set of properties from a file, the name of which is stored in the property project.buildfile, the value of which, in this case, is 'build.num'.
<property file="info.properties" />
reads a set of properties from a file named 'info.properties'
<path id="project.classpath">
<fileset dir="${project.libdir}">
<include name="${project.libs}" />
</fileset>
</path>
Creates a path named 'project.classpath'. The path will have the root defined in the projectlib.dir property and contains all files defined in the project.libs property
<target name="default" depends="jar"/>
means that the target default is dependent on the target jar successfully completing first. Ant will run the jar target automatically to satisfy this requirement.
Apache maintains a manual for Ant here
You should go to Ant Manual and read through it. It'll help you understand many of your questions.
Project Line
Ant files are XML files, and XML files have a root entity that encloses all the other entities. This is what the <project> entity is. Here's a simple Ant build.xml file:
<project>
<echo message="Hello, World!"/>
</project>
The sole task in this file (<echo>) is enclosed in the <project> XML entity.
The <project> entity can take some attributes:
name: The name of the Ant project. This is available in the ${ant.project.name} property.
default: The default target you want to execute. Ant basically has a two-level hierarchy. There are targets and there are tasks. A target is what you want to execute (compile, package, execute, clean, etc.), and targets contain tasks to accomplish what you want to do.
basedir: The base directory used when you specify a directory. The default is the current directory.
XML Namespaces: Don't worry about this one for now. You won't be using this until you get more comfortable with Ant.
Property Lines:
Then I have these lines:
<property name="project.buildfile" value="build.num" />
<property file="${project.buildfile}" />
<property file="info.properties" />
Ant uses something called properties which you can think of as variables. However, once a property is set, it can't be changed. For example:
<property name="foo" value="This is the initial value of foo"/>
<property name="foo" value="This is my new value"/>
The first line sets ${foo} to This is the initial value of foo. The second line does nothing. In fact, it doesn't fail or anything. It simply doesn't reset the value.
You can use this to adjust your build system by creating property files that Ant will read in first before the build takes place. The first line sets a property called ${project.buildfile} to the file build.num. This is the build number. The second line reads in all the properties in this file and sets their values. The third line reads in another property file that may setup other properties. Here's a quick example:
<project>
<property file="build.num"/>
<property name="build.number" value="Not Set"/>
<echo message="The build number is ${build.number}"/>
</project>
Let's say there's no file called build.num. In this case, ${build.number} is set to Not Set, and the build file will print out:
The build number is Not Set
Now, let's make a file called build.num, and it is this:
build.number = 1234
Now, when I run the build, it will read in the property build.number from the build.num file. The <property name="build.number" value="Not Set"/> won't change the build number since it was already set. The build will now print out:
The build number is 1234
Now, let's say I run my build like this:
$ ant -Dbuild.number=9876
I am setting ${build.number} on my command line. The build will still read in the file build.num, but won't set ${build.number} from it since I've already set it. The line <property name="build.number" value="Not Set"/> will also be ignored since ${build.number} is already set.
Path
Then in the ant script I find these lines:
<!-- ============================================ -->
<!-- Specify the classpath -->
<!-- ============================================ -->
<path id="project.classpath">
<fileset dir="${project.libdir}">
<include name="${project.libs}" />
</fileset>
</path>
There are two types of data in Ant. One is the properties which I mentioned above. The other are Paths. A Path is a series of files or directories. You see this in Unix and Windows with the PATH environment variable:
$ echo $PATH
$ /bin:/usr/bin:/usr/local/bin:/home/david/bin
If I type in a command, the operating system will search each of my directories in the PATH to find that command. In Java, you have the CLASS_PATH which are the jars you need to compile your project.
This is setting a path called project.classpath. It basically takes all of the jars that matches the <fileset> criteria and puts them in a path that can be used later, maybe for the compilation.
Targets:
<target name="default" depends="jar"/>
As I mentioned earlier, Ant has a two level hierarchy: Targets are the things you want to accomplish. Think of them as a program. A compile target is a program to compile your code. Targets contain tasks that need to be done to run the target.
Some tasks depend upon other tasks. For example, a target to test my code will be dependent upon the target to compile the code. I can't test my code without it first compiling:
<target name="test" depends="compile">
....
</target>
Some targets simply specify other targets. For example, I have a target called all. This runs the target to clean up my directory structure and get rid of any previously built files, compile my code, run tests, and then to package up my code:
<target name="all" depends="clean,compile,test,package"/>
The all target doesn't do anything itself. It's just a way to combine all of the other targets into one easy to use target. I run all, and everything I need to do a complete build is done.
In this case, the developer is defining a target called default that runs the jar target. You can see in their <project> entity that the default target for this project is called default, so if I run Ant without specifying a target, it will run the default target.
This is a bit convoluted. The developer could have left this out, and simply set default="jar" in the <project> entity.
As I mentioned before, go to the Ant Manual and it will help you learn Ant. Plus, give you a reference you can use to learn more about these various tasks.
Dude Maven is the new kid on the block. Try it and you won't need ANT. I have been using it for a month now and no more going back to ANT. There are a lot of tutorials online on how to used it with your projects. Maven Website
I have an Ant buildfile that imports the following at the top of the file:
<project name="..." ...>
<property file="build.properties"/>
...
In the project root I have two properties files: build.properties and special-config.properties. I am defining a target that needs to read the following property out of special-config.properties:
always.ask=true
So the target needs to be something like this (just keep in mind that build.properties was already set as the property file long before this target ever executes):
<target name="exec-special-logic">
<!-- Somehow read special-config.properties#always.ask and set it to a local variable... -->
</target>
But I am ansure of how to load this 2nd property file and make its properties (such as always.ask) available to Ant. Thanks in advance.
You can read properties from as many different files as you like, so you could have
<property file="build.properties"/>
<property file="build.properties.part2"/>
And so on. In Ant the first value set for a property sticks - properties are quietly immutable. Hence if you have:
<property name="my.prop" value="one" />
somewhere in the first file and
<property name="my.prop" value="two" />
later - perhaps in the second file, the property will have the value "one" for the duration of the build.
A feature of recent versions is that properties can be localised to an execution block - this lets you "get around" the immutability. Here's an example lifted straight from the docs for the Ant local task:
<property name="foo" value="foo"/>
<target name="step1">
<echo>Before local: foo is ${foo}</echo>
<local name="foo"/>
<property name="foo" value="bar"/>
<echo>After local: foo is ${foo}</echo>
</target>
<target name="step2" depends="step1">
<echo>In step2: foo is ${foo}</echo>
</target>
outputs
step1:
[echo] Before local: foo is foo
[echo] After local: foo is bar
step2:
[echo] In step2: foo is foo