13

I am interested in building a single jar containing all the module dependencies and external jars in a single executable jar file which I will be able to run with java -jar myApp.jar.

I have module A which is dependent on module B. Currently I'm using gradle, and my build.gradlescript looks like this:

    apply plugin: 'fatjar'
    description = "A_Project"
    dependencies {
      compile project(':B_Project')
      compile "com.someExternalDependency::3.0"
    }

When I build it through gradle command: clean build fatjar a fat jar 'A.jar' is created as expected. But running it with as I written above results in: no main manifest attribute, in A.jar How can I modify my build.gradle file and specify the main class, or the manifest?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
Ella Nikolic
  • 301
  • 1
  • 2
  • 9
  • No idea how to help You. It seems that command `clean build fatjar` has different outputs. Is that true? – Opal May 19 '14 at 19:05
  • See "Customization of MANIFEST.MF" in the [Gradle User Guide](http://gradle.org/docs/current/userguide/userguide_single.html). – Peter Niederwieser May 20 '14 at 03:45

2 Answers2

17

I have figured it out myself: I've used uberjar Gradle task. now my build.gradle file looks like this:

apply plugin: 'java'
apply plugin: 'application'

mainClassName  = 'com.organization.project.package.mainClassName'

version = '1.0'

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from {configurations.compile.collect {zipTree(it)}} {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
}

manifest {
    attributes 'Main-Class': 'com.organization.project.package.mainClassName'
    }
}


dependencies {
compile project(':B_Project')
compile "com.someExternalDependency::3.0"
}

and now I i use it with the command:

clean build uberjar

and it builds one nice runnable jar :)

Ella Nikolic
  • 301
  • 1
  • 2
  • 9
  • i needed to add `with jar` below manifest to get the files in `java/main/resources` in the uberjar. Also inside collect is should be { it.isDirectory() ? it : zipTree(it) }... – gladiator Dec 30 '14 at 12:19
5

To get it working using fatjar, I added a manifest section in fatJar task:

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
        attributes 'Main-Class': 'com.organization.project.package.mainClassName'
    }
}
Damien
  • 767
  • 9
  • 13