Bogdan
Bogdan

Reputation: 713

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).

Upvotes: 19

Views: 28935

Answers (2)

Rafał Spryszyński
Rafał Spryszyński

Reputation: 684

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.

Upvotes: 14

Raghuram
Raghuram

Reputation: 52645

You can achieve this to a certain extent.

Firstly, you would create an executable jar by configuring maven jar plugin suitably.

You would then use maven assembly plugin to create a jar-with-dependencies, excluding your project jar. To do this, you would create a descriptor file, say src/main/assembly/descriptor.xml, like this.

<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>false</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Use it in your project like this.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
        [...]
      </plugin>
    </plugins>
  </build>
</project>

You will end up getting two jars - one the executable jar created by your project and the other the jar-with-dependencies created by the assembly plugin.

Upvotes: 11

Related Questions