首页 > 解决方案 > 如何在 Gradle 生成的 jar 中生成 INDEX.LIST 文件?

问题描述

我正在将现有的 Ant 构建移动到 Gradle,并试图确保新构建生成的工件与旧构建相同。

一个现有的子项目根据以下内容生成一个带有INDEX.LIST文件的 jar: https ://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#JAR_Index

我想让 Gradle Jar 任务将此文件包含在其输出中,但似乎找不到任何内置方法来执行此操作。到目前为止,我为在我的根项目中启用此功能而构建的是:

allprojects { project ->
    /**
     * If we want to generate jars with META-INF/INDEX.LIST existing and populated, Gradle's jar task
     * will NOT do this by default.  So, at the END of running
     * a jar task, we can then call Ant's jar task, which allows for UPDATING an existing jar using
     * the index = true attribute argument, which will create that file with appropriate contents.
     *
     * This bit of code here will write a self-configuring method to every Jar task in the project which can do this.
     *
     * To make a Jar task use this option, do:
     *
     * <code>
     *     jar {
     *         withIndex()
     *     }
     * </code>
     */
    tasks.withType(Jar) { task ->
        task.ext.withIndex = {
            doLast {
                ant.jar(update: true, index: true, destfile: task.getArchiveFile().get().asFile.path)
            }
        }
    }
}

这似乎工作正常。

我是否错过了更好的(内置)方法来做到这一点?

标签: javagradlejar

解决方案


推荐阅读