Generate a runnable jar and include libraries in it with Maven

13,093

Use the maven-assembly-plugin to generate an executable jar with dependencies

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <groupId>org.apache.maven.plugins</groupId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <id>make-executable-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.coderplus.sample.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

It's not working for you because ,you have added the plugin execution in the pluginManagement section. pluginManagement is supposed to be used for managing the plugin version and configuration.

Minimal version of how your build tag should look like :

<build>
<!-- if you have a multimodule project, I will probably manage this in the
    parent pom -->
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>${assembly.version}</version>
      </plugin>
    </plugins>
  </pluginManagement>

  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <groupId>org.apache.maven.plugins</groupId>
      <executions>
        <execution>
          <id>make-executable-jar-with-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <archive>
              <manifest>
               <addClasspath>true</addClasspath>
               <mainClass>com.coderplus.sample.MainClass</mainClass>
             </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
      </execution>
    </executions>
  </plugin>
  </plugins>
</build>
Share:
13,093
AwaX
Author by

AwaX

Updated on June 05, 2022

Comments

  • AwaX
    AwaX about 2 years

    I'm trying to compile a Java project with Maven and Eclipse but I tried a lot of solutions seen on the web but none of them seem to work. I just want to build the application, create a runnable jar and include the needed libraries. I tried maven-dependency or maven-assembly but I surely miss something because I fail every time.

    Here is my pom.xml, is it ok or does it miss something?

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <!-- Project Description -->
        <groupId>org.awax</groupId>
        <artifactId>toolbox</artifactId>
        <version>0.1.0</version>
        <name>ToolBox</name>
        <packaging>jar</packaging>
        <url>http://maven.apache.org</url>
        <description>Custom library containing useful code</description>
    
        <!-- Project Properties -->
        <properties>
            <jdk.version>1.7</jdk.version>
            <!-- Libraries Version -->
            <log4j.version>1.2.17</log4j.version>
            <jdom.version>2.0.5</jdom.version>
            <miglayout.version>3.7.4</miglayout.version>
            <jfreechart.version>1.0.19</jfreechart.version>
            <bounce.version>0.18</bounce.version>
            <!-- Maven Plugins Version -->
            <eclipse.version>2.9</eclipse.version>
            <compiler.version>3.2</compiler.version>
            <jar.version>2.5</jar.version>
            <assembly.version>2.4.1</assembly.version>
            <dependency.version>2.5.1</dependency.version>
        </properties>
    
        <!-- Project Libraries -->
        <dependencies>
            <!-- LOG4J -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Jdom -->
            <dependency>
                <groupId>org.jdom</groupId>
                <artifactId>jdom2</artifactId>
                <version>${jdom.version}</version>
            </dependency>
            <!-- MigLayout -->
            <dependency>
                <groupId>com.miglayout</groupId>
                <artifactId>miglayout</artifactId>
                <version>${miglayout.version}</version>
            </dependency>
            <!-- JFreeChart -->
            <dependency>
                <groupId>org.jfree</groupId>
                <artifactId>jfreechart</artifactId>
                <version>${jfreechart.version}</version>
            </dependency>
            <!-- Bounce -->
            <dependency>
                <groupId>org.bounce</groupId>
                <artifactId>bounce</artifactId>
                <version>${bounce.version}</version>
            </dependency>
        </dependencies>
    
        <!-- Build Options -->
        <build>
            <pluginManagement>
                <plugins>
                    <!-- Attach source code and Javadoc -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-eclipse-plugin</artifactId>
                        <version>${eclipse.version}</version>
                        <configuration>
                            <downloadSources>true</downloadSources>
                            <downloadJavadocs>false</downloadJavadocs>
                        </configuration>
                    </plugin>
                    <!-- Set a JDK compiler level -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${compiler.version}</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                        </configuration>
                    </plugin>
                    <!-- Copy project dependencies -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>${dependency.version}</version>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <includeScope>provided</includeScope>
                                    <outputDirectory>${project.build.directory}/dependency</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Create the Jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>${jar.version}</version>
                        <configuration>
                            <archive>
                                <manifest>
                                    <!-- Jar file entry point -->
                                    <mainClass>${project.groupId}.${project.artifactId}.tools.WaveformGenerator</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                    <!-- Add dependencies to generated Jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>${assembly.version}</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>