• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to create Jar file with all the dependencies

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there,

I need to create a jar file with all the dependencies defined at the pom. I understand I have to use the maven assembly plugin.

Here is what I am doing...



Dependencies are generated in the distribution zip, however, I need them to be inside of a jar file, let's say something called dependencies.jar

What am I missing?


Thanks in advance for your help!
Eric
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the best approach for such purposes is to use the maven-shade-plugin which exactly creates such things.

simple setup extracted from the docs:



The maven-assembly-plugin is also able to create such a jar which contains all dependencies.

You can use a configuration like this:


But the support for a jar-with-dependencies is basic as described in the docs.

So i would recommend to use the maven-shade-plugin cause it will give a large number of options to control the resulting jar file etc.

Kind regards
Karl-Heinz Marbaise
 
Eric Lopez-Fernandez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karl,

Thank you very much for your reply. In my case I really need to use an assembly descriptor because I am adding other files to the final distrubution. From my limited knowledge I see that you can use either

<descriptorRefs>
<descriptorRef>myassembly.xml</descriptorRef>
</descriptorRefs>

or

<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

Is that correct? If that is the case, then I really need to find a way to generate the jar from the assembly descriptor. So far, I am getting all the dependencies sent to the final distribution, however, they are not in a jar file as I need them.

Does it make sense?

Thank you in advance,
Eric
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the assembly descriptor you posted, line 4. You are telling it to build a ZIP file. If you want it to build a JAR file, use <format>jar</format> instead.
 
Eric Lopez-Fernandez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply Peter!

Here is the scenario I'm dealing with.

I have to generate zip file that contains

One jar file with the application
One jar file with all the dependencies
Some help documents that will be placed under the HelpDocs directory
Some bat/sh files that will be placed under the OtherDocs directory

My idea is to generate the zip with the assembly, as well as the dependencies jar. The app's jar is created in the POM in the following way..

<packaging>jar</packaging>

Do you know of a more efficient way to accomplish that?

Thanks again!
Eric

 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to invoke the assembly plugin twice. For the first invocation, build the JAR containing all of the dependencies. For the second invocation, create the zip file with the contents you listed. You will need two assembly descriptors, one for each invocation.

Why do you want your app to be in one JAR and the dependencies in a separate JAR? Usually I do one of two things:
a) place everything into a single JAR file (and I make it runnable by specifying the main class)
b) Place the app JAR and the JARs for all the dependencies into a lib directory. Then I have my scripts build the classpath and run the app.

I usually prefer option (b), it makes it easier for my customers to see which components I am making use of, and the individual JARs retain their version and build information.
 
Eric Lopez-Fernandez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

Could you please advice on how to do that. I am currently using maven-assembly-plugin. I run it twice, however, the jar with al the dependencies is a big file and by the time when it is finally created the zip file was created already and does not contain the jar. Do you know of any way to stop maven assembly from starting to create the zip until the jar is finished. Here is the portion of my POM that is related to that:



And this is my dep.xml file



Thank you very much for your help!
Eric
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't declare the plugin twice. Instead, declare it only once, but have two <execution> sections. In other words, move lines 45..51 to after line 25 and then delete lines 28..53
 
reply
    Bookmark Topic Watch Topic
  • New Topic