简体   繁体   English

使用Gradle-Artifactory插件发布子项目

[英]Publishing subprojects with the Gradle-Artifactory plugin

I have the following multi-project Gradle build: 我有以下多项目Gradle构建:

myapp/
    myapp-client/
        build.gradle
        src/** (omitted for brevity)
    myapp-shared/
        build.gradle
        src/** (omitted for brevity)
    myapp-server
        build.gradle
        src/** (omitted for brevity)
    build.gradle
    settings.gradle

Where myapp/build.gradle looks like: myapp/build.gradle如下:

subprojects {
    apply plugin: 'groovy'
    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    repositories {
        mavenCentral()
        maven {
            // My local/private Artifactory
            url "http://localhost:8081/artifactory/myapp-snapshots"
        }
    }

    dependencies {
        compile (
            'org.codehaus.groovy:groovy-all:2.3.7'
        )
    }
}

And where each of the 3 subproject Gradle files are currently very simple: 目前,3个子项目Gradle文件中的每一个都非常简单:

dependencies {
    compile (
        'org.apache.commons:commons-lang3:3.3.2'
    )
}

Here's what I'm trying to achieve: 这是我想要实现的目标:

  • When I run gradle clean build -Pversion=0.1.5 from the parent myapp directory, I want all three subprojects to be built with a version of 0.1.5; 当我从父myapp目录运行gradle clean build -Pversion=0.1.5 ,我希望所有三个子项目都以0.1.5的版本构建; hence myapp-client-0.1.5.jar , myapp-shared-0.1.5.jar and myapp-server-0.1.5.jar . 因此myapp-client-0.1.5.jarmyapp-shared-0.1.5.jarmyapp-server-0.1.5.jar Currently my build already does this, however... 目前我的构建已经这样做,但是......
  • If the client and shared JARs build successfully, I also want them published to my private Artifactory repo. 如果客户端和共享JAR成功构建,我也希望它们发布到我的私有Artifactory仓库。 (I don't wish to publish the server JAR). (我不希望发布服务器JAR)。

The most compact code I've been able to find and test/confirm for publishing a Gradle-built JAR to Artifactory is: 我能够找到并测试/确认将Gradle构建的JAR发布到Artifactory的最紧凑的代码是:

buildscript {
    repositories {
        maven {
            url 'http://localhost:8081/artifactory/plugins-release'
            credentials {
                username = "admin"
                password = "12345"
            }
            name = "maven-main-cache"
        }
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
    }
}

apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"

repositories {
    add buildscript.repositories.getByName("maven-main-cache")
    maven {
        url "http://localhost:8081/artifactory/myapp-snapshots"
    }
}

artifactory {
    contextUrl = "http://localhost:8081/artifactory"
    publish {
        repository {
            repoKey = 'myapp-snapshots'
            username = "admin"
            password = "12345"
            maven = true
        }
        defaults {
            publications ('mavenJava')
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

When I use this snippet inside a test build.gradle project, and run gradle artifactoryPublish , it correctly publishes my JAR to Artifactory. 当我在测试build.gradle项目中使用此代码段并运行gradle artifactoryPublish ,它会正确地将我的JAR发布到Artifactory。

My question: How can I add this snippet to my project such that running gradle clean build from the parent dir, on success, produces client and shared JARs being published to Artifactory? 我的问题: 我如何将这个片段添加到我的项目中,以便从父目录运行gradle clean build ,成功时,会生成发布到Artifactory的客户端和共享JAR?

Although not ideal, I would be willing to make the publishing a separate, second step if its not possible to do all this in one fell swoop. 虽然不理想,但如果不可能一下子完成所有这一切,我愿意将出版单独的第二步。 Any ideas? 有任何想法吗?

Using the artifactory plugin is the right way to do. 使用artifactory插件是正确的方法。 You need to apply it to allprojects and disable the deployment on the top-level project with artifactoryPublish.skip=true . 您需要将其应用于allprojects项目,并使用artifactoryPublish.skip=true禁用顶级项目上的部署。 You can find a fully working example of a project much similar to yours here . 你可以找到一个项目的全面工作的例子很多类似于你在这里

By default build is not dependent on artifactoryPublish ,so you need to run gradle build artifactoryPublish to upload the jars. 默认情况下, build不依赖于artifactoryPublish ,因此您需要运行gradle build artifactoryPublish来上载jar。 The reason is you might want to run a lot of builds until you decide to upload something. 原因是您可能希望运行大量构建,直到您决定上传某些内容。 You can establish the dependency by configuring build to be depending on artifactoryPublish : 您可以通过将build配置为依赖artifactoryPublish来建立依赖关系:

build(dependsOn: 'artifactoryPublish')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM