Search code examples
gradleexecutable-jaruberjarfatjar

Using Gradle to build a JAR with dependencies


I have a multiproject build and I put a task to build a fat JAR in one of the subprojects. I created the task similar to the one described in this cookbook.

jar {
  from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  manifest { attributes 'Main-Class': 'com.benmccann.gradle.test.WebServer' }
}

Running it results in the following error:

Cause: You can't change a configuration which is not in unresolved state!

I'm not sure what this error means. I also reported this on the Gradle JIRA in case it is a bug.


Solution

  • I posted a solution in JIRA against Gradle:

    // Include dependent libraries in archive.
    mainClassName = "com.company.application.Main"
    
    jar {
      manifest { 
        attributes "Main-Class": "$mainClassName"
      }  
    
      from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
      }
    }
    

    Note that mainClassName must appear BEFORE jar {.