Skip to content

Document how to handle MANIFEST.MF in native image with Maven #42412

@krezovic

Description

@krezovic

When native image is built, there is no MANIFEST.MF for the application being built. Running "MyApp.class.getPackage().getImplementationVersion()" will return "null" in such scenario.

This is expected, as MANIFEST.MF is written by maven-jar-plugin, but native image build operates on target/classes dir itself.

How would one proceed with "get version of the currently running app" when running as native executable?

Consider the following example: https://github.com/krezovic/native-image-demo

@SpringBootApplication
public class DemoApplication {
	private static final String VERSION;

	static {
		VERSION = DemoApplication.class.getPackage().getImplementationVersion();
	}

	public static void main(String[] args) {
		log.info("Running version: {}", VERSION);
		SpringApplication.run(DemoApplication.class, args);
	}
}
./mvnw clean package -P native
./mvnw native:compile

will produce

$ ls -l target
total 104128
drwxr-xr-x 5 armin armin     4096 Sep 22 13:45 classes
-rwxr-xr-x 1 armin armin 85846072 Sep 22 13:46 demo
-rw-r--r-- 1 armin armin 20596296 Sep 22 13:45 demo-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 armin armin   137454 Sep 22 13:45 demo-0.0.1-SNAPSHOT.jar.original
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 generated-sources
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 generated-test-sources
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 graalvm-reachability-metadata
drwxr-xr-x 2 armin armin     4096 Sep 22 13:45 maven-archiver
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 maven-status
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 spring-aot
drwxr-xr-x 2 armin armin     4096 Sep 22 13:45 surefire-reports
drwxr-xr-x 3 armin armin     4096 Sep 22 13:45 test-classes
drwxr-xr-x 2 armin armin     4096 Sep 22 13:45 test-ids

Running

$ java -jar target/demo-0.0.1-SNAPSHOT.jar
13:47:41.896 [main] INFO com.example.demo.DemoApplication -- Running version: 0.0.1-SNAPSHOT

Running native executable, however

$ target/demo
13:48:04.685 [main] INFO com.example.demo.DemoApplication -- Running version: null

As expected, there's no MANIFEST.MF inside target/classes/META-INF

$ ls -l target/classes/META-INF
total 4
drwxr-xr-x 6 armin armin 4096 Sep 22 13:45 native-image

It is only present inside final JAR file

$ jar -tf target/demo-0.0.1-SNAPSHOT.jar | grep META-INF
META-INF/
META-INF/MANIFEST.MF
META-INF/services/
META-INF/services/java.nio.file.spi.FileSystemProvider
META-INF/native-image/
META-INF/native-image/ch.qos.logback/
META-INF/native-image/ch.qos.logback/logback-classic/
META-INF/native-image/ch.qos.logback/logback-classic/1.5.8/
META-INF/native-image/com.example/
META-INF/native-image/com.example/demo/
META-INF/native-image/com.fasterxml.jackson.core/
META-INF/native-image/com.fasterxml.jackson.core/jackson-databind/
META-INF/native-image/com.fasterxml.jackson.core/jackson-databind/2.17.2/
META-INF/native-image/org.apache.tomcat.embed/
META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/
META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/10.1.30/
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/demo/
META-INF/native-image/ch.qos.logback/logback-classic/1.5.8/reflect-config.json
META-INF/native-image/ch.qos.logback/logback-classic/1.5.8/resource-config.json
META-INF/native-image/com.example/demo/reflect-config.json
META-INF/native-image/com.example/demo/resource-config.json
META-INF/native-image/com.example/demo/native-image.properties
META-INF/native-image/com.fasterxml.jackson.core/jackson-databind/2.17.2/reflect-config.json
META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/10.1.30/reflect-config.json
META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/10.1.30/resource-config.json
META-INF/maven/com.example/demo/pom.xml
META-INF/maven/com.example/demo/pom.properties

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions