如何设置 Jenkinsfile 以将 maven 工件上传到 Artifactory

How to set Jenkinsfile for upload maven artifact to Artifactory

我的 .Jenkins 文件是这样的:

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
    }
  }
}

使用此方法,jenkins 上传工件而不进行 "maven's style" 布局(包子文件夹和 poms)。

我想将生成的工件上传到 Artifactory,就像正常作业上传它时选中 "Maven3-Artifactory Integration" 一样。

从 Artifactory Jenkins 插件版本 2.7.2 开始,您可以 运行 Maven 和 Gradle 使用 Artifactory pipeline DSL

使用新的 DSL,您的构建脚本将如下所示:

  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

您可以在 jenkins-pipeline-examples 中找到更多 Artifactory 管道 DSL 示例。