Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an executable jar without dependencies using Maven?

I want to package it not in a single executable jar for distribution. I need an executable to be something like main.jar and all dependencies to be in libs/*.jar

How can I make maven executable jar without preincluded into it dependencies libraries?

In How can I create an executable JAR with dependencies using Maven? there is a note by answered Dec 1 '10 at 10:46 André Aronsen, but that one simply doesn't work (failed s.a.descriptorRef is not set).

like image 656
Bogdan Avatar asked Nov 30 '11 12:11

Bogdan


People also ask

How do I manually create an executable JAR file?

In Eclipse you can do it simply as follows : Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.

Does JAR file contain dependencies?

Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.

Which maven command is used to create a jar?

4. mvn package. This command builds the maven project and packages them into a JAR, WAR, etc.


1 Answers

I prefer this a bit modified solution. Create Your executable jar with classpath set and copy all dependencies to given directory.

You don't need any additional files.

Dependencies are being copied during install phase.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <!-- optional -->
                        <!-- exclude copying test and provided dependencies -->
                        <includeScope>runtime</includeScope>
                        <excludeScope>provided</excludeScope>
                        <!-- optional -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I updated the answer to copy only runtime dependencies as described here - copying the right dependencies. Test and provided dependencies are excluded.

It turns out that if you want to exclude dependencies from both the test and provided scopes, you need to exclude the provided scope and include the runtime scope.

like image 72
Rafał Spryszyński Avatar answered Sep 21 '22 23:09

Rafał Spryszyński