How to set Jenkinsfile for upload maven artifact to Artifactory

匆匆过客 提交于 2021-02-06 11:24:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/39412014/how-to-set-jenkinsfile-for-upload-maven-artifact-to-artifactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!