aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes How to create Jar file with all the dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to create Jar file with all the dependencies" Watch "How to create Jar file with all the dependencies" New topic
Author

How to create Jar file with all the dependencies

Eric Lopez-Fernandez
Greenhorn

Joined: Oct 13, 2011
Posts: 17
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
Karl Heinz Marbaise
Greenhorn

Joined: Aug 12, 2007
Posts: 19
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


Software Configuration Management (Subversion, CVS, SVK etc.) http://www.skmwiki.de/
Subversion Repository Search Engine http://www.supose.org/
Eric Lopez-Fernandez
Greenhorn

Joined: Oct 13, 2011
Posts: 17
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
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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.


JBoss In Action
Eric Lopez-Fernandez
Greenhorn

Joined: Oct 13, 2011
Posts: 17
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
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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

Joined: Oct 13, 2011
Posts: 17
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
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create Jar file with all the dependencies