How to call java function in Jenkinsfile using Pipeline plugin in Jenkins

↘锁芯ラ 提交于 2019-12-05 14:35:57

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<Integer> 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`…
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!