0
votes

I have recently switched from Maven to Gradle. So far my Jenkins declarative pipelines included a stage where we generated the packaged artifact and published it to 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). 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.

The point is, following Jenkins Nexus Plugin documentation I have to use a step like the following:

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
  2. 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:

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.

Suppose my build.gradle is as follows:

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'
}  
...
1

1 Answers

0
votes

Are you using Groovy in your pipeline? Gradle don't provide native support for parsing POM files. I recommend you to use XmlSlurper in Groovy.

If your pipeline is in Groovy you can read these properties with Gradle with this code:

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

EDIT:

If you don't have pom file and you have build.gradle file, I recommend you to read this post:

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