Alxa
Alxa

Reputation: 79

Why can't I read MANIFEST.MF file generated by maven when packaging jar?

I have two separate projects: one is spring-boot with jar packaging, and one is a non-spring-boot app with war packaging.

In my spring-boot application, I use maven-jar-plugin to generate MANIFEST.MF with custom entries like following:

<plugin>
   <artifactId>maven-jar-plugin</artifactId>
   <version>${maven.jar.version}</version>
   <configuration>
      <archive>
         <addMavenDescriptor>false</addMavenDescriptor>
         <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            <packageName>${project.artifactId}-${project.version}</packageName>
         </manifest>
         <manifestEntries>
            <Custom-Entry>someValue</CustomEntry>
         </manifestEntries>
      </archive>
   </configuration>
</plugin>

At runtime, when I try to get manifest:

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

It can't find my manifest, instead, it takes first arbitrary manifest from another jar (e.g. from lobmok's jar). I tried to iterate over all the available manifests in classpath, but it appears that my manifes isn't there. But when I create MANIFEST.MF file myself in resources/META-INF/, I get right one from getResourseAsStream()

I don't know why this happens. Probably, becuase manifest is being searched not in jar file, but in target/classes/META-INF.

Now, when I work with another application, which is war archived, and use maven-war-plugin with exactly the same configuration, it does work properly. But I don't use

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

I use

servletContext.getResourceAsInputStream("META-INF/MANIFEST.MF")

So, basically, the thing is that I need to use servletContext instead of classLoader. Thus, I tried to make a workaround in my first spring-boot app and do something like:

@Autowired
ServletContext sc;

public void init() {
    sc.getResourceAsInputStream("META-INF/MANIFEST.MF");
}

But again, it returned null.

I also tried to do the following:

public CustomServletContextListener extends ServletContextListener {
    @Override
    public void contextInitialized(final ServletContextEvent event) {
        event.getServletContext().getResourseAsInputStream("META-INF/MANIFEST.MF");
    }
}

Still, null it is

So, I guess that the problem is in archive format. Something goes wrong in jar application. Any ideas what is actually happening here?

Upvotes: 0

Views: 499

Answers (0)

Related Questions