Unable to create a fat test jar with gradle


(obesga tirant) #1

Hi everyone

I’m new to the world of gradle, I was previously on the maven land.
I found Gradle to be powerful and complex, but I want to learn

I want to make a fat jar with tests classes and dependencies, a fat test jar
So far, I have done a fatJar with compile classes and dependiencies and a test jar with only test classes.

I’ve tried many configurations and options, but I didn’t hit the nail

My configuration gradle file is

//THIS WORKS
task fatJar(type: Jar) {
    dependsOn install
    manifest {
        attributes 'Manifest-Version': '1.0', 'Implementation-Title': 'xyz', 'Implementation-Version': version, 'Main-Class': 'ClientMain', 'SplashScreen-Image': 'splashScreen.png'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

// THIS ALSO WORKS
task testJar(type: Jar) {
    manifest {
        attributes 'Manifest-Version': '1.0', 'Implementation-Title': 'xyz', 'Implementation-Version': version, 'Main-Class': 'TestMain', 'SplashScreen-Image': 'splashScreenTest.png'
    }
    //classifier = 'tests'
    baseName = project.name + '-test'
    from sourceSets.test.output+sourceSets.test.allSource
}

// THIS NOT WORK
// Generates a Jar but without test classes and resources
task fatTestJar(type: Jar) {
    dependsOn testJar, fatJar
    manifest {
        attributes 'Manifest-Version': '1.0', 'Implementation-Title': 'xyz', 'Implementation-Version': version, 'Main-Class': 'TestServer', 'SplashScreen-Image': 'splashScreenTest.png'
    }
    baseName = project.name + '-test-all'
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
        configurations.testCompile.collect { it.isDirectory() ? it : zipTree(it) }
        sourceSets.test.output
    }
    with jar
}

Anyone has a clue ?

:disappointed_relieved:

Thanks


(Mark Vieira) #2

The problem is the following. The result of a Groovy closure (neglecting an explicit return statement) is the last line of the closure.

from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    configurations.testCompile.collect { it.isDirectory() ? it : zipTree(it) }
    sourceSets.test.output
}

So the code above will result in including only the compile test classes. You’ll want to do this.

from { configurations.testRuntime.collect { it.isDirectory() ? it : zipTree(it) }
from sourceSets.test.output

Couple of other things you might notice:

  1. No need to include both compile and testCompile since testCompile extends compile. That is, all compile dependencies are also automatically on the test compilation classpath.
  2. Really, you should be using testRuntime as you’ll need any and all runtime dependencies to actually execute the tests.

(obesga tirant) #3

Works like a charm !
Thanks