Writing a custom JavaCompile task

(Ham Vocke) #1

I’m using Gradle in a Java project. In my build.gradle I have defined following source sets:

sourceSets {

main {

java {

srcDirs = [‘src’, ‘src.mdd’]


resources {

srcDir ‘resources’



test {

java {

srcDir ‘test’




src is for my regular source files, src.mdd contains all source files that were generated from an UML modelling tool.

I am also using a code generator in my project which takes several of my Java classes as input and then generates new source files (.java) to a new directory (build/gen).

In order to build my project correctly, I have to run following sequence:

  1. compile sources in src and src.mdd 2. run the code generator and pass some of the previously compiled classes as parameter. The code generator will save all generated .java files in build/gen 3. compile sources in build/gen 4. jar everything

Steps 1 & 2 are working just fine. The sources are compiled via Gradle’s compileJava task and the code generator is run via a custom task that I wrote.

My approach for step 3 is to use following task that should be executed after the generator task has completed:

task compileGen(type: JavaCompile) {

source genDir

classpath sourceSets.main.compileClasspath

destinationDir ‘$buildDir/genClasses’


This approach, however, doesn’t work at all. Everytime I try to run this task, Gradle gives me following error message:

Could not find method classpath() for arguments [configuration ‘:compile’] on root project ‘ProjectName’

The console output points to the line starting with classpath in my compileGen task.

Could anyone tell me what I’m doing wrong and whether I’m using the right approach?

(Gary Hale) #2

I don’t think classpath is a method on the JavaCompile task, it’s only a property so you would have to do:

classpath = sourceSets.main.compileClasspath

(Ham Vocke) #3

Thank you very much, this was the solution to my problem. I wasn’t aware that my notation was a shorthand for a method call but rather thought it’d assign the value to a property.