get ANT_HOME for configured ant in shell - ant

I have an ant installation configured in jenkins (Manage Jenkins->Ant Installations). I have a build step Execute shell where I need to call ant targets. How can I get ANT_HOME for specific ant installation or configure that shell needs to have ANT_HOME for specific installation?

JAVA_HOME is setting when you select an ANT Step, the shell script will use the ANT_HOME setted on the server as environment variable.
A solution is write the ANT_HOME value during an ANT task on a file on the workspace.
<echo message="${ant.home}" file="anthome.txt" />
The shell script read this file and use it for launch ant targets.
FOR /F %i IN (anthome.txt) DO SET ANT_HOME=%i
In any case perhaps is easier to have a ant script for launch other ant targets that your approach.

Related

Jenkins Build is giving error "workspace directory not valid"

I am trying to run a ear task with ant on Jenkins. The ant task builds successfully on my local machine.
However, when I try to build using Jenkins, I get the following error: "specified workspace is not valid". I tried hardcoding the workspace value, I tried getting it from the environment variables and I also tried to define it in a custom workspace and then accessing it.
But I keep getting the following error:
[subProjEAR] $ cmd.exe /C '"F:\Build\Ant\apache-ant-1.8.4\bin\ant.bat && exit %%ERRORLEVEL%%"'
Buildfile: E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml
init.env:
init.typedefs:
Trying to override old definition of task apt
init:
BUILD FAILED
E:\Jenkins\jobs\ProjDev\workspace\subProjEAR\build.xml:81: Specified workspace directory "E:\Jenkins\jobs\ProjDev\workspace" is not valid.
Total time: 0 seconds
Build step 'Invoke Ant' marked build as failure`
The code snipped around line 81 is as below
<target name="init" depends="init.env,init.typedefs" unless="init.executed">
<property name="init.executed" value="true" />
<fail unless="workspace" message="The workspace property needs to be set!" />
<dirname property="project.dir" file="${ant.file}" />
<property name="echo.metadata" value="false" />
<!-- Line below is no 81 -->
<mdimport workspace="${workspace}" pjdir="${project.dir}" echo="${echo.metadata}">
</mdimport>
<property name="archive.name" value="${project.name}.ear"/>
<property name="uri" value=""/>
</target>
I have created an environment variable named as WORKSPACE in my "local machine". Echoing prints it correctly ( C:\bea\user_projects\w4WP_workspaces\myProjWS). I have also set the WORKSPACE environment variable on the Jenkins machine. This is the same workspace as Jenkins. Echo on Jenkins prints the below:
Buildfile: E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml
init.env:
init.typedefs:
Trying to override old definition of task apt
init:
[echo] workspace : E:\Jenkins\jobs\ProjectDev\workspace
[echo] weblogic workspace : ${env.WORKSHOP_WORKSPACE}
BUILD FAILED
E:\Jenkins\jobs\ProjectDev\workspace\subProjEAR\build.xml:85: Specified workspace directory "E:\Jenkins\jobs\ProjectDev\workspace" is not valid.
Please help.
Best way to access WORKSPACE from ANT is through:
<property environment="env" />
<echo message="${env.WORKSPACE}" />
It doesn't seem like a Jenkins error, as Jenkins is able to successfully load the build file from that workspace. What is on line: 81 of that build.xml? The cause of the problem is there. Paste a snippet of your build file around line: 81
Edit after author comments
So, as I guessed, it wasn't anything to do with Jenkins. The issue is with <mdimport> task. So far, I've been able to find zero documentation about it, other that it comes from Weblogic
It is this task that is throwing the error. And considering that Weblogic and Jenkins have little to do with each other, I doubt you should be passing Jenkins's workspace for this task. It is probably looking for some Weblogic workspace.
Do you have a Weblogic workspace checked out under a subfolder in Jenkins, like my_checkout_folder? If so, maybe you should use ${workspace}/my_checkout_folder for that task?
In this issue from someone, they are reading ${workspace}/workspace.xml file
In this example (which looks very similar to yours), the value of workspace comes from environment variable WORKSHOP_WORKSPACE. Do you have that environment variable set on your local machine (the one where it works)?
Probable cause
It just came to me, while reviewing your question.
You said it works on local machine, but not on Jenkins.
If you are using the same ant file on both, means you are expecting an environment variable workspace to be present on your local machine, right? (Otherwise the same ant script wouldn't have worked on local)
Jenkins, when executing a build, has its own environment variable workspace which has nothing to do with the one you might be having on your local machine. Jenkins's workspace variable defines its job's working directory.
Even if you manually set a global workspace environment variable on Jenkins machine, it will be overwritten by the job's workspace everytime it runs.
Do the following test:
echo workspace environment variable on your local machine
in Jenkins job, use "execute shell" (or "execute windows batch command") to echo workspace from within the job.
Paste the result here. But you can also compare them yourself.

ivy ignores ANT_OPTS and -D variables when running from intellij idea

I set environment variable in ANT_OPTS in Windows:
-Duser.home=C:/Projects;-Divy.default.ivy.user.dir=C:/Projects/ivy
and specified two ant variables in Ant properties window in Intellij Idea
user.home C:/Projects
ivy.default.ivy.user.dir C:/Projects/ivy
But when I run ant tasks that works with ivy they use my profile home dir (at network dist) as a home dir for ivy repo and all settings are ignored.
In intellij's output I see that it passes
-Duser.home=C:/Projects/ -Divy.default.ivy.user.dir=C:/Projects/ivy
to ant.
Is it possible to fix it?
Thanks!

Overriding Environment Variables in an Ant Script

I have an issue where my automated build environment necessitates a different version of ant than the actual delegated scripts being run to produce the build.
I do not have an environmental variable for ANT_HOME.
I throw my CI environment the ANT_HOME to a directory of ant-1.7.0
But i need to override or unset ANT_HOME to a directory of ant-1.6.5 so the targets I call don't inherit the ANT_HOME, and run out of the 1.7.0 folder.
Can anyone think of a good way to do this? I can't modify the delegated targets of the build script but I can modify anything in the front end.
I'm using Cruisecontrol2.8.4 and the aforementioned ant versions.
Edit: The build targets I call set their own ANT_HOME to the 1.6.5 folder but it is being ignored as the previously set ANT_HOME to invoke cruisecontrol is immutable
You can unset properties with AntContrib.
In your ant script...
Import AntContrib:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="folderwithantcontribjar/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
Do the unset:
<var name="ANT_HOME" unset="true"/>
This removes ANT_HOME from the current set of properties allowing it to be set again.
Try setting the anthome attribute of the ant builder in your cruisecontrol configuration file :
http://cruisecontrol.sourceforge.net/main/configxml.html#ant
This should call your version of ant and presumably all called ant scripts will also use this version.

Is there a way to use just build command instead of ant build while using ant scripts?

I am using Apache Ant scripts for building a web application. I have written some targets in the build.xml file and the script is running fine. I remember using just "build" command to run ant build instead of "ant build". Can anyone tell me how is that achieved? I was a bit curious on this.
There's no built in "build" command. You could create a simple script file called "build" in the same directory that launched the ant build.
Create a text file with this as the contents:
ant build
In windows save this as a file called build.bat then you can just type build from the command line to start your build.
On unix or linux, save the file as build, then make it executable (with chmod +x build). You'll need to type ./build to get it to run.
I don't think there's a lot of value doing this to replace the simple case of ant build, but if you have to regularly run a build that has multiple targets, or need to pass in certain system variables then it could come in useful.
Maybe your are remembering typing "ant" instead of "ant build" in the past. This is possible to setup. You just need to set default attribute on the root project element in your Ant script to the name of the target you want invoked when an explicit target isn't specified.
For instance...
<project name="myproj" default="build">
...
</project>

Ant build tool installation not working properly!

i have downloaded the Ant distribution from the Ant web site.
apache-ant-1.7.0
and set environment variable specifying the location of the Ant installationas
ANT_HOME = C:\portal\apache-ant-1.7.0
PATH=;%ANT_HOME%\bin
But ant is not getting properly installed
Better use batch or shell scripts to start your ant files, f.e. =
windows
set JAVA_HOME=C:\java\jdk\1.6.0_xx
set ANT_HOME=C:\ant
set ANT_ARGS=-lib C:\ant_xtralibs;C:\ant_testlibs
set PATH=%PATH%;%JAVA_HOME%\bin;%ANT_HOME%\bin;C:\cvsnt
:: default
call ant -f %1
:: debug
::call ant -debug -f %1
...
unix - don't forget the quotationmarks on the ANT_ARGS line !
...
ANT_ARGS="-lib /usr/local/ant_xtralibs:/usr/local/ant_testlibs"
export ANT_ARGS
...
Some advantages of starting ant like that :
ANT_ARGS is a special environment variable. Its content is automatically
added to the invocation of ant.
you may use your own ant settings on a machine where you have no admin rights
using a separate folder for your ant addon libs and load via -lib option
keeps your ant installation clean and avoids polluting the %ANT_HOME%/lib folder
Move the ';' to the end. Be sure that java is also correct installed (variable for classpath, etc.)
Depending on you verson of windows (win2k & win XP), you might need to reboot your computer to make your PATH modifications taken into account.

Resources