-1

java 1.8

Gradle 7

In my build.gradle

plugins {
    id 'application'
    id "com.nocwriter.runsql" version "1.0.3"
    id 'idea'
}


ext {
    distributeVersionNumber = "1.2.5"
    distributeOsFamily = "win"
    distributeVersionType = "Lite"
    vendorName = "SomeCompany"
    vendorURL = "www.some_host.com"
    projectShortName = "myApp"
    projectFullName = "My Application"
    javaMainClass = "${group}.Main"
    xml2SqlConverterMainClass = "${group}.dict.XML2SQLConverter"
    sqlStrategy = "${group}.db.SQLiteStrategy"
    buildResources = "${buildDir}/resources"
    buildResourcesMain = "${buildResources}/main"
    buildResourcesDbSqlitePath = "${buildResourcesMain}/db/sqlite"
    distArchiveFileName = "${projectShortName}-${version}-${distributeVersionType}-${distributeOsFamily}"
}


jar {
    exclude("db")
    exclude("scripts")
    archiveFileName = "${projectShortName}.jar"
    manifest {
        attributes(
                "Manifest-Version": "1.0",
                "Created-By": "Gradle ${gradle.gradleVersion}",
                "Built-By": System.properties["user.name"],
                "Built-On": new java.text.SimpleDateFormat("dd.MM.yyyy'T'HH:mm:ss").format(new Date()),
                //"Build-Revision": versioning.info.commit,
                "Build-Jdk": "${System.properties["java.version"]} (${System.properties["java.vendor"]} ${System.properties["java.vm.version"]})",
                "Build-OS": "${System.properties["os.name"]} ${System.properties["os.arch"]} ${System.properties["os.version"]}",
                "Implementation-Title": "${projectFullName}",
                "Implementation-Version": "${distributeVersionNumber}-${distributeVersionType}",
                "Implementation-Vendor": "$vendorName",
                "Implementation-Vendor-URL": "$vendorURL",
                "Sealed": "true",
                "Project-Type": "application",
                "Main-Class": "$javaMainClass",
                "SplashScreen-Image": "images/splash.png",
                "Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(" "))
    }
}

Create distribute like this:

gradle distZip

Ok. It's create distribution of my application. But in my application jar has MANINFEST.MF file with the next content:

   Manifest-Version: 1.0
    Implementation-Title: My Application
    Implementation-Version: 1.2.5-Lite
    Built-By: alexei
    Sealed: true
    Class-Path: ssp-0.5.7.jar log4j-1.2.17.jar jcalendar-1.4.jar sqlite-jd
     bc-3.36.0.3.jar commons-net-3.6.jar
    Built-On: 26.06.2022T13:30:25
    Project-Type: application
    Implementation-Vendor: Some Company
    Implementation-Vendor-URL: www.some_host.com
    Main-Class: com.mycompany.myapp.Main
    Build-OS: Windows 7 amd64 6.1
    Created-By: Gradle 7.3
    Build-Jdk: 1.8.0_231 (Oracle Corporation 25.231-b11)
    SplashScreen-Image: images/splash.png

So the question is: Why order of fields in MANIFEST.MF is not same as in build.gradle?

I need order just like in build.gradle

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 3
    Why do you care? Manifests use format based on 822 (and thus MIME) and the order of attributes within a section is ignored, except Manifest-Version and Signature-Version (if used) must be first; see https://docs.oracle.com/en/java/javase/17/docs/specs/jar/jar.html#notes-on-manifest-and-signature-files bullet 3. – dave_thompson_085 Jun 26 '22 at 14:37

1 Answers1

1

This is a bug in gradle https://github.com/gradle/gradle/issues/2295.

You can work around it by creating your own MANIFEST.MF as a file and then copying it into the jar with something like this:

task addManifest(dependsOn: jar) {
    zip {
        archiveExtension = 'jar'
        //Include the content of the original jar.
        from zipTree('build/libs/jarname.jar').matching {
            //But leave out the manifest file we want to replace.
            exclude 'META-INF/MANIFEST.MF'
        }
        //Then add our manifest file with the same name to replace it.
        into('META-INF') {
            from 'build/MANIFEST.MF'
        }
    }
    //Replace the original jar file with the modified one.
    copy {
        from "$buildDir/distributions"
        into "$buildDir/libs"      

    }
}
opticyclic
  • 7,412
  • 12
  • 81
  • 155