DEV Community

Query Filter
Query Filter

Posted on

gradle-69

import java.text.SimpleDateFormat
import java.util.jar.Manifest

// 1. Task to generate a standalone MANIFEST.MF file
tasks.register('generateTarManifest') {
    def manifestFile = layout.buildDirectory.file("tmp/tar-manifest/MANIFEST.MF")
    outputs.file(manifestFile)

    doLast {
        File file = manifestFile.get().asFile
        file.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", archiveVersion.getOrElse(project.version.toString()))
        attributes.putValue("Build-Date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date()))
        attributes.putValue("Build-Jdk", "${System.getProperty('java.version')} (${System.getProperty('java.vendor')} ${System.getProperty('java.vm.version')})")
        attributes.putValue("Build-OS", "${System.getProperty('os.name')} ${System.getProperty('os.arch')} ${System.getProperty('os.version')}")
        attributes.putValue("Compile-Source-JDK", project.sourceCompatibility.toString())
        attributes.putValue("Compile-Target-JDK", project.targetCompatibility.toString())

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

// 2. Include the standalone manifest into distTar
tasks.named('distTar') {
    dependsOn tasks.named('generateTarManifest')
    into("${project.name}-${project.version}/META-INF") {
        from tasks.named('generateTarManifest')
    }
}

// 3. Inspection task to report TAR contents and embedded Manifest
tasks.register('inspectDistTar') {
    dependsOn tasks.distTar

    doLast {
        File tarFile = tasks.distTar.archiveFile.get().asFile

        if (!tarFile.exists()) {
            println "ERROR: Archive file not found at ${tarFile.absolutePath}"
            return
        }

        def sizeInMB = String.format("%.2f", tarFile.length() / (1024.0 * 1024.0))
        def timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())

        println "--------------------------------------------------------"
        println "BUILD SUCCESSFUL: Distribution Artifact Generated"
        println "File     : ${tarFile.name}"
        println "Path     : ${tarFile.absolutePath}"
        println "Size     : ${sizeInMB} MB (${tarFile.length()} bytes)"
        println "Timestamp: ${timestamp}"
        println "--------------------------------------------------------"

        // Report Archive Content
        println "--- Contents of ${tarFile.name} ---"
        int fileCount = 0
        File extractedManifest = null

        tarTree(tarFile).visit { details ->
            if (!details.directory) {
                println " [${++fileCount}] ${details.relativePath}"
                // Locate the standalone MANIFEST.MF inside the archive
                if (details.name.equalsIgnoreCase('MANIFEST.MF')) {
                    extractedManifest = details.file
                }
            }
        }

        println "--------------------------------------------------------"

        // Report Manifest Content directly from the extracted MANIFEST.MF
        if (extractedManifest != null && extractedManifest.exists()) {
            println "MANIFEST ENTRIES (from ${extractedManifest.name}):"
            Manifest manifest = new Manifest(extractedManifest.inputStream())
            manifest.mainAttributes.each { key, value ->
                println "  ${key}: ${value}"
            }
        } else {
            println "MANIFEST ENTRIES: No standalone MANIFEST.MF file found inside tarball."
        }
        println "--------------------------------------------------------"
    }
}

// Run inspection automatically after distTar
tasks.distTar.finalizedBy tasks.inspectDistTar
Enter fullscreen mode Exit fullscreen mode

Top comments (0)