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 21, 2018 in Java by Akrati
• 3,190 points
15,698 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 21, 2018 by samarth295
• 2,220 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, 2020 in Java by Niroj
• 82,840 points
6,048 views
+5 votes
4 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 7, 2018 by Omkar 81,193 views
+5 votes
11 answers

How to import a jar file in Eclipse?

Click on File > Import. The Import ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
359,639 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,640 points
7,412 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
1,263 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,920 points
11,768 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,920 points
15,372 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 30, 2018 in Java by samarth295
• 2,220 points
1,226 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 28, 2018 in Java by samarth295
• 2,220 points
2,478 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,140 points
3,695 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP