Search code examples
mavenjarmaven-3

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


Since the last Java update, I need to tag my applet jars manifest with the Trusted-Library attribute to avoid warning popup when javascript is communicating with the applet. (see http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/mixed_code.html)

Manifest-Version: 1.0
Trusted-Library: true
Created-By: 1.6.0-internal (Sun Microsystems Inc.)

I never done such things before, is there a plugin which allow to do that in a seamless way or should I write one, or use the ant plugin?

The jars are already assembled and available through dependencies, copied in the target folder to be signed during the packaging. I'm using Maven 3


Solution

  • 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>