How do I conditionally compile individual classes?


I’m just learning gradle. I thought I’d try to convert an existing project from ant to gradle as an experiment. It’s mostly going well (the project isn’t terribly complicated).

One thing I haven’t been able to figure out is conditional compilation. There are a couple of classes that should only be compiled if a particular commercial library is available.

Basically, if “commercial.jar” is in the projects “lib” directory, then compile “src/main/java/my/extensionClass”. If commercial.jar isn’t in the lib directory, just skip that class.

Pointers, please?

Darn. I was hoping this was going to be an easy question.

Try something like:

if (...) { "**/my/" }

Sorry. Groovy novice too. More web surfing was unhelpful.

What’s a reasonable test for the equivalent of the ant construct:

<available classname=“” property=“companyAvailable”>

<classpath refid=“build.classpath”/>


I did eventually figure this out.

‘’’’ task checkThirdPartyLib() {

doFirst {

try {


} catch (Exception e) {

println “Third party library classes unavailable, skipping XYZ” ‘com/project/extension/’


} } compileJava.dependsOn checkThirdPartyLib ‘’’’

The check* task excludes the classes that depend on the third party library if some exemplar class from that library can’t be found on the classpath.