My linux world » Java – Manifest

Java - Manifest


Contents

Where is the manifest ?

By default the manifest can be found here in your artifact : “META-INF/MANIFEST.MF”.

How to fill the manifest file ?

The manifest file can be set :

Some plugins helps you to custom your manifest.

For jar artifacts :

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>${maven-jar-plugin.version}</version>
        <configuration>
          <archive>
            <index>true</index>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
              <mode>development</mode>
              <url>${project.url}</url>
              <key>value</key>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

For war artifacts :

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven-war-plugin.version}</version>
        <configuration>
          <archive>            
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
              <mode>development</mode>
              <url>${project.url}</url>
              <key>value</key>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

Getting manifest

// get manifest as input stream :
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
 
// read manifest:
Manifest manifest = new Manifest();
manifest.read(inputStream);

Manifest elements

Main attributes

To get the main Attributes for the Manifest :

Attributes mainAttributes = manifest.getMainAttributes();
 
for(Object attribute : mainAttributes.keySet()) {
  Attributes.Name attributeName = (Attributes.Name) attribute;
  String attributeValue = mainAttributes.getValue(attributeName);
}

Entries

To get entires for the Manifest :

Map<String, Attributes> entries = manifest.getEntries();
 
for(Object attribute : entries.keySet()) {
  Attributes.Name attributeName = (Attributes.Name) attribute;
  String attributeValue = entries.getValue(attributeName);
}

Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub