Mixed Groovy and Java Class in Gradle Build

Have seen this question often before. If we have a groovy script that builds an object that a java class needs, why is it not seen during compile time ? Looked for ages for the answer to this. Unless both/all java and groovy sources live together in sourceSets.src.main.groovy directory, the groovy compile cannot ‘see’ the class from the groovy source to pass tho that java compiler when requested. Using

apply plugin: ‘groovy’

takes precedence over the java plugin. Any  xxx.java source files are passed to the java compiler by the groovy mechanism. The groovy classes are invisible to the java compiler. Assume this example

src/main/groovy/Person.groovy

src/main/java/PersonManager.java

the Person class created in the groovy step cannot be seen by the java compiler. Drat ! But there is a work-around until gradle change this behaviour. Try this in build.gradle :

sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDir 'src/main/java'

hope it works for you :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s