3

How to add manifest files in jar files?

plugins {
        id 'java'
        id 'application'
}

application {
        mainClassName = 'com.Main'
}

jar {
        from "MANIFEST.MF"
}

sourceCompatibility = 11

when I try to execute jar, I get following :

% java -jar tmpApp.jar
no main manifest attribute, in tmpApp.jar

Thelouras
  • 852
  • 1
  • 10
  • 30
LightSith
  • 795
  • 12
  • 27

1 Answers1

9

Here’s how you can generate an appropriate manifest file in the jar task of your build:

jar {
    manifest {
        attributes 'Main-Class': application.mainClassName
    }
}

Alternatively, you can use a custom file for the manifest:

jar {
    manifest {
        from 'MANIFEST.MF'
    }
}

You can even create a mixture of both:

jar {
    manifest {
        // take the existing file as a basis for the generated manifest:
        from 'MANIFEST.MF'
        // add an attribute to the generated manifest file:
        attributes 'Main-Class': application.mainClassName
    }
}
Chriki
  • 15,638
  • 3
  • 51
  • 66