首页 > 解决方案 > 在 nebula/gradle 中,如何将正在发布的版本注入正在发布的 jar 中?

问题描述

我们有一个从命令行运行的工具。命令之一是-version。

在我们转换为 nebula 发布插件之前,版本位于 gradle.properties 文件中,作为构建的一部分,我们将其从那里复制到 src/main/resources/version.txt 文件中,该文件后来被工具读取输出版本。

但是现在该版本永远不会在签入 git 的文件中。相反,它只有在星云释放过程中才知道。

我们想在 nebula 发布过程中获取那个版本,并注入到 nebula 即将发布的 jar 中。例如,它可以添加到清单中。

我们试图弄清楚如何做到这一点,但在网上看不到任何示例,文档中也没有任何相关内容。

标签: gradlejarversionnebula

解决方案


只需创建一个缓存 Nebula 动态推断的版本的任务。

由于您最初复制/创建src/main/resources/version.txt,我们将使用该模型我们的任务。

假设一个简单/标准的 Java 项目,使用Kotlin DSL

val cacheNebulaVersion by tasks.registering {
    mustRunAfter(tasks.named("release"))
    doLast {
        val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
        sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output.resourcesDir?.let {

            // If there are not existing resources in your project then you must create
            // the resources dir otherwise a FileNotFoundException will be thrown.
            if (!it.exists()) {
                it.mkdirs()
            }

            File(it, "version.txt").printWriter().use { out ->
                out.println(project.version)
            }
        }
    }
}

当我调用./gradlew clean build snapshot cacheNebulaVersion时,Nebula 生成的版本src/main/resources/version.txt构建输出中被缓存/创建。上面的任务没有将它与 jar 捆绑在一起。

希望这能让你知道该怎么做。

在此处输入图像描述


推荐阅读