Upload an RPM to Artifactory from Gradle

2019-02-22 18:51发布

问题:

How can I upload an RPM file to Artifactory using Gradle? Gradle always uploads the files using a maven-style directly layout which is inappropriate for a YUM repository.

回答1:

The issue here is that Gradle insists on uploading everything in a maven-style directory format of group-id/version/artifact, while a yum repository needs a flat layout. There are two approaches here - using the Artifactory plugin or Gradles newer publishing mechanism. I could only get this to work with the latter.

I assume here that you're using the Gradle ospackage plugin and already have an RPM build created. In my case the name of the RPM task is distRpm. For example:

task distRpm(type: Rpm) {
    packageName = 'my_package'
    version = version
    release = gitHash
    arch = 'X86_64'
    os = 'LINUX'
    // Etc
}

Add the ivy publish plugin to your project:

apply plugin: 'ivy-publish'

And then add a publishing block:

publishing {
    publications {
        rpm(IvyPublication) {
            artifact distRpm.outputs.getFiles().getSingleFile()
            /* Ivy plugin forces an organisation to be set. Set it to anything
               as the pattern layout later supresses it from appearing in the filename */
            organisation 'dummy'
        }
    }
    repositories {
        ivy {
            credentials {
                username 'yourArtifactoryUsername'
                password 'yourArtifactoryPassword'
            }
            url 'https://your-artifactory-server/artifactory/default.yum.local/'
            layout "pattern", {
                artifact "${distRpm.outputs.getFiles().getSingleFile().getName()}"
            }
        }
    }
}

The Ivy Publication allows you to specify the directory and filename pattern for upload. This is overwritten to be simply the exact filename of the RPM.



回答2:

This is my code snippets with Gradle Artifactory Plugin

Apply plugins:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.0"
    }
}

apply plugin: 'ivy-publish'
apply plugin: 'com.jfrog.artifactory'

Configure artifactory

artifactoryPublish {}.dependsOn(buildRpm)

publishing.publications.create('yum-publication', IvyPublication) {
        artifact buildRpm.outputs.getFiles().getSingleFile()
}



artifactory {
    contextUrl = 'https://artifactory.acme.com/artifactory'   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        //A closure defining publishing information
        repository {
            repoKey = 'demo-yum'   //The Artifactory repository key to publish to
            username ="${artifactory_user}"
            password = "${artifactory_password}"
            ivy {
                 artifactLayout = "${buildRpm.outputs.getFiles().getSingleFile().getName()}"
            }
        }
        defaults {
            //List of Gradle Publications (names or objects) from which to collect the list of artifacts to be deployed to Artifactory.
            publications ('yum-publication')

            publishBuildInfo = false   //Publish build-info to Artifactory (true by default)
            publishArtifacts = true   //Publish artifacts to Artifactory (true by default)
            publishPom = false   //Publish generated POM files to Artifactory (true by default).
            publishIvy = false   //Publish generated Ivy descriptor files to Artifactory (true by default).
        }
    }
}