Stefan Kendall
Stefan Kendall

Reputation: 67822

Building a runnable jar with Maven 2

I'm relatively new to the Maven mantra, but I'm trying to build a command-line runnable jar with Maven. I've setup my dependencies, but when I run mvn install and attempt to run the jar, two things happen. First, no main class is found, which is correctable. When I've corrected this, I get errors on run stating that classes cannot be found.

Maven is not packaging my dependency libraries inside of the jar, so I am unable to run the jar as a stand-alone application. How do I correct this?

Upvotes: 86

Views: 101151

Answers (7)

ElSaeed
ElSaeed

Reputation: 1

To package your dependencies inside the JAR so that it can be run as a standalone application, you need to configure the Maven Assembly Plugin or the Maven Shade Plugin. Here's how you can do it using the Maven Shade Plugin:

  1. First, add the Maven Shade Plugin to your pom.xml file inside the section:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version> <!-- Or use the latest version -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.mapReduce.WordCountRunner</mainClass> <!-- Update with your main class -->
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. Replace org.mapReduce.WordCountRunner with the fully qualified name of your main class.
  2. After adding the plugin configuration, run mvn clean package to create the shaded JAR.

Upvotes: 0

Aaron Kurtzhals
Aaron Kurtzhals

Reputation: 2036

I have a spring boot application, and the jar created by maven package can be run without setting up additional plugins.

Inside my pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.12</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

https://spring.io/projects/spring-boot

Upvotes: 0

likeGreen
likeGreen

Reputation: 1079

Just add the below code in pom.xml and Run as: maven:install . The jar will be created in target folder of eclipse which can be used as "java -jar Hello.jar" . but make sure that name of main class is given com.abc.HelloWorld.java

<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-shade-plugin</artifactid>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalname>HelloW</finalname>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestentries>
<main-class>com.abc.HelloWorld.java</main-class>
<build-number>1</build-number>
</manifestentries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

Upvotes: 2

user2409824
user2409824

Reputation: 81

This is what I would do for small projects. Most of the time you don't want one huge jar.

to build: mvn clean dependency:copy-dependencies package

to execute (in target dir): java -cp myjar.jar:./dependency/* com.something.MyClass

Upvotes: 8

Joachim Sauer
Joachim Sauer

Reputation: 308031

Maven is not packaging your dependencies inside your jar file, because you don't usually do this with Java programs.

Instead you deliver the dependencies together with your jar file and mention them in the Class-Path header of the Manifest.

To go this route, you'll need to enable the addClasspath property (documented here) for the maven-jar-plugin.

If you really want to include all your dependencies in your jar file, then you can use the Maven Assembly plugin to create a jar-with-dependencies.

Upvotes: 26

formixian
formixian

Reputation: 1559

I Agree with Joachim Sauer,

Instead of using jar-with-dependency you should configure the jar plugin like that in your pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <index>true</index>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>[mainClassFullName]</mainClass>
            </manifest>
            <manifestEntries>
                <mode>development</mode>
                <url>${project.url}</url>
                <key>value</key>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

And use this assembly configuration to add the jar dependencies to you assembly:

<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>zip-with-jars</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySets>
    <dependencySet>
    <outputDirectory>/</outputDirectory>
    <useProjectArtifact>true</useProjectArtifact>
    <unpack>false</unpack>
    <scope>runtime</scope>
    </dependencySet>
</dependencySets>
  </dependencySets>
</assembly>

Upvotes: 6

Pascal Thivent
Pascal Thivent

Reputation: 570345

The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Then, to generate the assembly, just run:

mvn assembly:assembly

If you want to generate the assembly as part of your build, simply bind the assembly:single mojo to the package phase:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

And simply run:

mvn package

Upvotes: 114

Related Questions