maven - 在 Jenkins 声明性管道的不同阶段重用对象/属性

标签 maven jenkins groovy jenkins-pipeline artifactory

我们创建一个新的 maven 构建:

def rtMaven = Artifactory.newMavenBuild()

现在我们想在与当前阶段不同的阶段重用这个 rtMaven;
就像下面的代码:
pipeline {
agent any

...
stages {

    stage('stage1') {
        steps {
            script {
                def rtMaven = Artifactory.newMavenBuild()
            }
    }

    stage('stage2') {
         steps {
            script {
                //REUSE rtMaven (now it's unknown)
            }
         }

     }
}

是否可以重用rtMaven而无需在第二阶段再次重新定义它?

现在我们有一个错误,如:
groovy.lang.MissingPropertyException: No such property: rtMaven for class: groovy.lang.Binding

最佳答案

在全局范围内定义 var

def rtMaven = ''
pipeline {
    agent any
    stages {
        stage('stage1') {
            steps {
                script {
                    rtMaven = Artifactory.newMavenBuild()
                }
            }
        }
    }
    stage('stage2') {
        steps {
            script {
                echo "$rtMaven"
            }
        }
    }
}

关于maven - 在 Jenkins 声明性管道的不同阶段重用对象/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155326/

相关文章:

java - 如何管理模块化 Maven 项目的版本号?

Jenkins 无法识别 git 二进制文件

jenkins - 使用 Active Choice Reactive Reference 参数构建项目

jenkins - 测试用例在 Jenkins 中失败,在 Katalon Studio 中成功

groovy - 使用 Groovy 解析 XML : How do I keep the CDATA when parsing a XML file

java - 如何访问 pom.properties

maven - 尽管已转移,但仍出现错误 "Failed to transfer file *.jar with status code 409"

grails - OutOfMemoryError : Java heap space to upload 8 MB size file

list - 如何获取我在 groovy 中迭代的列表的引用

java - 如何选择运行哪些maven执行?