7

Possible Duplicate:
Including dependencies in a jar with Maven

I am beginner in maven and would like to build a jar containing dependency jars in it using maven. But, I could not do so. Kindly help me about it.

Community
  • 1
  • 1
sridhar
  • 1,117
  • 5
  • 31
  • 59
  • 1
    It's not a duplicate. The other question is about putting all dependencies in a single jar, by any means. This question is specifically about making a jar of jars. In this case, the jars must not be unpacked into a flat structure, but retained as distinct jar files (much like the war file format). This is clear from sridhar's comment to the accepted answer from JHS -- it doesn't do what he wants. – drrob Apr 24 '18 at 11:01

3 Answers3

3

If you're trying to create a single jar that contains your application and it's required libraries, there are two ways (that I know of) to do that. The first is One-Jar, which uses a special classloader to allow the nesting of jars. The second is UberJar, (or Shade), which explodes the included libraries and puts all the classes in the top-level jar.

I should also mention that UberJar and Shade are plugins for Maven1 and Maven2 respectively. As mentioned below, you can also use the assembly plugin (which in reality is much more powerful, but much harder to properly configure).

Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80
  • Thanks for you advise but I would like to make a jar say jarFinal.jar contains jar1.jar, jar2.jar, jar3.jar etc. The suggestions given makes one jar extracting all class files from the dependency jars. I would like to make a jar of jars without unpacking class files. – sridhar Jun 18 '12 at 10:40
  • 1
    @sridhar It sounds like One-Jar would satisfy your requirements. – Max Nanasy Aug 18 '12 at 07:43
  • 1
    There is a Maven plugin for One-Jar: http://onejar-maven-plugin.googlecode.com/svn/mavensite/usage.html – Thilo Jan 29 '13 at 03:46
0

Use shade plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
    <version>1.7</version>
<executions>
    <execution>
    <phase>package</phase>
    <goals>
        <goal>shade</goal>
    </goals>
    </execution>
</executions>
</plugin>
plucury
  • 1,120
  • 1
  • 8
  • 16
  • 1
    Thanks for you advise but I would like to make a jar say jarFinal.jar contains jar1.jar, jar2.jar, jar3.jar etc. The suggestions given makes one jar extracting all class files from the dependency jars. I would like to make a jar of jars without unpacking class files. – sridhar Jun 18 '12 at 10:39
-1

You would to include the following in your pom.xml file.

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

JHS
  • 7,761
  • 2
  • 29
  • 53
  • 5
    Thanks for you advise but I would like to make a jar say jarFinal.jar contains jar1.jar, jar2.jar, jar3.jar etc. The suggestions given makes one jar extracting all class files from the dependency jars. I would like to make a jar of jars without unpacking class files. – sridhar Jun 18 '12 at 10:38
  • @sridhar Could you make jar in jar? Does this accepted answer make "jar in jar"? – vikramsjn Mar 29 '18 at 15:11
  • This doesn't answer the question as it unpacks the jars. The closest answer is Hip Hip Array's suggestion to use One-Jar as it does actually maintain the nesting of jars. – drrob Apr 24 '18 at 11:05