Upload an RPM to Artifactory from Gradle

回眸只為那壹抹淺笑 提交于 2019-12-01 18:16:21

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.

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