使用Gradle Kotlin DSL和Travis CI在Bintray上发布Kotlin库

使用Gradle Kotlin DSL在Bintray上分发库

在我最新的博客文章中,几周后发布,我通知了使用的使用 Gradle Kotlin DSL. 以及它如何帮助描述构建脚本。在另一个早期的帖子中,我介绍了一个小型库,用于使用自定义kotlin dsl简化创建TLS / SSL套接字: 撒尿.

在这篇文章中,我们将调查如何为其他人提供的图书馆,实际上想要利用其他项目。最终,应该可以将Sekurity库列为简单 依赖性 在像Maven或Gradle这样的构建脚本中。由于库本身已被Gradle备份,因此我将展示在该构建的结尾处发布结果的工件。

哪里发表?

为了向任何人提供任何人的图书馆,需要发布到公开访问的方面。 Maven存储库就像着名 Maven Central.。除非您对图书馆的质量绝对肯定,否则建议将其分享到自定义存储库,而不是直接将其丢失,因为它有点失去控制它。一个替代方案被称为 Bintray. 它也将用于本文。创建帐户或只是使用GOTHUB,Google或Twitter帐户登录后,您可以在那里创建自己的存储库。

自动化构建

在使我们的构建启用以发布构建的伪影之前,该过程应在CI工具中自动化 特拉维斯CI.,它与GitHub很好地相互作用。要使它工作,必须通过将其添加到GitHub repo设置中启用它 服务 (看 Settings -> Integrations & services). Travis expects the repository to incorporate a .travis.yml configuration that tells it what to do. For a simple Java/Kotlin project using Gradle, the following will be sufficient:

language: java

before_install:
- chmod -R +x src
- chmod +x gradlew

script: ./gradlew clean build
deploy:
  provider: script
  script: ./gradlew bintrayUpload -PbintrayUser=$BINTRAY_USER
  -PbintrayApiKey=$BINTRAY_KEY
    on:
      branch: master

It's normally not necessary to specify the script command explicitly as long as you're using the default instructions. In the shown case, Gradle runs the build task first before it deploys via the gradle bintrayUpload taks, which we will learn about later. The deployment only happens for the master branch. The gradle deploy takes two arguments $BINTRAY_USER and $BINTRAY_KEY, which contain private data and are therefore securely stored in Travis directly. When the build is run, Travis takes care of replacing them.
您最终可以在特拉维斯启用存储库,并立即启动每个推送的构建。有关特拉维斯的更多信息,请 文件 强烈推荐。

Gradle建造

As shown earlier, the bintrayUpload Gradle task gets invoked in the build. Where does it come from? Let's have a look at the relevant parts of the gradle.build.kts file.

插件


plugins {
    kotlin("jvm") version "1.2.30"
    id("com.github.johnrengelman.shadow") version "2.0.2"
    `maven-publish`
    id("com.jfrog.bintray") version "1.8.0"
}
  • shadow 使用插件是因为我们需要使用其所有依赖项捆绑库以避免类路径冲突。
  • maven-publish plugin helps with creating the relevant artifact and a relevant pom.xml file that will also be published.
  • bintray plugin does all the heavy work at the end of the build. It refers to the maven-publish result and pushes the artifacts to Bintray.

插件配置

构建文件还为所有以前所示的插件添加自定义配置,如下所示:

I 阴影


val artifactID = "sekurity"

val shadowJar: ShadowJar by tasks
shadowJar.apply {
    baseName = artifactID
    classifier = null
}

shadow plugin uses the project's name as the artifact's baseName and the "all" qualifier by default, which is overridden here.

II Maven-Publish.


fun MavenPom.addDependencies() = withXml {
    asNode().appendNode("dependencies").let { depNode ->
        configurations.compile.allDependencies.forEach {
            depNode.appendNode("依赖性").apply {
                appendNode("groupId", it.group)
                appendNode("artifactId", it.name)
                appendNode("version", it.version)
            }
        }
    }
}

val publicationName = "tlslib"
publishing {
    publications.invoke {
        publicationName(MavenPublication::class) {
            artifactId = artifactID
            artifact(shadowJar)
            pom.addDependencies()
        }
    }
}

This one is a bit tricky. We reuse the result of shadow and add it to the publication which will be published under the artifactId that corresponds to the JAR's baseName. By default, the generated POM does not list the library dependencies, which is done by mapping each compile dependency to a valid 依赖性 XML node.

III Bintray.


fun findProperty(s: String) = project.findProperty(s) as String?

bintray {
    user = findProperty("bintrayUser")
    key = findProperty("bintrayApiKey")
    publish = true
    setPublications(publicationName)
    pkg(delegateClosureOf {
        repo = "撒尿"
        name = "撒尿"
        userOrg = "S1M0NW1."
        websiteUrl = "//airportparkinghotels.net"
        vcsUrl = "//github.com/s1monw1/TlsLibrary"
        setLabels("kotlin")
        setLicenses("MIT")
    })
}

bintray plugin is most important since it takes care of actually uploading the generated artifacts to Bintray itself. The user and key are obtained from the Gradle project properties, which are passed by Travis later on as already shown earlier. By referring to the publication created in maven-publish, all artifacts will be uploaded as specified. The pkg block describes the library's attributes and 在哪里,即,在什么存储库中,发布发生。

结果

所有这些块都合并了一个相当漂亮的地表构建脚本,可以在其中观察到 这个小麦 而且撒上了 存储库 本身。由此产生的工件在此处提供: //bintray.com/s1m0nw1/SeKurity/SeKurity

参考已发布的图书馆

既然库正在上传到BintRay,它可以列为任意构建文件中的依赖项。如果我们有另一个备份项目,我们首先需要将相关的存储库添加到存储库列表中:


repositories {
    mavenCentral()
    jcenter()

    maven {
        setUrl("//dl.bintray.com/s1m0nw1/SeKurity/")
    }
}

After that, the library can be added as a simple compile 依赖性 like this:


dependencies {
//...other dependencies
    compile("de.swirtz:sekurity:0.0.2")
}

搭档

如图所示,Gradle Plugins可用于设置适当的构建,可轻松向BintRay发布自定义库。为了自动化整个过程,只要进行新的更改,就可以使用像特拉维斯这样的CI工具来执行构建。

我希望小教程有助于您的构建!如果你喜欢,看看我的 推特 如果您对更多Kotlin的东西感兴趣,请遵循账户,谢谢。

2 thoughts on “使用Gradle Kotlin DSL和Travis CI在Bintray上发布Kotlin库

  • Nick Apperley

    在使用Gradle Kotlin DSL(不使用CI)的情况下,有一篇关于发布Kotlin多平台库的文章。文章中的样品唐’在Gradle 4.9的工作。

  • Yuri Schimke.

    是否有更好的方法来继承依赖关系。例如使用Kotlin-DSL设置开发人员和许可证部分可以通过

    pom {
    packaging = "jar"
    developers {
    developer {
    email.set("yuri@schimke.ee")
    id.set("yschimke")
    name.set("Yuri Schimke.")
    }
    }
    licenses {
    license {
    name.set("Apache License")
    url.set("http://opensource.org/licenses/apache-2.0")
    distribution.set("repo")
    }
    }
    scm {
    connection.set("scm:git://github.com/yschimke/okurl.git")
    developerConnection.set("scm:git:git@github.com:yschimke/okurl.git")
    url.set("//github.com/yschimke/okurl.git")
    }
    }

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *