How to include compile time required Jars for a Task (of type Jar)

Hi, i am a new user trying out Gradle. I am writing a simple gradle build script where i build a set of Java sources. I am using gradle Java plugin that brings in a set of default tasks (like build, clean e.t.c)
I have defined dependencies like below which i wish to be included during compile time.

  • include path to local libs /
    dependencies {
    compile fileTree(dir: ‘lib’, include: [’
    .*’])
    }

This works for my default build task " ./gradlew build" where in it picks up all the dependent Jars from the lib folder during compile time.

However when i write a new Task ( i am writing as i want more than one Jars for my sources and each of the source folder/module has compile time dependancy on a set of Jars placed in lib folder)

For e.g; my new task is:

task myJarTask (type:Jar) {
println 'compiling myTask’
from sourceSets.myJarTask.output
classifier = "MyJar"
archiveName = “myJarTask.jar”
}

Question: when i do ./gradlew myJarTask it does not pick up compile time dependencies from the lib folder specified above. Is there a way i can specify where to look for compile time jars in Jar Task ?

Regards,
Ram

The ‘compile’ configuration is used only by the ‘main’ source set, that is, code living in src/main/java. Looking at your ‘myJarTask’ it looks like you’ve also declare a ‘myJarTask’ source set somewhere, with code living in src/myJarTask/java (according to default convention). Each source set has it’s own completely independent compile classpath. You have a couple options:

  • Add the ‘lib’ folder to the ‘myJarTaskCompile’ configuration:

dependencies {
    myJarTaskCompile fileTree(dir: 'lib', include: '*.jar')`
}
  • Have the ‘myJarTask’ source set inherit dependencies from the ‘main’ source set:

configurations {
    myJarTaskCompile.extendsFrom compile
    myJarTaskRuntime.extendsFrom runtime
}

Thanks Mark. The above suggestion worked for me. You are right i have a sourceSet declared with path to my source dir where sources of myJarTask resides.

Regards,
Ram