Simpliest way to add an attribute to a jar Manifest in Maven

As of today I needed to add some manifest attributes for signed Java applet. I found it very straightforward with maven-jar-plugin. Just put required attributes to src/main/resources/META-INF/MANIFEST.MF:

    Permissions: all-permissions

Then just configure maven-jar-plugin plugin:

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>

And the result was:

    Manifest-Version: 1.0
    Build-Jdk: 1.7.0_51
    Built-By: bart
    Permissions: all-permissions
    Created-By: Apache Maven 3.0.5
    Archiver-Version: Plexus Archiver

    Name: name/prokop/bart/fps/util/BartDate.class
    SHA-256-Digest: XatHlhiWAK3ArocdOcVPCS3ftOcokJNlUeRhKPTHUKs=

In the end I just used the antrun plugin like the following, antcontrib is used to loop over the list of jars:

build-trusted.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a wrapper for all the other build files. -->
<project basedir="." name="project_name">

  <target name="addTrustedLibraryProperty">
    <jar file="${jarFile}" update="true">
      <manifest>
        <attribute name="Trusted-Library" value="true" />
      </manifest>
    </jar>
  </target>

  <target name="addTrustedLibraries">
    <ac:foreach target="addTrustedLibraryProperty" param="jarFile" xmlns:ac="antlib:net.sf.antcontrib">
      <path>
        <fileset dir="target/lib" includes="**/*.jar" />
      </path>
    </ac:foreach>
  </target>

</project>

In the pom

<plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>add-trusted-library-attribute</id>
            <phase>package</phase>
            <configuration>
              <target>
                <ant antfile="${basedir}/build-trusted.xml">
                  <target name="addTrustedLibraries" />
                </ant>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
              <exclusion>
                <groupId>ant</groupId>
                <artifactId>ant</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-nodeps</artifactId>
            <version>1.8.1</version>
          </dependency>
        </dependencies>
      </plugin>

You can do that with the Maven JAR Plugin during the creation of the JAR file. Add the following to your pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifestEntries>
                <Trusted-Library>true</Trusted-Library>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jarsigner-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <id>sign</id>
            <goals>
                <goal>sign</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <keystore>/path/to/testkeystore</keystore>
        <alias>myalias</alias>
        <storepass>test123</storepass>
    </configuration>
</plugin>

The main attributes as specified in the JAR File Specification are available as dedicated elements, e.g.:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <manifestEntries>
                <Trusted-Library>true</Trusted-Library>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

See the Maven Archiver Reference for further information.

To modify the manifest inside an existing jar file create a text file, e.g. mymanifest.mf which contains the required properties:

Trusted-Library: true

You can add the attributes of this file to an existing jar by executing the following command:

jar -cfm file-to-be-modified.jar mymanifest.mf

This will modify the manifest.mf inside the given jar.

Tags:

Maven

Jar

Maven 3