Jolta
Jolta

Reputation: 2725

Can Gradle upload different artifacts to different Maven repos?

I'm using Gradle 2.3 and the new maven-publish plugin.

Is it possible to control the plugin so that it selects the target repo based on the type of artifacts the project is producing?

I'm generating RPMs using the 'os-package' plugin, which works fine. We also have an Artifactory server configured to act as both jar repo, and a yum repo.

Manually, I can upload jars to the project proj-dev-local and rpms to proj-yum-local, which works fine for all dependency resolution of rpms and jars.

Now, I want to set up Gradle tasks to publish these two types of artifacts, each to the correct repo in Artifactory.

I understood that the official Artifactory plugin for Gradle is not able to handle several publishing repos, but I think it should be possible using the maven-publish plugin, or some other mechanism.

If I only have jar artifacts in one project and rpms in a separate one, I can easily (well...) get it working using the following configuration of the root project.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath (group: 'com.netflix.nebula', name: 'gradle-ospackage-plugin', version: '2.0.3')
    }
}

apply plugin: 'java'
version = '0.1.1'

subprojects {
    apply plugin: 'maven-publish'
    version = parent.version
    plugins.withType(JavaPlugin) {
        publishing {
            publications {
                mavenJava(MavenPublication) {
                    from components.java
                    repositories {
                        maven {
                            url "https://url/artifactory/proj-dev-local"
                        }
                    }
                }
            }
        }
    }
    plugins.matching {it.class.name == "com.netflix.gradle.plugins.packaging.CommonPackagingPlugin"}.all {
        ospackage {
            vendor = 'myCompany'
        }
        publishing {
            publications {
                mavenRpm(MavenPublication) {
                    artifact buildRpm //buildRpm is a task added by the os-package plugin
                    repositories {
                        maven {
                            url "https://url/artifactory/proj-yum-local"
                        }
                    }
                }
            }
        }
    }
}

However, such is not the nature of my software projects - the Rpm projects are almost all also java projects. In the below test case, I have two subprojects, jarproject which only applies the 'java' plugin, and the rpmproject which applies both 'java' and 'os-package'.

In the rpmproject, I get the jar published in my yum repo, and vice versa.

Is there, for example, some way to modify the publishMavenRpmPublicationToMavenRepository task, so it ignores a certain repo?

$ gradlew publish  
:jarproject:generatePomFileForMavenJavaPublication  
:jarproject:compileJava UP-TO-DATE
:jarproject:processResources UP-TO-DATE
:jarproject:classes UP-TO-DATE
:jarproject:jar UP-TO-DATE
:jarproject:publishMavenJavaPublicationToMavenRepository
Uploading: test/jarproject/0.1.1/jarproject-0.1.1.jar to repository remote at https://url/artifactory/proj-dev-local
Transferring 0K from remote
Uploaded 0K
:jarproject:publish
:rpmproject:generatePomFileForMavenJavaPublication
:rpmproject:compileJava UP-TO-DATE
:rpmproject:processResources UP-TO-DATE
:rpmproject:classes UP-TO-DATE
:rpmproject:jar UP-TO-DATE
:rpmproject:publishMavenJavaPublicationToMaven2Repository
Uploading: test/rpmproject/0.1.1/rpmproject-0.1.1.jar to repository remote at https://url/artifactory/proj-dev-local
Transferring 0K from remote
Uploaded 0K
:rpmproject:publishMavenJavaPublicationToMavenRepository
Uploading: test/rpmproject/0.1.1/rpmproject-0.1.1.jar to repository remote at https://url/artifactory/proj-yum-local
Transferring 0K from remote
Uploaded 0K
:rpmproject:buildRpm UP-TO-DATE
:rpmproject:generatePomFileForMavenRpmPublication
:rpmproject:publishMavenRpmPublicationToMaven2Repository
Uploading: test/rpmproject/0.1.1/rpmproject-0.1.1.rpm to repository remote at https://url/artifactory/proj-dev-local
Transferring 2K from remote
Uploaded 2K
:rpmproject:publishMavenRpmPublicationToMavenRepository
Uploading: test/rpmproject/0.1.1/rpmproject-0.1.1.rpm to repository remote at https://url/artifactory/proj-yum-local
Transferring 2K from remote
Uploaded 2K
:rpmproject:publish

BUILD SUCCESSFUL

EDIT: Thanks to Mark Vieira, was able to get it all working when I added the following code, in the subprojects closure, after the above snippet.

//Disable rpm publishing to jar repo and vice versa
gradle.taskGraph.whenReady { 
    try {
    tasks.getByPath("${project.getPath()}:publishMavenRpmPublicationToDevRepository").enabled = false;
    } catch (UnknownTaskException e) {
    }
    try {
    tasks.getByPath("${project.getPath()}:publishMavenJavaPublicationToYumRepository").enabled = false;
    } catch (UnknownTaskException e) {
    }
}

I still would have liked to see a more aesthetically pleasing way, so further answers are welcome, but at least this solution does the job for me.

Upvotes: 0

Views: 1806

Answers (1)

Mark Vieira
Mark Vieira

Reputation: 13466

You could just disable that particular publishing task.

model {
    tasks.publishMavenRpmPublicationToMavenRepository {
        enabled = false
    }
}

Additionally you can give your repos names so the task names make more sense.

maven {
    name = 'yum'
}

Upvotes: 2

Related Questions