DEV Community

Query Filter
Query Filter

Posted on

gradle-71

import groovy.json.JsonOutput
import java.text.SimpleDateFormat
import java.util.jar.Manifest

// 1. Task generating both internal MANIFEST.MF and external build/distributions/manifest.json
tasks.register('generateTarManifest') {
    def internalManifestFile = layout.buildDirectory.file("tmp/tar-manifest/MANIFEST.MF")
    def externalManifestFile = layout.buildDirectory.file("distributions/manifest.json")

    outputs.file(internalManifestFile)
    outputs.file(externalManifestFile)

    doLast {
        // --- A. Generate internal MANIFEST.MF ---
        File mfFile = internalManifestFile.get().asFile
        mfFile.parentFile.mkdirs()

        Manifest manifest = new Manifest()
        def attributes = manifest.mainAttributes
        attributes.putValue("Manifest-Version", "1.0")
        attributes.putValue("Built-By", System.getProperty('user.name'))
        attributes.putValue("Implementation-Version", project.version.toString())
        attributes.putValue("Build-Date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date()))

        mfFile.withOutputStream { os -> manifest.write(os) }

        // --- B. Generate external build/distributions/manifest.json ---
        File jsonFile = externalManifestFile.get().asFile
        jsonFile.parentFile.mkdirs()

        def manifestData = [
            project_name : project.name,
            version      : project.version.toString(),
            built_by     : System.getProperty('user.name'),
            build_date   : new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date())
        ]

        jsonFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(manifestData))
        logger.lifecycle("External manifest generated at: ${jsonFile.absolutePath}")
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)