Search code examples
mavenjenkinsartifactoryjenkins-pipeline

How to set Jenkinsfile for upload maven artifact to Artifactory


I've my .Jenkinsfile like this:

properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab@srv']])
node {
  env.JAVA_HOME = tool 'JDK 7'
  def mvnHome = tool 'Maven 3.2.2'
  def nodeJS = tool 'IA_NodeJS'
  env.PATH = "${mvnHome}/bin:${nodeJS}/bin:${env.JAVA_HOME}/bin:${env.PATH}"

  stage ('checkout') {
    checkout scm
  }

  stage ('build') {
    gitlabCommitStatus("build") {
      // your build steps
      sh 'mvn clean install -Denv=dev -P !faster'
    }
  }

  stage ('upload') {
    gitlabCommitStatus("upload") {
      def server = Artifactory.server "artifactory@ibsrv02"
      def buildInfo = Artifactory.newBuildInfo()
      buildInfo.env.capture = true
      buildInfo.env.collect()

      def uploadSpec = """{
        "files": [
          {
            "pattern": "**/target/*.jar",
            "target": "libs-snapshot-local"
          }, {
            "pattern": "**/target/*.pom",
            "target": "libs-snapshot-local"
          }, {
            "pattern": "**/target/*.war",
            "target": "libs-snapshot-local"
          }
        ]
      }"""
      // Upload to Artifactory.
      server.upload spec: uploadSpec, buildInfo: buildInfo

      buildInfo.retention maxBuilds: 10, maxDays: 7, deleteBuildArtifacts: true
      // Publish build info.
      server.publishBuildInfo buildInfo
    }
  }
}

with this method jenkins uploads artifacts without make the "maven's style" layout (packages subfolder and poms).

I want to upload resulting artifact to Artifactory like a normal job uploads it with "Maven3-Artifactory Integration" checked.


Solution

  • From Artifactory Jenkins plugin version 2.7.2 you can run Maven and Gradle using Artifactory pipeline DSL.

    Using the new DSL your build script would look like this:

      def server = Artifactory.server "artifactory@ibsrv02"
      def buildInfo = Artifactory.newBuildInfo()
      buildInfo.env.capture = true
      def rtMaven = Artifactory.newMavenBuild()
      rtMaven.tool = MAVEN_TOOL // Tool name from Jenkins configuration
      rtMaven.opts = "-Denv=dev"
      rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
      rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
    
      rtMaven.run pom: 'pom.xml', goals: 'clean install', buildInfo: buildInfo
    
      buildInfo.retention maxBuilds: 10, maxDays: 7, deleteBuildArtifacts: true
      // Publish build info.
      server.publishBuildInfo buildInfo
    

    You can find more Artifactory pipeline DSL examples in the jenkins-pipeline-examples.