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).
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
Please note that this will only use the dependencies from myConf
You can also do sth like ‘classpath =myConf +sourceSets.main.compileClasspath’ if needed
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.