Gradle 可以将不同的工件上传到不同的 Maven 存储库吗?

Can Gradle upload different artifacts to different Maven repos?

我正在使用 Gradle 2.3 和新的 maven-publish 插件。

是否可以控制插件,使其根据项目生成的工件类型选择目标存储库?

我正在使用 'os-package' plugin 生成 RPM,效果很好。我们还有一个 Artifactory 服务器配置为同时充当 jar 仓库和 yum 仓库。

手动,我可以将 jars 上传到项目 proj-dev-local 并将 rpms 上传到 proj-yum-local,这对于 rpms 和 jars 的所有依赖性解析都很好。

现在,我想设置 Gradle 任务来发布这两种类型的工件,每个都发布到 Artifactory 中的正确存储库。

我知道 Gradle 的官方 Artifactory 插件无法处理多个发布存储库,但我认为使用 maven-publish 插件或其他一些机制应该可以。

如果我在一个项目中只有 jar 工件,而在另一个项目中只有 rpm,我可以使用根项目的以下配置轻松地(嗯...)让它工作。

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"
                        }
                    }
                }
            }
        }
    }
}

然而,这不是我的软件项目的性质 - Rpm 项目几乎都是 java 项目。 在下面的测试用例中,我有两个子项目,仅应用 'java' 插件的 jarproject 和同时应用 'java' 和 'os-package' 的 rpmproject

rpmproject 中,我将 jar 发布到我的 yum 存储库中,反之亦然。

例如,是否有某种方法可以修改 publishMavenRpmPublicationToMavenRepository 任务,使其忽略某个回购协议?

$ 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

编辑: 感谢 Mark Vieira,当我在上述代码段之后的 subprojects 闭包中添加以下代码时,能够使它全部工作。

//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) {
    }
}

我仍然希望看到一种更美观的方式,因此欢迎进一步的回答,但至少这个解决方案对我有用。

您可以只禁用该特定发布任务。

model {
    tasks.publishMavenRpmPublicationToMavenRepository {
        enabled = false
    }
}

此外,您可以为您的存储库命名,这样任务名称更有意义。

maven {
    name = 'yum'
}