简体   繁体   English

如何在Jenkins Groovy管道中调用Ant

[英]How to invoke Ant in a Jenkins Groovy Pipeline

I have created a Jenkins Pipeline job. 我创建了一个Jenkins管道工作。 In this job I want to do the build using Ant. 在这项工作中,我想使用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 . 我已将Manage **Jenkins > Global Tool Configuration**的Ant变量**Jenkins > Global Tool Configuration**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: 在自由式jenkins Job中,我知道build.xml位置可以在下面的屏幕截图中指定: 在此输入图像描述

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: 但是我无法理解如何在此之后指定ant groovy脚本,特别是在哪里可以提到build.xml文件的路径:

def antHome = tool 'Ant1.9.1'
????
????

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): 在Windows下,这看起来像这样(我没有测试它):

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'. 这假设您在Jenkins中配置了名为“Ant1.9.1”的Ant。

you can use ant wrapper in Jenkins`s pipeline groovy script. 你可以在Jenkins的管道groovy脚本中使用ant包装器。

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". 请记住在Jenkins“全局工具配置”中使用相同名称“LocalAnt”配置ant工具。

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. 我需要在同一个Jenkinsfile中多次需要这个,需要在linux和windows代理上执行,所以我为它创建了一个方法。

You can call ant like this 你可以这样叫蚂蚁

callAnt("-v -p")

if you add this method definition to your jenkinsfile 如果将此方法定义添加到jenkins文件中

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}"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM