Search code examples
javamavenmaven-assembly-pluginmaven-jar-plugin

How do you create a standalone application with dependencies intact using Maven?


I have a desktop Java application built using Maven 2 (but I could upgrade to Maven 3 if that helps) with a number of open source dependencies. I'm now trying to package it up as a standalone to make it available to end users without them needing to install maven or anything else.

I've successfully used maven-assembly-plugin to build a single jar containing all dependencies but this is not really what I want because when using LGPL libraries you are meant to redistribute the libraries you are using as separate jars.

I want Maven to build a zip containing a jar with my code and a MANIFEST.MF that refers to the other jars I need together with the other jars. This seems like standard practice but I cannot see how to do it.

Here's an extract from my pom

     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.company.widget.Main</mainClass>
                            <packageName>com.company.widget</packageName>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.company.widget.Main</mainClass>
                            <packageName>com.company.widget</packageName>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> 

EDIT:Taken on Kals idea

created a file called descriptor.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

and pom contains:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
        <archive>
            <manifest>
                <mainClass>com.company.widget.cmdline.Main</mainClass>
                <packageName>com.company.widget</packageName>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Now maintains the jar and puts them all in the lib folder, including my own code


Solution

  • Try creating a custom assembly descriptor and add a dependencySet and make sure you specify, unpack as false.

    Use this as assembly descriptor,

    <?xml version="1.0" encoding="UTF-8"?>
    <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>distribution</id>
        <formats>
            <format>zip</format>
        </formats>
        <dependencySets>
            <dependencySet>
                <scope>runtime</scope>
                <outputDirectory>lib</outputDirectory>
                            <useProjectArtifact>false</useProjectArtifact>
                <unpack>false</unpack>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    Store this file to say src/main/assembly/assembly.xml and update your assembly plugin configuration in pom.xml like this.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                        <configuration>
                               <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    Here is assembly descriptor reference if you need anything more

    http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html