This is in response to the near-universally accepted answer to an old question. Since the answer is rather old (2009) and since it isn't working for me, I thought I'd ask to see if anyone knows why.
My use case is developing a gradle plugin for internal use. Whenever the plugin is applied, I'd dearly like to be able to print what version of my plugin is being used. So I write the version into the plugin's manifest when the plugin is being built and attempt to read it back from the manifest when the plugin is applied, using the technique in the linked answer.
I wrote up some code along the lines of the answer but it's not working. I added a bunch of debugging code, and I can see that the code first finds the Manifest provided by the JDK itself:
Manifest-Version: 1.0
Created-By: 1.7.0_07 (Oracle Corporation)
Then it finds my manifest, the one I am looking for, but throws a FileNotFoundException:
java.io.FileNotFoundException: JAR entry META-INF/MANIFEST.MF not found in {path to my plugin jar in my local maven repository}. How could
Enumeration<URL> resources = getClass().getClassLoader()
.getResources("META-INF/MANIFEST.MF");
find the manifest and then throw a FileNotFoundException when trying to read it with:
Manifest manifest = new Manifest(resources.nextElement().openStream());
This is the line where the exception is thrown.
Can someone explain this odd behavior or come up with another, perhaps newer way of reading the manifest?
This is running under Eclipse, in Windows, by the way. Haven't tried it yet on Linux, where it might actually work, but I'd like it to work in both cases.