0

i follow this Reading my own Jar's Manifest guidelines to get manifest entries programmatically.

I am building jar file with maven-assembly-plugin. Manifest file is located in META-INF/MANIFEST.MF inside jar file.

Problem is that i am getting empty entry in any case. I tried to load using JarFile, Using ClassLoaderResources not effect.

But if add extra entries into pom file, using then i can read this entries if they are in separate section (separated with extra newline).

If i add entries to main attributes section with then they are invisible.

Is it possible that for some security reason reading programmatically manifest.mf file is disabled?

Community
  • 1
  • 1
simar
  • 1,782
  • 3
  • 16
  • 33
  • Why are you using the maven-assembly-plugin and not just the maven-jar-plugin? – Robert Scholte Aug 25 '14 at 14:59
  • it much powerfull. and i need a single jar. – simar Aug 25 '14 at 14:59
  • Strange things happens. I read manifest and see that is it empty, then dump it into file. File is contains all entries. I tried to read manifest file from file and same result, in memory is it shown as empty – simar Aug 25 '14 at 15:02
  • off-topic: The plugins have different purposes. maven-assembly-plugin is mainly used to bundle all kinds of different files into a single zip (or tar or any other archiver supported) for ditribution. If you want a single jar, use maven-jar-plugin. If you want a fat-client jar, use maven-shade-plugin. – Robert Scholte Aug 25 '14 at 15:36

1 Answers1

3

I dont know why you cant find your manifest entries without additional dependencies, but you shouldn't have any issues reading from a manifest. I do it like this:

private static final String MANIFEST_FILE_KEY = "META-INF/MANIFEST.MF";

......

        try (JarFile jar = new JarFile(URLDecoder.decode(jarFileURL.getPath(), "UTF-8"))) {
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (entry.getName().equals(MANIFEST_FILE_KEY)){
                    //found our manifest
                    Manifest manifest = new Manifest(jar.getInputStream(entry));
                    Attributes attributes = manifest.getMainAttributes();
                    for (Entry<Object, Object> key : attributes.entrySet()) {
                        //do something with the key values
                    }
                    break;
                }
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
Mark W
  • 2,791
  • 1
  • 21
  • 44
  • I also eventually came up with same solution. Problem with method getEntries(). It returns always empty hashmap. – simar Aug 26 '14 at 05:53