[GRADLE-1361] Fat jar cookbook example leads to error Created: 01/Feb/11  Updated: 04/Jan/13  Resolved: 21/Jun/11

Status: Resolved
Project: Gradle
Affects Version/s: 0.9.2
Fix Version/s: 1.0-milestone-4

Type: Bug
Reporter: Ben McCann Assignee: Szczepan Faber
Resolution: Fixed Votes: 0

Attachments: Zip Archive example.zip    

 Description   

I have a multiproject build and I put a fat jar task in one of the subprojects. I created the fat jar task similar to the one described in the cookbook - http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

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. It seems that this is a bug or the documentation should be updated.



 Comments   
Comment by Ben McCann [ 02/Feb/11 ]

I created a small example gradle project similar to mine which reproduces this error.

Comment by Ben McCann [ 03/Feb/11 ]

It's much more complicated, but this seems to work:

task deployJar(type: Jar, dependsOn: jar) {
  baseName = project.name + '-deploy'
  deps = configurations.runtime + configurations.archives.allArtifactFiles
  depClasses = { deps.collect { it.isDirectory() ? it : zipTree(it) } }
  from(depClasses) { 
    exclude 'META-INF/MANIFEST.MF'
  }
  manifest {
    attributes 'Main-Class': 'com.benmccann.test.frontend.server.WebServer'
  }
}
Comment by Russ Egan [ 10/Jun/11 ]

Hit this myself. Unfortunately, seems to run much much slower than the ant equivalent:

    <target name="fatJar" depends="jar">
        <jar destfile="${libs.dir}/${ant.project.name}-deploy-${version}.jar">
            <zipgroupfileset dir="lib" includes="*.jar"/>
            <fileset dir="${classes.main.dir}"/>
        </jar>
    </target>
Comment by Szczepan Faber [ 21/Jun/11 ]

I just fixed the cookbook example.

//incorrect:
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }

//correct:
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

Since we're passing closure to the from() method it means that the it will be evaluated at execution time. At execution time, the configuration is already resolved meaning that all dependencies are downloaded, etc.

Generated at Wed Jun 30 11:53:37 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.