DEV Community

Query Filter
Query Filter

Posted on

gradle-68

import java.text.SimpleDateFormat
import java.util.jar.JarFile

// 1. Attach custom manifest attributes to your standard jar task
jar {
    manifest {
        attributes(
            "Built-By": System.getProperty('user.name'),
            "Implementation-Version": archiveVersion,
            "Build-Date": new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date()),
            "Build-Jdk": "${System.getProperty('java.version')} (${System.getProperty('java.vendor')} ${System.getProperty('java.vm.version')})",
            "Build-OS": "${System.getProperty('os.name')} ${System.getProperty('os.arch')} ${System.getProperty('os.version')}",
            "Compile-Source-JDK": project.sourceCompatibility,
            "Compile-Target-JDK": project.targetCompatibility
        )
    }
}

// 2. Inspection task to report TAR contents and Manifest entries
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 extractedJar = null

        tarTree(tarFile).visit { details ->
            if (!details.directory) {
                println " [${++fileCount}] ${details.relativePath}"
                // Locate internal application JAR to read its manifest
                if (details.name.endsWith('.jar') && extractedJar == null) {
                    extractedJar = details.file
                }
            }
        }

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

        // Report Manifest Content from the embedded application JAR
        if (extractedJar != null && extractedJar.exists()) {
            println "MANIFEST ENTRIES (from internal ${extractedJar.name}):"
            JarFile jf = new JarFile(extractedJar)
            def manifestAttributes = jf.manifest.mainAttributes
            manifestAttributes.each { key, value ->
                println "  ${key}: ${value}"
            }
            jf.close()
        } else {
            println "MANIFEST ENTRIES: No embedded JAR file found inside tarball."
        }
        println "--------------------------------------------------------"
    }
}

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

Top comments (0)