简体   繁体   English

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

[英]Jenkins pipeline and Gradle project, how can I get the GAV properties to deploy the artifact with Jenkins Nexus Plugin?

I have recently switched from Maven to Gradle. 我最近从Maven切换到Gradle。 So far my Jenkins declarative pipelines included a stage where we generated the packaged artifact and published it to Nexus: 到目前为止,我的Jenkins声明性管道包括一个阶段,在该阶段中,我们生成了打包的工件并将其发布到Nexus:

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

This way we were using our build tool (Maven) to deploy the artifact using one of its plugins (maven deploy plugin). 这样,我们使用构建工具(Maven)通过其插件之一(maven deploy插件)部署工件。 With Nexus Jenkins Plugin we can decouple these functionalities which makes sense to me: Gradle is in charge of building, testing and packaging the artifact and the Jenkins Nexus Plugin will upload it to Nexus. 使用Nexus Jenkins插件,我们可以将这些功能分离开来,这对我来说很有意义:Gradle负责构建,测试和打包工件,Jenkins Nexus插件会将其上传到Nexus。

The point is, following Jenkins Nexus Plugin documentation I have to use a step like the following: 关键是,根据Jenkins Nexus插件文档,我必须使用类似以下的步骤:

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']]]

Since I want to produce a resusable pipeline, I don't want to hardcode the following properties in the step: 由于我想产生可重用的管道,因此我不想在该步骤中硬编码以下属性:

  1. groupId groupId
  2. artifactId artifactId
  3. packaging 打包
  4. version

If I were working with Maven I would use the readMavenPom step from the Pipeline Utility steps plugin to pass them to the nexusPublisher step: 如果我正在使用Maven,则可以使用Pipeline Utility步骤插件中的readMavenPom步骤将其传递给nexusPublisher步骤:

def pom = readMavenPom file: 'pom.xml'
...
nexusPublisher ... mavenCoordinate: [artifactId: pom.artifactId
...

My problem is how to get those four parameters from Gradle configuration in the pipeline. 我的问题是如何从管道的Gradle配置中获取这四个参数。

Suppose my build.gradle is as follows: 假设我的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'
}  
...

Are you using Groovy in your pipeline? 您在管道中使用Groovy吗? Gradle don't provide native support for parsing POM files. Gradle不提供解析POM文件的本机支持。 I recommend you to use XmlSlurper in Groovy. 我建议您在Groovy中使用XmlSlurper。

If your pipeline is in Groovy you can read these properties with Gradle with this code: 如果您的管道位于Groovy中,则可以使用以下代码使用Gradle读取以下属性:

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

If you want, you can check this values directly printing variables: 如果需要,可以直接打印变量来检查以下值:

println 'my pom version ' + pom.version
println 'my pom version ' + pom.artifactId
println 'my pom version ' + pom.groupId

More info about XmlSlurper: Official page of XmlSlurper 有关XmlSlurper的更多信息:XmlSlurper的官方页面

EDIT: 编辑:

If you don't have pom file and you have build.gradle file, I recommend you to read this post: 如果您没有pom文件,但是具有build.gradle文件,建议您阅读以下内容:

how to read a properties files and use the values in project gradle script? 如何读取属性文件并使用项目gradle脚本中的值?

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

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