Undescriptive failure


(aispina) #1

I spent a lot of time tracking down this bug. It might be worth making a change to Gradle to help poor saps like myself =). I wrote a simple sample project to illustrate the problem. It only has two files. One is a ‘hello world’ java file. Here is the code:

class Main {
   public static void main(String[] args){
  System.out.println("hello world");
 }
  }

The second file is the build.gradle. It is meant to be as simple as possible. As presented, it will cause a failure that looks like this:

C:\code\ra\examples\fatjar-example>gradle packageAsJar Picked up _JAVA_OPTIONS: -Duser.home=“C:\Users\aspina” :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar :packageAsJar

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:packageAsJar’. > Could not expand ZIP ‘C:\code\ra\examples\fatjar-example\build\libs\fatjar-example.jar’.

Here is the build file (notice the commented out line that corrects the failure):

apply plugin: "java"
  repositories {
 mavenCentral();
}
  dependencies {
 compile "junit:junit:4.10"
}
  task packageAsJar(type: Jar, dependsOn: jar) {
        // This line fixes the failure (it's a name collision if you haven't guessed).
 //archiveName = "blah.jar"
 from {
   configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
  zipTree(project.jar.archivePath)
 }
   manifest { attributes 'Main-Class': 'Main' }
}

Finally as an aside, adding this as an example to the cookbook might help address a common usecase.