Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a custom MANIFEST.MF file while using the Maven Shade plugin?

When a project uses the Maven-jar-plugin, it's easy to include a custom manifest file in the jar, but I can't find a way to do the same thing with Maven shade. How can I use my own manifest file while using the "Maven-shade-plugin"?

Additional details:

My custom manifest file is located in "src/main/resources/META-INF/MANIFEST.MF". Maven is not including my file, instead it is being replaced in the final jar with a default Maven manifest file.

The reason I need a custom manifest file is to specify some JavaBeans classes in my manifest, for a swing component library. Multiple JavaBeans classes should be specified in the manifest file in the following format, as described here. Note that the empty lines (and the line grouping) are important for marking JavaBeans classes in the manifest.

Name: SomeBean1.class
Java-Bean: True

Name: SomeBean2.class
Java-Bean: True

Name: SomeBean3.class
Java-Bean: True

A list of attempted solutions (these did not work):

  1. This code only works when using the Maven jar plugin (not shade).

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
          </archive>
        </configuration>
      </plugin>
    
  2. This link says "As with all the examples here, this configuration can be used in all plugins that use Maven Archiver, not just Maven-jar-plugin as in this example." Based on that advice, I tried the following code, but this did not work either. (Maven still replaced my manifest file with the default manifest file.)

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration> 
                        <archive>
                            <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                        </archive>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>core</shadedClassifierName>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <minimizeJar>true</minimizeJar>      
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
  3. I cannot use the shade "ManifestResourceTransformer" as described here to do the job, for the following reason. I need to add JavaBeans classes to my manifest file as described above under "additional details". However, if I add manifest entries using the shade ManifestResourceTransformer, those entries are inserted into the manifest file in an unpredictable (random-seeming) ordering. For specifying JavaBeans classes, the ordering of the manifest entries (the line order) is important.

  4. I attempted to use IncludeResourceTransformer, but the below code produces the following error: "Error creating shaded jar: duplicate entry: META-INF/MANIFEST.MF".

                    <configuration> <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>core</shadedClassifierName><createDependencyReducedPom>false</createDependencyReducedPom>
                        <minimizeJar>true</minimizeJar>    
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>META-INF/MANIFEST.MF</resource>
                                <file>src/main/resources/META-INF/MANIFEST.MF</file>
                            </transformer>
                        </transformers>  
                    </configuration>
    
like image 699
BlakeTNC Avatar asked Jul 07 '16 10:07

BlakeTNC


1 Answers

The following pom configuration allows the programmer to replace the manifest file created by Apache Maven Shade plugin, with a custom manifest file. The custom manifest file should be placed in this directory in the maven project: "src/main/resources/META-INF/MANIFEST.MF"

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        ...
                        <transformers>

                            <!-- Don't do this: Avoid adding anything that makes shade create or modify a manifest file.
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.mypackage.MyMainClass</mainClass>
                            </transformer>
                            -->

                            <!-- Add a transformer to exclude any other manifest files (possibly from dependencies). -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>MANIFEST.MF</resource>
                            </transformer>

                            <!-- Add a transformer to include your custom manifest file. -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>META-INF/MANIFEST.MF</resource>
                                <file>src/main/resources/META-INF/MANIFEST.MF</file>
                            </transformer>

                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 51
BlakeTNC Avatar answered Nov 10 '22 03:11

BlakeTNC