Search code examples
mavenmaven-assembly-pluginmeta-inf

correct use of the exclude term in maven assembly plugin


short: I need to filter all .java files and every META-INF folder from a set of jars and package the class files and resources from that jars into one single jar. I currently use the maven-assembly-plugin, but would try something else as long as it can easily be integrated into a maven build process.

long: I use maven to manage different stages of development for my tool. basic stage is freeware, second has some more features, third stage is all features)

That works fine so far, I use profiles to add the different source directories to the classpath and the sources are neatly compiled into the project.jar.

  • First problem: The .java sources included into the project via the profiles end up in the project.jar.

Then I use the maven-assembly-plugin to construct a final.jar that also contains the dependencies and in the end use launch4j to produce an executable for windows (the current target platform).

  • Second problem: The various META-INF parts from the dependency jars mix in the final.jar and I would want them all to be skipped.

I have searched for examples of the assembly.xml using the <exclude> tag, but did not find any that used my combination of dependencySet and <exclude>*.java</exclude>. I'm not even positive that I can do that.

Here is my assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <useDefaultExcludes>true</useDefaultExcludes>
            <!--<useTransitiveFiltering>true</useTransitiveFiltering>-->
            <!--<useStrictFiltering>true</useStrictFiltering>-->
            <excludes>
                <exclude>META-INF</exclude>
                <exclude>**/*.java</exclude>
                <exclude>*.java</exclude>
                <exclude>*:sources</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

My research so far:

I have googled with example assembly.xml exclude java but could not find examples that covered my problem. (I have also googled a lot the past days but did not save all I found) I have read http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html but could not apply that knowledge to my problem.


Solution

  • Okay, so I figured it out for me.

    first: to filter out java and other source files from source parts that were included using profiles I use:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.steamnet.oneClickWonder.awt.OCWController</mainClass>
                        </manifest>
                    </archive>
                    <excludes>
                        <exclude>**/*.java</exclude>
                        <exclude>**/*.form</exclude>
                    </excludes>
                </configuration>
            </plugin>
    

    The task of filtering the META-INF from the dependencies has gone away when I started using an installer so now I can just deliver mulitple jars with their own META-INF.

    So, as Michael-O stated this approach (using profiles to include additional source parts) may not be the correct one to do but it is very handy and I stick to it. With the excludes tag from the jar plugin the troubles with source files being added to the final jar also goes away.