Reading a native MANIFEST.MF file in a Java servlet

I am trying to read my own MANIFEST.MF resource in a Java servlet. My position: I have a WAR (with a manifest that I want to read) inside the EAR. There are several other WARs and JARs in the EAR. The path to the class is really long.

I tried several ways to find on the Internet, including StackOverflow.

I can read all MANIFEST.MF files using

this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); 

and repeat them. However, I do not know which of them - I do not even know the name of the implementation, since this is generated by the assembly. (I can guess the knowledge of the assembly, so I know that the correct manifest exists. However, I cannot guess the production code.)

Sure,

 this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"); 

returns a completely incorrect manifest from any other jar in the class path.

I also tried

 this.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF"); 

but it returns zero.

How to access the MANIFEST.MF file owned by WAR containing the current servlet?

+5
source share
1 answer

I also tried

 this.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF"); 

but it returns zero.

This path must begin with / to represent the absolute path of the WAR resource.

 this.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"); 

Using ClassLoader#getResourceXxx() does not make sense, since the native WAR manifest file is not in the classpath. It is located in webroot, next to /WEB-INF and all. Therefore, ServletContext#getResourceXxx() is the only way.

+3
source

Source: https://habr.com/ru/post/1235025/


All Articles