Including dependencies in a jar with Maven

0 votes

Way to force maven to include all the dependencies in a single jar file?

we have a project the builds into a single jar file. we want the classes from dependencies to be copied into the jar as well.

Update: We know that we can't just include a jar file in a jar file. We are searching for a way to unpack the jars that are specified as dependencies and package the class files into my jar.

Jul 20, 2018 in Java by Akrati
• 3,170 points
5,047 views

1 answer to this question.

0 votes

You can do this using the maven-assembly-plugin with the "jar-with-dependencies" descriptor. Here's the relevant chunk from one of our pom.xml's that does this:

  <build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>
answered Jul 20, 2018 by samarth295
• 2,200 points

here what is "jar-with-dependencies" where do I mention it in pom.xml

Hii,

jar-with-dependencies means to put all the dependencies either A) inside the authors jar via repackaging, or B) make an executable jar that has the others in a classpath of MANIFEST.MF

You can ran into this with the alexa-skills-kit-sdk-for-java which worked well until the instructions to create the WAR barfed in Maven. In 'Developing Your First Skill' it says enter the line command 'mvn org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly -DdescriptorId=jar-with-dependencies package' which does not work.

 I put the above plug-in in the 'highest POM' (closest to the root file) and ran the line command 'mvn clean compile assembly:single' from that POM (directory) and it worked perfectly. 

For example: if your main class path is src/main/java/com/example/ui/Main.java, you must write like that to pom.xml: <mainClass>com.example.ui.Main</mainClass>.

Hope it helps!!
Thank you!!

Related Questions In Java

0 votes
1 answer

How can I create an executable JAR with dependencies using Maven?

Hello @kartik, You can use the dependency-plugin to ...READ MORE

answered May 28 in Java by Niroj
• 59,780 points
636 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 6, 2018 by Omkar 30,266 views
+2 votes
10 answers

How to import a jar file in Eclipse?

To import jar file in your Eclipse ...READ MORE

answered May 15, 2018 in Java by sharth
• 3,370 points
165,894 views
0 votes
1 answer

How to run a JAR file in Java?

The command given below will help you ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,630 points
3,948 views
0 votes
1 answer

How can I create an executable JAR with dependencies using Maven?

<build> <plugins> <plugin> ...READ MORE

answered Apr 26, 2018 in Java by sophia
• 1,400 points
409 views
0 votes
2 answers

How can we add the local JAR files to the Maven Project in Java?

Firstly I would like to give credit ...READ MORE

answered Nov 5, 2018 in Java by Sushmita
• 6,890 points
1,972 views
+1 vote
1 answer

How to tell the Maven to use the latest version of dependency in Java?

If you always want to use the ...READ MORE

answered Jun 26, 2018 in Java by Sushmita
• 6,890 points
7,201 views
0 votes
1 answer

Add jars to maven 2 build class path without installing them?

wn voteA quick&dirty batch solution (based on Alex's answer): libs.bat @ECHO ...READ MORE

answered Aug 29, 2018 in Java by samarth295
• 2,200 points
197 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,200 points
342 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,110 points
1,860 views