Module dependencies for a specific task?

I have a small Java project using Gradle as build framework (see the current build file here). I’m trying to create a JavaCompile task to compile a single file that represents some kind of optinal “plugin” to the project, and require some additional module dependencies. But I don’t seem to find how to specify a transitive dependency that is only downloaded whenever a specific task is executed (and is ignored for a “standard” build).

Here is what I tried so far:

task compilePlugin(type: JavaCompile) {
    dependencies {
        compile group:'de.dfki.mary', name:'marytts', version:'5.1' 
    }
    source = fileTree(dir: 'src', include: 'opendial/plugins/MaryTTS.java')
    classpath=sourceSets.main.output
    destinationDir = sourceSets.main.output.classesDir
}

The code above does not seem to download the dependencies (let alone add it to the classpath to compile the file). Any ideas how to solve this problem?

You’re almost there.
You need to declare a custom configuration, using non-standard name, then you need to declare the dependencies of this configuration, and add this configuration in the classpath of your JavaCompile task, ie

configurations{
myConf
}
dependencies {
myConf group:‘de.dfki.mary’, name:‘marytts’, version:‘5.1’
}

task compilePlugin(type: JavaCompile) {

classpath=myConf

}

Please note that this will only use the dependencies from myConf
You can also do sth like ‘classpath =myConf +sourceSets.main.compileClasspath’ if needed

1 Like

Many thanks, it works perfectly now!
(but I had to set classpath = configurations.myConf instead of classpath=myConf in the task block)

Ooops you’re right, forgot the ‘configurations’

This should probably be modeled as a separate source set.

Yes, @mark_vieira is right.
It’s cleaner to use a source set, as the associated tasks and configurations will be done by Gradle for you

apply plugin: 'java'

sourceSets {
  maryTTS{
    java {
      srcDir 'src'
      include 'opendial/plugins/MaryTTS.java'
    }
  }
}

Now you have already what you need for, i.e. a task named compileMaryTTSJava
and a configuration, called maryTTSCompile

You just have to install your dependency using

dependencies {
  maryTTSCompile 'de.dfki.mary:marytts:5.1'
}

and of course call the compileMaryTTS task

1 Like

I’ve done similar things in the past, and it works well.

One things still bugs me, however, which is that sometimes, a custom dependency for a custom configuration is hosted in a custom repository that should not be used for anything else. My solution is to isolate this custom repository/configuration/dependency in its own subproject. But this seems like a hack, and IMHO a more elegant solution would be to be able to declare a repository only for a specific configuration.

Would this feature make sense to anyone else?