How To Skip Jar When Empty (Manifest Only)

(Björn Harmen Gerth) #1


I am repeating a question of Jared D. Cotrell from April 2011 to the mailing list: > How do I do the equivalent of Ant’s whenmanifestonly=“skip” using the > Gradle java plugin’s jar task? > >

<jar jarfile=“lib/${module}.jar” >

basedir="${build}" >

whenmanifestonly=“skip” />

See nabble

I stumbled over the same issue. My intermediate solution is

jar {
  onlyIf{ !compileJava.source.empty }

I feel this is akward because it firstly duplicates the indirect task dependency compileJava (-> classes) -> jar and secondly has to be repated for each task of type Jar. Actually for this second point I have no clue how to write it as

tasks.withType(Jar) {
  onlyIf{ /* what to write here? */ }

Environment: java and war plugin.

Any ideas how to improve this situation?

(René Groeschke) #2

Hello bjoern,

gradle currently has no build-in support for skipping Jar creation when manifestonly=true. In your case, I would go with a slidely modified version of your onlyIf closure:

jar {
 onlyIf { !sourceSets.main.allSource.files.isEmpty() }

This checks that your main source set does not contain any files. If you want to use tasks.withType(Jar) with this onlyIf closure you have to be careful, as it also skips the creation of jars, that might have nothing todo with your main source set.

regards, René