wood burning stoves 2.0*
The moose likes Ant, Maven and Other Build Tools and the fly likes Jar file creation using Maven 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 "Jar file creation using Maven" Watch "Jar file creation using Maven" New topic
Author

Jar file creation using Maven

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

Guys,

I have a simple class file with a main method. I would like to now create a jar file using Maven which also has all the jar's that it uses. I have even set up the Maven project and when I actually built it, it creates a jar file but I do not find any referenced libraries as part of that jar. How to acheive this?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Even if the jar contained the other jars it's still not going to work like you think it will; you'd either need to include all the classes from the dependencies in the jar (i.e., unzip then add to the distribution jar's class directory hierarchy) or create a zip file distro which can be unzipped and run with the appropriate classpath set.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

I thought of doing it with Assemblies in Maven. Any idea from that front?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why do you want a jar that contains other jars?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

David Newton wrote:Why do you want a jar that contains other jars?


Maybe to make an executable jar that depends on library jars without having to explode them and merge them into the main jar classes?

There is, BTW a Maven plug-in to do exactly that. It's called something original like "Maven Executable JAR plugin" and its targets include both a jar that can be executed via the "java -jar" command and a more traditional JAR that can be included in a classpath.


Customer surveys are for companies who didn't pay proper attention to begin with.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Does referencing bundled jars in the manifest work? I guess I didn't know you could do that without using shade--I thought referenced libs had to be external to the jar being executed.

In any case:

http://maven.apache.org/plugins/maven-jar-plugin/

Specifically:

http://maven.apache.org/shared/maven-archiver/examples/classpath.html

For shading:

http://maven.apache.org/plugins/maven-shade-plugin/index.html

(Very handy sometimes.)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

I don't think this is shade:

http://maven.apache.org/shared/maven-archiver/examples/classpath.html

And, if I understand the question correctly, yes, dependencies get included in the executable version of the jar, according to the usual scope rules.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

Thanks for all the responses!
 
GeeCON Prague 2014
 
subject: Jar file creation using Maven