Intellij Java 2016 & Maven : how to embed dependencies in JAR?

20,506

Solution 1

I finally managed to generate this JAR with Intellij Java, here is how I do:

  • add the dependencies in the pom.xml file
  • go to File -> Project Structure -> Artifacts -> New -> JAR -> From module with dependencies
  • choose the Main class and click OK
  • in your project, in src/main, create the "resources" folder
  • move the "META-INF" (with MANIFEST.MF in it) folder in this "resources" folder
  • go to Build -> build artifacts to build the JAR

EDIT

A better (and easier way) to do it is adding the following lines in the pom.xml file :

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

then use the "clean" and "package" maven commands.

The last 3 steps above (about MANIFEST.MF) still seem to be mandatory.

Solution 2

Okay, so you basically want to create a "fat jar" (sometimes called assembly), that contains all its own dependencies (usually, the dependencies are external).

You need to use a Maven plugin for that. Below is a sample assembly plugin configuration jar-with-dependencies:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        ...
</project>

then, simply run

mvn package
Share:
20,506

Related videos on Youtube

matteoh
Author by

matteoh

Updated on September 05, 2020

Comments

  • matteoh
    matteoh over 3 years

    I'm using Intellij Java 2016.2.2 and Maven to create a very simple Java console application.

    I want to add an external library, so I add my dependency in Maven like this:

    <dependency>
        <groupId>jline</groupId>
        <artifactId>jline</artifactId>
        <version>2.12</version>
    </dependency>
    

    It works fine when I run it in the IDE, but not in an external console (I have the following error: java.lang.NoClassDefFoundError).

    I checked and for some reason, the external JAR is not added in the JAR I just generated. I also tried many things in "File -> Project Structure", but still not working...

    I just want to build my JAR with my dependencies in it, so I can simply run my application in a console using:

    java -jar myproject.jar
    

    How can I do that? Thanks for your help!

    • khmarbaise
      khmarbaise over 7 years
      Use the maven-assembly-plugin using the jar-with-dependencies descriptor....
    • matteoh
      matteoh over 7 years
      Thanks for your help, but still not working. So I wonder: I build my JAR using "Build -> Build artifacts...". Is it the right way to do it?
  • matteoh
    matteoh over 7 years
    Thanks, it's seems interesting! But now, another simple problem: "No main manifest attribute". It seems that it doesn't manage to find my MANIFEST.MF file... Any idea?
  • YMomb
    YMomb over 7 years
  • Yushan ZHANG
    Yushan ZHANG over 5 years
    This does NOT create a jar with dependencies.
  • isaolmez
    isaolmez almost 4 years
    Although the descriptor is selected, it doesn't specify the main class. Also, the execution part is missing. We must bind the assembly:single goal to a build phase or run the goal explicitly. javabyexamples.com/…