2
votes

Can someone help me to write the gradle script to upload the artifact to artifactory? I want to provide the artifact name through jenkins job as a parameter and the gradle script will upload the artifact with proper groupid, artifactid and version to artifactory with POM file. But I am not sure how to provide the zip as a parameter and I want the zip file upload with some version and build number from the jenkins job

apply plugin: 'java'
apply plugin: 'maven'

repositories {
    maven {
          url "http://localhost:8081/nexus/content/groups/public"
    }
}

dependencies {
    testCompile "junit:junit:3.8.1"
    compile "org.jbundle.util:org.jbundle.util.jbackup:2.0.0"
    compile "net.sf.webtestfixtures:webtestfixtures:2.0.1.3"
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://localhost:8081/nexus/content/repositories/snapshots") {
                authentication(userName: "admin", password: "admin123")
            }
            pom.version = "1.0-SNAPSHOT"
            pom.artifactId = "simple-project"
            pom.groupId = "com.example"
        }
    }
}

Below is the script which you suggested me

buildscript {
  repositories {
    maven {
      url "http://cm.t.th.com:8/artifactory/files-release-local"
    }
  }
}

allprojects {
  apply plugin: 'java'

  group = 'com.trlth.anaengine'
 // artifactId = 'reference-files'
  version = '0.0.6'
  status = 'integration'
}

// Setting this property to true will make the artifactoryPublish task
// skip this module (in our case, the root module):
artifactoryPublish.skip = true


def dir = 'build'
def f = file("$dir/test.txt")
artifacts {
    archives file: f, name: f.getName(), type: 'txt'
}

task wrapper(type: Wrapper) {
  gradleVersion = '2.3'
}
1
I was trying the below script - unknown

1 Answers

11
votes

Thee following steps should provide what you need: Step 1: Create a Jenkins job that runs a Gradle build and deploys its build info to Artifactory. To do that, create the Gradle job, and have it build the gradle2-example-ci-server found at: https://github.com/JFrogDev/project-examples/tree/master/gradle-examples You'll see that each artifact is deployed with the buildName and buildNumber properties matching the Jenkins build name and number.

Step 2: Now that you have a working job and can see the Jenkins build name and number in Artifactory, modify the build.gradle code as you wish - you'll probably want to remove the code being built and have the artifacts being deployed removed.

Step 3: You'd now want to add your zip artifact to the list of artifacts published to Artifactory. To achieve that, you'll need to add this artifact to the "archives" Gradle configuration (by default, the Gradle Artifactory Plugin which is applied by the Jenkins Artifactory Plugin, uses the archives configuration to fetch the list of artifacts deployed to Artifactory). To add a file to the archives configuration, you can add code similar to the following to your build.gradle:

def dir = 'build/outputs/artifacts' 
def f = file("$dir/my-zip-file.zip")
artifacts {
    archives file: f, name: f.getName(), type: 'zip'
}

Step 4: Now all you need to do is to replace the name of the zip file with a variable you send to the build from your Jenkins job. You can add a system property to the gradle command line in the job: clean artifactoryPublish -DfileName=my-zip-file.zip

and inside you gradle script, access it as follows:

def dir = 'build/outputs/artifacts'
def f = file("$dir/$fileName")
artifacts {
    archives file: f, name: f.getName(), type: 'zip'
}