Extend source dir if a dependency is added

I have a Gradle Java project where in the dependencies section I have

dependencies {
    ...
    compile fileTree(dir: ‘libs’, include: ‘*.jar’)
}

I would like to detect when a specific third party jar is added (easy) by matching the jar name (e.g. LicensedExtension.jar), and when this appens I would like to include some specific classes to handle the external library, something like

if(LicensedJarAdded) {
    sourceSets.main.java.srcDirs += ["extension"]
}

Is it doable?