How can I generate an INDEX.LIST file in a jar produced by Gradle?

Viewed 379

I'm moving an existing Ant build to Gradle and am trying to ensure that the artifacts produced by the new build are identical to the old ones.

One existing sub-project produces a jar with an INDEX.LIST file, as per: https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#JAR_Index

I'd like to get the Gradle Jar task to include this file in its output, but can't seem to find any built in way to do this. What I've built so far to enable this in my root project is:

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)
            }
        }
    }
}

And this seems to work fine.

Am I missing a better (built-in) way to do this?

0 Answers
Related