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

时间:2015-02-25 14:19:10

标签: maven gradle publishing artifactory

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

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

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

手动,我可以将jar上传到项目proj-dev-local,并将rpms上传到proj-yum-local,这适用于rpms和jar的所有依赖项解析。

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

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

如果我在一个项目中只有jar工件而在另一个项目中只有rpms,我可以很容易地(好......)使用根项目的以下配置来使其工作。

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项目。 在下面的测试用例中,我有两个子项目,jarproject只适用于' java'插件,rpmproject同时适用于' java'和' os-package'。

rpmproject中,我在我的yum repo中发布了jar,反之亦然。

例如,是否有某种方法可以修改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) {
    }
}

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

1 个答案:

答案 0 :(得分:2)

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

model {
    tasks.publishMavenRpmPublicationToMavenRepository {
        enabled = false
    }
}

此外,您可以提供您的回购名称,以便任务名称更有意义。

maven {
    name = 'yum'
}