# Making an executable jar with dependencies

**URL:** <https://discuss.gradle.org/t/making-an-executable-jar-with-dependencies/4710>\
**Category:** Old Forum Archive\
**Created:** [May 18, 2013, 5:54pm UTC](https://discuss.gradle.org/t/making-an-executable-jar-with-dependencies/4710 "2013-05-18T17:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirk](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@Kirk](https://discuss.gradle.org/u/Kirk)\
**Post date:** [May 18, 2013, 5:54pm UTC](https://discuss.gradle.org/t/making-an-executable-jar-with-dependencies/4710/1 "2013-05-18T17:54:00Z")

</div>

I’ve found some discussion of this on the site, but nothing I’ve seen answers my question.

I want to make a directory, lets say,

```gradle
MyApplication

```

Inside this directory I want to put a jar file (the result of my build), and a lib directory with all my project’s runtime dependencies. So it ends up like this:

```gradle
MyApplication
        MyApplication.jar
        lib
            dependency1.jar
            dependency2.jar

```

The jar file needs to have a manifest that contains a Class-Path property that includes the jars in the lib directory.

The plugins available for this task have so-far shown themselves to not quite be right. For example, I do not want a shell script, and I do not want the project’s position in the overall project hierarchy to be baked into the resulting structure.

So, the copy tasks are easy enough to make and I can construct the above directory structure by hand.

But I’ve been unable to get the Class-Path string into the jar file. Here’s what I’ve tried:

```gradle
project.ext.cpString = {
         // Build the string that will go in the manifest's
         //
Class-Path attribute
        String result
        configurations.runtime.resolve().each { f ->
            result = result + " lib/${f.name}"
        }
        result
    }

```

Then

```gradle
jar {
        manifest {
            attributes('Main-Class': 'mypackage.MyMainClass')
            attributes('Class-Path': project.ext.cpString )
        }
    }

```

And many variations. I’ve tried making the string generator a function, I’ve tried defining the property and then setting that property in a task wich jar depends on. Lots of things. The Class-Path attribute in the jar always ends up blank.

I’m sure I’m just missing some detail about the gradle build phases or something. Any help?

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [May 18, 2013, 8:27pm UTC](https://discuss.gradle.org/t/making-an-executable-jar-with-dependencies/4710/2 "2013-05-18T20:27:00Z")

</div>

Your code is never calling the ‘cpString’ closure. You could also turn it into a method. You’d also have to defer the work until you can be sure that the ‘jar’ task is really going to be executed, for example with ‘jar.doFirst { … }’. I recommend to check out the [gradle-one-jar](https://github.com/rholder/gradle-one-jar) plugin.

---

<div class="post-metadata">

**Author:** ![Kirk](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@Kirk](https://discuss.gradle.org/u/Kirk)\
**Post date:** [May 23, 2013, 4:05pm UTC](https://discuss.gradle.org/t/making-an-executable-jar-with-dependencies/4710/3 "2013-05-23T16:05:00Z")

</div>

Thanks Peter,

Reordering things seems to have done the trick. The closure does appear to get called when the property is evaluated in the jar task.
