9

I have created a Jenkins Pipeline job. In this job I want to do the build using Ant. I have configured the Ant variable in Manage **Jenkins > Global Tool Configuration** as Ant1.9.1= D:\path_to_hybris\hybris\bin\platform\apache-ant-1.9.1.

In a freestyle jenkins Job, I know that the build.xml location can be specified as in the below screenshot: enter image description here

but I am unable to understand how to specify the ant groovy script beyond this point, especially where can we mention the path to build.xml file:

def antHome = tool 'Ant1.9.1'
????
????
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user5917011
  • 1,137
  • 5
  • 14
  • 22
  • Possible duplicate of [How to invoke Ant in Jenkins pipeline job using groovy script?](https://stackoverflow.com/questions/38103958/how-to-invoke-ant-in-jenkins-pipeline-job-using-groovy-script) – PhoneixS Jul 14 '17 at 10:45

3 Answers3

9

you can use ant wrapper in Jenkins`s pipeline groovy script.

withAnt(installation: 'LocalAnt') {
// some block
   sh "ant build"
//for windows 
   bat "ant build"
}

Remember to configure the ant tool in the Jenkins "Global Tool Configuration" with the same name "LocalAnt".

Khalid Bin Huda
  • 1,583
  • 17
  • 16
  • for the `sh "ant build"` do I need to put the path to the build xml file or will simply typing `"ant build" work? – henhen Mar 25 '20 at 05:56
  • 1
    use something like `sh "ant -f build.xml all"` where `build.xml`is the path to the build file and `all` is the target. the complete looks like this: `withAnt(installation: 'Ant1.9.1') { sh "ant -f build.xml all" }` where `Ant1.9.1` is the name of the Ant installation in Jenkins – sys64738 Feb 10 '22 at 10:01
6

You can try this:

def antVersion = 'Ant1.9.1'
withEnv( ["ANT_HOME=${tool antVersion}"] ) {
    sh '$ANT_HOME/bin/ant target1 target2'
}

Under Windows this would look like this (I didn't test it though):

def antVersion = 'Ant1.9.1'
withEnv( ["ANT_HOME=${tool antVersion}"] ) {
    bat '%ANT_HOME%/bin/ant.bat target1 target2'
}

This assumes that you have Ant configured in Jenkins with name 'Ant1.9.1'.

user1053510
  • 1,548
  • 1
  • 15
  • 23
2

I needed this multiple times within the same Jenkinsfile that needs to be executed on both linux and windows agents so I created a method for it.

You can call ant like this

callAnt("-v -p")

if you add this method definition to your jenkinsfile

def callAnt(String Parameters) {
    if (isUnix()) {
        env.PATH = "${tool 'ant'}/bin;${env.PATH}"
        sh "ant ${Parameters}"
    }
    else {
        env.PATH = "${tool 'ant'}\\bin;${env.PATH}"
        bat "ant ${Parameters}"
    }
}

carl verbiest
  • 1,113
  • 1
  • 15
  • 30