2

I have a free project that is all based on Free and Open source programs:

  • some of them have pom and public accessible jars
  • some of them have pom but no public accessible jars
  • some of them have no pom

I want to distribute the project as a unique jar since all licences are compatible I legally can, if it's possible to generate such a jar with maven, I don't know how. Can you help?

If it's not possible what is the best way to distribute such project?


My pom is managed by netbeans, here is the missing configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <!-- I added the following -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>net.hypermove.graphitidb.GraphitiDB</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
amirouche
  • 7,682
  • 6
  • 40
  • 94
  • The above link was likely found with a Google for "maven executable jar" - it is the first hit in my case. Something to remember for the next time you have a "how do I do this" type of tool question: Google is your friend! – Gimby Nov 11 '13 at 16:35

1 Answers1

6

There are many options for you

  1. Maven Assembly Plugin with jar-with-dependencies descriptor

  2. Maven One Jar Plugin

  3. Distribution Jar - Zip Archive with your jar, all dependencies in directory lib and configured manifest for easy execution. Sample configuration below.

  4. Executable Jar with Maven Shade Plugin

  5. Runnable Jar with dependencies mapped to local maven repository (possible, but not portable) Altering The Classpath: Using a Maven Repository-Style Classpath

1. Jar Assembled With Dependencies

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>make-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>eu.codearte.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

2. Maven One Jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>eu.codearte.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <execution>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
...
<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>

3. Distribution Jar configuration

Maven Dependency Plugin

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>true</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <stripVersion>false</stripVersion>
                <stripClassifier>false</stripClassifier>
            </configuration>
        </execution>
    </executions>
</plugin>

Maven Assembly Plugin

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/bundle.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

Maven Jar Plugin

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
       <execution>
            <id>exe-jar</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>exe</classifier>
                <archive>
                    <manifest>
                        <mainClass>eu.codearte.MainClass</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

Assembly Descriptor

<?xml version='1.0' encoding='UTF-8'?>
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bundle</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>target/lib</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>target/project-exe.jar</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • 1
    After using the shade plugin for years, I've been very happy with the assembly plugin, which builds much faster, and is easier to verify in a production environment since all dependencies are viewable in the package instead of being aggregated into one huge jar. – lreeder Nov 11 '13 at 16:56