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

时间:2016-09-09 12:49:29

标签: maven jenkins artifactory jenkins-pipeline

我的.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的样式”布局(包子文件夹和poms)。

我想将生成的工件上传到Artifactory,就像普通的工作上传“Maven3-Artifactory Integration”一样。

1 个答案:

答案 0 :(得分:17)

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

使用新的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示例。