Ant build.xml not reading env properties - java

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>

Related

CreateProcess error=2, Can not run program "python" on Ant

I have a C:\Users\Sony\Desktop\Project\Workspace\testpython\verify_fam_extended.xml:52: Execute failed: java.io.IOException: Cannot run program "python" (in directory "C:\Users\Sony\Desktop\Project\Workspace\testpython\backend\generated"): CreateProcess error=2, The system cannot find the file specified error when I run my xml code through Ant.
<exec dir="backend/generated" executable="python">
<arg line="toplevel.py"/>
<env key="PYTHONPATH" value="${dsltrans.install.path}:./backend/generated"/>
</exec>
edit python.exe must be in a directory defined in the executable path variable PATH.
Following working example assumes the presence of C:\Python33\python.exe
<project name="demo" default="main" basedir=".">
<property environment="env" />
<echo>PATH is set as: ${env.PATH}</echo>
<target name="main">
<exec dir="backend/generated" executable="python">
<arg line="--version"/>
</exec>
</target>
</project>
execute in a command session
set PATH=C:\Python33;%PATH%
ant
output
Buildfile: X:\temp\build.xml
[echo] PATH is set as: C:\Python33;...
...
main:
[exec] Python 3.3.0
edit Just checked. The executable can be executable="python" or executable="python.exe". So #cdarke is right the issue is that python.exe is not in PATH.

Read property from file and assign to a different name in ant?

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.

Pass environment variables to Ant build.xml from Jenkins?

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}

Java ant Eclipse run error [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
ant error JAVA_HOME does not point to SDK
I'm getting the following error:
BUILD FAILED
C:\Users\myname\Documents\CMSC\Proj1\build.xml:22: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files (x86)\Java\jre7"
Total time: 1 second
My build.xml file contains the following code:
<project name="Project1" default="compile" basedir=".">
<description>
Build file for Project1
</description>
<!-- global properties for this build file -->
<property name="source.dir" location="src"/>
<property name="build.dir" location="bin"/>
<property name="doc.dir" location="doc"/>
<property name="main.class" value="proj1.Project1"/>
<!-- set up some directories used by this project -->
<target name="init" description="setup project directories">
<mkdir dir="${build.dir}"/>
<mkdir dir="${doc.dir}"/>
</target>
<!-- Compile the java code in ${src.dir} into ${build.dir} -->
<target name="compile" depends="init" description="compile java sources">
<javac srcdir="${source.dir}" destdir="${build.dir}"/>
</target>
<!-- execute the program with the fully qualified name in ${build.dir} -->
<target name="run" description="run the project">
<java dir="${build.dir}" classname="${main.class}" fork="yes">
<arg line="${args}"/>
</java>
</target>
<!-- Delete the build & doc directories and Emacs backup (*~) files -->
<target name="clean" description="tidy up the workspace">
<delete dir="${build.dir}"/>
<delete dir="${doc.dir}"/>
<delete>
<fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
</delete>
</target>
<!-- Generate javadocs for current project into ${doc.dir} -->
<target name="doc" depends="init" description="generate documentation">
<javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
</target>
</project>
What am I doing wrong here? It appears that it's trying to find a javac file in my src folder but I don't know how to fix that.
The JAVA_HOME must be pointed to JDK and not JRE.
To test it, before executing ant type set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_07 or whatever version you have.
Download the latest JDK here. Install it and notice where it goes. For example, the 32-bit JDK should install to C:\Program Files (x86)\Java\jdkSOMETHING.
In Eclipse, go to Window > Preferences > Java > Installed JREs and click Add > Standard JVM (then Next). Browse to the directory noted above for the JDK and then hit Finish. To check that your project is using this JDK, right-click the project and go to Properties. From there, look for Java Build Path. Under the Libraries tab you should see a JRE System Library. If it's not the JDK you just added, click the entry and hit the Edit button and change it.
Now run your Ant build.
Looking at your error, looks like its Windows environment and you are running ant through command prompt.
Follow Right Click(My Computer) -> properties -> Advanced
-> Environment Variables -> System Variables -> New
Add Variable Name = JAVA_HOME
Variable Value = Path to your Base folder of Java
e.g. C:\Program Files\Java\jdkxxx
Restart the command prompt, type java -version. If it prints your java version correctly, you should be all set.
it is saying
"Perhaps JAVA_HOME does not point to the JDK."
Perhaps you should check that :D

Trouble Executing Bash.exe From an Ant Buildfile in Eclipse

I've created an Ant buildfile and an associated Ant builder in my Eclipse project. The builder is executing correctly but I can't seem to pass the correct information to bash. I'm running cygwin on an XP Professional SP3 machine. I know the command works and have verified it from a cygwin terminal. I created a custom builder earlier with this command so I also know that it works from Eclipse.
Here is my build xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="BlazeLibrary.makejar" default="makejar" basedir=".">
<property name="bash" location="e:\cygwin\bin\bash.exe" />
<property name="workingdir" location="e:\cygwin\bin" />
<property name="cmdline" value="--login -c \"cd /cygdrive/c/dev/projects/droid/NDKTestApp && /cygdrive/c/dev/tools/droid/android-ndk-r4b/ndk-build"" />
<target name="nativeBuild" description="Build the native binaries using the Android NDK">
<exec dir="${workingdir}" executable="${bash}">
<arg value="${cmdline}" />
</exec>
</target>
</project>
The task runs fine but the output I indicates that the command line parameters are incorrect. Even though they are listed exactly as they should be (and verified by running from cmd prompt as well as the custom builder mentioned previously).
Here is the relevant part of the error message (the rest just vomits the help and is of no relevancy for this question):
nativeBuild:
[exec] /usr/bin/bash: --login -c "cd /cygdrive/c/dev/projects/droid/NDKTestApp && /cygdrive/c/dev/tools/droid/android-ndk-r4b/ndk-build": invalid option
[exec] Usage: /usr/bin/bash [GNU long option] [option] ... blah blah blah
I'll be the first to admit that I am an Ant noob so I'm probably missing something very obvious. I've searched but nothing really jumps out at me and the task seems to run correctly, just something funky about the command line. Thanks for any help in advance.
The immediate problem is that the ${cmdline} property is being passed to bash as a single argument - hence the very long 'invalid option'.
You could pass the command as an arg line instead:
<exec dir="${workingdir}" executable="${bash}">
<arg line="${cmdline}" />
</exec>
Or perhaps break it up into separate values. Note that you don't need the quots around the -c arg in that case:
<property name="cmdline" value="cd /cygdrive/ ..etc.. ndk-build" />
<exec dir="${workingdir}" executable="${bash}">
<arg value="--login" />
<arg value="-c" />
<arg value="${cmdline}" />
</exec>
More here.
You can still use the format in your initial post, just change your quotes around a bit, like this:
<!-- These properties hold the location of the android-ndk, for us to build our antive libs -->
<property name="bash" location="c:\cygwin\bin\bash.exe" />
<property name="workingdir" location="c:\cygwin\bin" />
<property name="cmdline" value="--login -c 'cd /path/to/project/files;/path/to/ndk_build/ndk-build;exit'" />
<target name="NDKBUILD" description="Build the native binaries using the Android NDK">
<exec dir="${workingdir}" executable="${bash}">
<arg line="${cmdline}" />
</exec>
</target>
Note the usage of " " to denote the XML string, but ' ' to denote command lines passed to cygwin/bash.
Also, when using this script in my android builder (heavily customised ant script with extensive source preprocessing) I ran into issues where the ant build was "Already In Progress".
To this end, I added an exit command to the end of the bash script, so the cygin bash process is always closed after being called.

Categories

Resources