jenkins 调用java程序,如何使用Jenkins中的Pipeline插件调用Jenkinsfile中的java函数

I am using pipeline plugin in jenkins. My Jenkinsfile has numToEcho =1,2,3,4 but I want to call Test.myNumbers() to get list of values.

How can I call myNumbers() java function in Jenkinsfile?

Or do I need to have a separate groovy script file and that file I should place inside java jar which has Test class?

My Jenkinsfile:

def numToEcho = [1,2,3,4]

def stepsForParallel = [:]

for (int i = 0; i < numToEcho.size(); i++) {

def s = numToEcho.get(i)

def stepName = "echoing ${s}"

stepsForParallel[stepName] = transformIntoStep(s)

}

parallel stepsForParallel

def transformIntoStep(inputNum) {

return {

node {

echo inputNum

}

}

}

import com.sample.pipeline.jenkins

public class Test{

public ArrayList myNumbers() {

ArrayList numbers = new ArrayList();

numbers.add(5);

numbers.add(11);

numbers.add(3);

return(numbers);

}

}

解决方案

You could write your logic in a Groovy file, which you can keep in a Git repository, or in a Pipeline Shared Library, or elsewhere.

For example, if you had the file utils.groovy in your repository:

List myNumbers() {

return [1, 2, 3, 4, 5]

}

return this

In your Jenkinsfile, you could use it like this via the load step:

def utils

node {

// Check out repository with utils.groovy

git 'https://github.com/…/my-repo.git'

// Load definitions from repo

utils = load 'utils.groovy'

}

// Execute utility method

def numbers = utils.myNumbers()

// Do stuff with `numbers`…

Alternatively, you can check out your Java code and run it, and capture the output. Then you could parse that into a list, or whatever data structure you need for later in the pipeline. For example:

node {

// Check out and build the Java tool

git 'https://github.com/…/some-java-tools.git'

sh './gradlew assemble'

// Run the compiled Java tool

def output = sh script: 'java -jar build/output/my-tool.jar', returnStdout: true

// Do some parsing in Groovy to turn the output into a list

def numbers = parseOutput(output)

// Do stuff with `numbers`…

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值