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.
Related
I have a cmd script file, which sets all the environment values and invoke ant class to build the java project.
I have:
<property environment="env"/>
set in build.xml and
<property name="CD" value="${env.CDTEC}"/>.
build.xml unable to read these env values set from the cmd script file. If I echo the values from command prompt that prints, but not from the ant.xml file. Getting the error as
BUILD FAILED
c:\Users\test\Projects\Spring testing\build.xml:85: c:\Users\test\Projects\Spring testing\${env.CDTEC}\lib does not exist.
I added echo message in build.xml
as
<echo message="Message from ${this.CDTEC} Client" />
and printing that as
'Message from {env.CDTEC} Client'.
Command prompt is printing these values but Ant is not able to access these env values, any idea why?
Is the environment variable set externally to ANT?
Example
Setting the variable and calling ANT
$ CDTEC=hello ant
Buildfile: /....../build.xml
build:
[echo] CDTEC=hello
build.xml
<project name="demo" default="build">
<property environment="env"/>
<target name="build">
<echo message="CDTEC=${env.CDTEC}"/>
</target>
</project>
I am using Jenkins as CI. I have an build.xml. Build.xml has code like below.
<property name="environment" value="$environment}" />
How can i pass value to build.xml from Jenkins ? Can i pass it through environment variables?
Your environment variables are available via the environment property.
In the example below, the environment variable VIEW is printed from the simple hello world ant script via ${env.VIEW}. Change VIEW to the name of the environment variable of interest.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World" default="Hello" basedir=".">
<property environment="env"/>
<property name="HelloText" value="Hello"/>
<target name="Hello">
<echo>VIEW=${env.VIEW}</echo>
</target>
</project>
IMPORTANT! Note that this line is needed in order for env.VIEW to be understood by ant:
<property environment="env"/>
If your ant build dependeds on the property environment:
<property
name="environment"
value="a_value_I_edit_just_before_run_ant_default_target_mannually" />
you could configure your Jenkins' job to build with parameter:
so Jenkins will log (i.e. on Windows) something like:
cmd.exe /C '"ant.bat ant -file build.xml -Djenkins_environment=myValue && exit %%ERRORLEVEL%%"'
Then you could modify your ant build to check for system property:
<property
name="manually_edited_environment"
value="a_value_I_edit_just_before_run_ant_default_target_manually" />
<condition property="environment"
value="${jenkins_environment}"
else="${manually_edited_environment}">
<isset property="jenkins_environment" />
</condition>
if the jenkins_environment property is set then environment gets its value, else environment gets manually_edited_environment's value; the remaining part of your build still depend on the property environment.
If you have trouble in correctly matching the -DpropertyName use the echoproperties ant task to make ant logging all system properties.
your ant script should look like this:-
<target name="test" >
<property environment="env" />
<echo>BUILD_NUMBER: ${env.BUILD_NUMBER}</echo>
</target>
You can set properties with build parameters (ie: myParameter) and get them in an ant script with ${myParameter}.
Just make sure you don't use dots in parameters names in jenkins because there might be problems getting them in the ant script.
As for environment variables, i don't know, sorry.
Considering parametrized build as in the case above e.g you must have set variable called environment in the job and getting its value from user.Now you can refer to its value in build.xml using property name="environment" value="$environment}
For example, I have two build.xml: build.xml and subbuild.xml. In the build.xml, it will use an ant task to call and run the subbuild.xml like this:
<target name="antCall1" >
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>
My question is like this: I have an "test.xml" next to the build.xml. How can I modified the codes above so that I can pass the "test.xml" into the subbuild.xml and do some operations in the subbuild.xml?
Actually, I want to pass the "test.xml" as a of a task in subbuild.xml. I am just confused about how to let the subbuild.xml get accessed to the "test.xml" in the build.xml. Thanks!
Just set a property containing the name of the file in your build.xml before calling the subbuild.xml. An Ant subproject invoked using the ant task automatically inherits all properties set in their parent unless the attribute inheritAll is set to false. Alternatively you can pass the property to the subproject with a nested property element as suggested by CAustin in the comments.
First method:
<target name="antCall1" >
<!-- since the file is in the same directory as the build.xml -->
<property name="input.file" value="test.xml" />
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>
Second method:
<target name="antCall1" >
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml">
<property name="input.file" value="test.xml" />
</ant>
</target>
In subbuild.xml:
<loadfile property="file.contents" srcFile="${input.file}"/>
<echo message="${file.contents}" />
I am using a property file in ANT build script. Now, I am modifying some properties in the file based on the user input. The problem is that the changes are not effective during the execution but once the execution completes.
Here is the ANT script:
<project name="testProject" default="test" basedir=".">
<property file="my.properties"/>
<target name="test" depends="input">
<echo>${user}</echo>
</target>
<target name="input">
<echo>Taking user input</echo>
<input message="Please enter db-username:" addproperty="db.user"/>
<propertyfile file="my.properties">
<entry key="user" value="${db.user}"/>
</propertyfile>
</target>
</project>
Here is my.properties file:
#Fri, 23 May 2014 21:23:43 +0530
#My properties
#Thu, 22 May 2014 19:01:12 +0530
test=1
user=test
Here is the output after running:
D:\tmp>ant
Buildfile: D:\tmp\build.xml
input:
[echo] Taking user input
[input] Please enter db-username:
me
[propertyfile] Updating property file: D:\tmp\my.properties
test:
[echo] test
BUILD SUCCESSFUL
Total time: 4 seconds
But the file my.properties is update with user=me. Please tell me if this is the correct way of doing or any other alternative.
Interesting scenario, you can do the way how you are doing now. There will no problem but unnecessary complex for maintenance.
if you have different db user names you should pre define in property file instead of overwriting during execution.
dev.username="dev1234"(..${db.user})
qa.username="q1234"
prod.username="prodxxx"
First, once an Ant property is set, its value is immutable. It cannot change.
Second, once a property is set with <property file="my.properties"/>, the property's value cannot change. Even if my.properties has changed, further calls to <property file="my.properties"/> will not overwrite any existing properties.
Third, the <echo> in the test target doesn't echo the user's input.
This...
<target name="test" depends="input">
<echo>${user}</echo>
</target>
...should be...
<target name="test" depends="input">
<echo>${db.user}</echo>
</target>
Finally, the <propertyfile> task appears to create a new property in my.properties:
<propertyfile file="my.properties">
<entry key="username" value="${db.user}"/>
</propertyfile>
The above creates a property named username in my.properties. However, the rest of the script refers to the plain user property.
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