Home > Enterprise >  How to add properties to java Manifest using Maven
How to add properties to java Manifest using Maven

Time:03-10

I migrated from Java 8 to Java 11 and from Ant project to Maven project, the manifest lost the Project version and build date information.

I printed all Main attributes of Manifest using the following code:

        manifest = new Manifest(new URL(manifestPath).openStream());
        Attributes attr = manifest.getMainAttributes();
        
        // Enumerate each attribute
        for (Iterator it=attr.keySet().iterator(); it.hasNext(); ) {
            // Get attribute name
            Attributes.Name attrName = (Attributes.Name)it.next();
            String attrValue = attr.getValue(attrName);
            
            AppLogger.getInstance().Log(attrName   ": "   attrValue);

            }

Here is all the attributes names that I get: Main-Class, Class-Path, Build-Jdk, Built-By, Created-By, Manifest-Version, jar

There is no project version or build date.

What change should I do in my POM file, to add this information?

CodePudding user response:

You can do something like this in your pom.xml:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <archive>
            <manifestEntries>
              <Build-Jdk>${java.version} (${java.vendor} ${java.vm.version})</Build-Jdk>
              <Build-OS>${os.name} ${os.arch} ${os.version}</Build-OS>
              <Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
              <Project-Version>${project.version}</Project-Version>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

References: https://andresalmiray.com/customize-jar-manifest-entries-with-maven-gradle/, https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html

  • Related