gpt4 book ai didi

maven - Jenkins 管道和 Gradle 项目,如何获取 GAV 属性以使用 Jenkins Nexus 插件部署 Artifact ?

转载 作者:行者123 更新时间:2023-12-03 05:52:59 26 4
gpt4 key购买 nike

我最近从 Maven 切换到 Gradle。到目前为止,我的 Jenkins 声明性管道包括一个阶段,我们生成打包的 Artifact 并将其发布到 Nexus:

  pipeline {
...
stages {
stage ('Build Stage') {
steps {
...
sh "mvn clean deploy"
...
}
}

通过这种方式,我们使用我们的构建工具 (Maven) 使用其插件之一(maven deploy 插件)来部署 Artifact 。与 Nexus Jenkins Plugin我们可以解耦这些对我来说有意义的功能:Gradle 负责构建、测试和打包 Artifact ,Jenkins Nexus 插件会将其上传到 Nexus。

重点是,在 Jenkins Nexus Plugin documentation 之后我必须使用如下步骤:
nexusPublisher nexusInstanceId: 'localNexus', nexusRepositoryId: 'releases', packages: [[$class: 'MavenPackage', mavenAssetList: [[classifier: '', extension: '', filePath: 'buildmavenCoordinate: [artifactId: 'myproject'/lib/myproject-1.0.0.jar']], , groupId: 'com.codependent.myproject', packaging: 'jar', version: '1.0.0']]]

由于我想生成一个可重用的管道,我不想在步骤中硬编码以下属性:
  • groupId
  • Artifact ID
  • 包装
  • 版本

  • 如果我使用 Maven,我会使用 readMavenPom step from the Pipeline Utility steps plugin将它们传递给 nexusPublisher 步骤:
    def pom = readMavenPom file: 'pom.xml'
    ...
    nexusPublisher ... mavenCoordinate: [artifactId: pom.artifactId
    ...

    我的问题是如何从管道中的 Gradle 配置中获取这四个参数。

    假设我的 build.gradle 如下:
    apply plugin: 'java'
    apply plugin: 'eclipse'

    sourceCompatibility = 1.8

    group = 'com.codependent.myproject'
    version = '1.0.0'
    jar {
    manifest {
    attributes 'Implementation-Title': 'Gradle Quickstart',
    'Implementation-Version': version
    }
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    }

    test {
    systemProperties 'property': 'value'
    }
    ...

    最佳答案

    您在管道中使用 Groovy 吗? Gradle 不提供解析 POM 文件的原生支持。我建议您在 Groovy 中使用 XmlSlurper。

    如果您的管道在 Groovy 中,您可以使用 Gradle 使用以下代码读取这些属性:

    def pom = new XmlSlurper().parse(new File('pom.xml'))
    def version = pom.version
    def artifactId = pom.artifactId
    def groupId = pom.groupId

    如果需要,您可以直接检查此值打印变量:
    println 'my pom version ' + pom.version
    println 'my pom version ' + pom.artifactId
    println 'my pom version ' + pom.groupId

    有关 XmlSlurper 的更多信息: Official page of XmlSlurper

    编辑:

    如果你没有 pom 文件并且你有 build.gradle 文件,我建议你阅读这篇文章:

    how to read a properties files and use the values in project gradle script?

    关于maven - Jenkins 管道和 Gradle 项目,如何获取 GAV 属性以使用 Jenkins Nexus 插件部署 Artifact ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46595489/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com