0

I am trying to make my application work in a stand alone jar. The problem is, a jar is generated for my program, and a bunch of other jars are generated for the libraries. Is there any way to get these jars to get inside one? I am using Gradle if that helps.

The IntelliJ IDEA artifact config:

enter image description here

The output directory:

enter image description here

What I expected (and want) to happen:

enter image description here

Chris Smith
  • 2,928
  • 4
  • 27
  • 59
  • Added a picture for what I would like to be the result. – Chris Smith Aug 29 '15 at 22:15
  • http://stackoverflow.com/questions/4871656/using-gradle-to-build-a-jar-with-dependencies refers to this as an uberjar or a fatjar. Using maven this is called a shaded jar. – user2290362 Aug 29 '15 at 22:17
  • Instead of merging all the jars into a fat jar, just put them all in the same folder and enhance the manifest to specify the classpath, so they're all loaded when you run with `java -jar`. – Andreas Aug 29 '15 at 22:22
  • 1
    @Andreas I want to release my software as a stand along file. That way it simplifies the download process. It does work with having all the jars spread out, but that isn't what I want to do. – Chris Smith Aug 29 '15 at 22:26
  • It appears that this cannot be done normally: http://stackoverflow.com/questions/12357136/reference-jars-inside-a-jar – Chris Smith Aug 29 '15 at 22:55

1 Answers1

2

You need a fat-jar (jar file with all it's dependencies inside). It's not a big problem for Gradle, you just need to make one additional task of type jar, which will collect all the dependencies and zip it alltogether

There are many examples, how you can do it, here is one of them. Take a closer look at task fatJar:

task fatJar(type: Jar) {        
  baseName = project.name + '-all'
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
}
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • I have never used Gradle before for building, it just does whatever IntelliJ IDEA does by default. – Chris Smith Aug 30 '15 at 13:05
  • I'm not sure that it's a default Idea's behaviour, because it's not standart Gradle's behaviour. And the only Idea does - is call the gradle script. And furthermore, it will put all the dependencies inside a single jar, but they'll be unziped – Stanislav Aug 30 '15 at 13:16
  • You can configure the way stuff is built inside IDEA, I don't see anything about the specific build stuff that I have configured inside build.gradle – Chris Smith Aug 31 '15 at 11:32
  • Ok, now I see, you don't use your gradle scripts to pack the artfacts, good. But anyway, not sure that it's possible to do like you want it, because you need a specific classloader for this, take a look here http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar – Stanislav Sep 01 '15 at 11:13
  • So, if you need just a single jar, why don't you try to do it with unziped classes added to the root of your jar? I'm pretty sure, it's possible to do not only with maven or gradle, but with Idea too. – Stanislav Sep 01 '15 at 11:18
  • Yeah, I ended up just using the example you gave above, took some figuring out how to do it though. – Chris Smith Sep 01 '15 at 14:09