如何扩展gradle中的现有任务?

问题描述

我创建了一个新任务 jarWithDependencies 来创建一个我可以执行的 jar 文件。它正确地打包了所有依赖项,但它实际上不包含我自己的代码,因此无法运行,因为它找不到我的主文件。如果我改为修改现有的 jar 任务,它工作得很好,但我丢失了标准的 jar 任务。

我假设标准的 jar 任务做了一些我的任务没有做的事情。我可以让我的任务扩展标准的 jar 任务吗?像一个子任务或类似的东西。看起来这么简单的事情,我却找不到。

以下是我在 build.gradle 中配置 jarWithDependencies 任务的方式。

val jarWithDependencies by tasks.creating(Jar::class) {
    description = "Jar with all dependencies"
    group = "build"
    archiveBaseName.set(archiveBaseName.get() + "-with-dependencies")
    configurations.compileClasspath.get().forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "de.lehrbaum.bot.translate.MainKt"
    }
}

如果我只是添加以下代码,那么认的 jar 任务会完成这项工作,但我会失去正常的 jar 任务。难道不应该扩展任务而不是覆盖它吗?

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "de.lehrbaum.bot.translate.MainKt"
    }
    configurations.compileClasspath.get().forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)