Search code examples
javamavenmaven-assembly-plugin

Alternative to maven assembly:single for packaging


What are the alternatives for packaging a standalone java application apart from creating a big fat assembly, which is horrible in some scenarios as

1) Conflicting files (with same names eg. reference.xml) in resource path of two or more jars get overriden by the lucky one.

2) Replacing a single jar is not possible without extracting and merging and compressing again.

Is there a solution more on the lines of an exploded war file, with all the libs in a lib folder and main class file's jar containing manifest entries. I am sure i had done that in ant and could surely be done in maven too.


Solution

  • Assembly plugin will be the good choice with a custom descriptor:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
    </configuration>
    </plugin>
    

    And in the 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>bin</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.artifactId}-${project.version}.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
    </assembly>
    

    All the dependencies will be seperate jars in the lib directory next to the project jar file.