Tomas Longo
Tomas Longo

Reputation: 105

Gradle Groovy Plugin Seems to Remove Compiled Java Classes

Here is quite a strange effect I get when trying to compile my project that uses groovy as scripting language. The setup is pretty simple:

I have a java application that can be configured by a groovy script. The parsing of the config file is all handled by groovy code and generate several classes that contain the extracted information from the script and which than are made available to the java app.

The configuration classes all implement interfaces, to decouple the groovy aspect so that the java app is not aware that it actually talks to groovy objects.

So, with the interfaces the only dependency i have here is from groovy to java. This should be the normal case, since the groovy plugin executes compileJava before compileGroovy by default.

This worked till today!!!

A few hours ago things started getting strange. Trying to test my app with gradle test resulted in errors telling me that the groovy classes do no see the java interfaces. I tried than to compile the java and groovy seperately with compileJava followed by compileGroovy and noticed that the latter simply deletes all class files generated by the java task. I also found a strange output when running with the --info option:

Output file /home/tomas/projects/unnecessary-wizard/build/classes/main has changed.
Output file /home/tomas/projects/unnecessary-wizard/build/classes/main/de/tlongo/unneccesarywizard/java/core/Wizard.class has changed.
Output file /home/tomas/projects/unnecessary-wizard/build/classes/main/de/tlongo/unneccesarywizard/java/core/ConstructorInjector.class has changed.

Why does the groovy task change java classes at all?

As I said, I tried to reproduce the errors with a simple showcase where a groovy class also implements a java interface, w/o success.

Here is my build.script which is pretty 9-to-5, imo:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'

group = 'de.tlongo'
version = '0.3-SNAPSHOT'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.5'
    compile 'commons-configuration:commons-configuration:1.7'
    compile 'ch.qos.logback:logback-classic:1.1.1'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'org.reflections:reflections:0.9.9-RC1'

    testCompile 'org.hamcrest:hamcrest-all:1.3'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'org.mockito:mockito-all:1.9.5'
}

Any idea what is going wrong? Or do I not understand the concept of how gradle compiles this kind of projects.

Upvotes: 0

Views: 524

Answers (2)

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

This should only ever happen after a Groovy class has been ported to Java. It's a known limitation that results from the fact that both GroovyCompile and JavaCompile delete the class files they produced on the previous run in order to prevent stale class files.

Upvotes: 0

cjstehno
cjstehno

Reputation: 13984

When compiling both Java and Groovy source in Gradle, you should generally put all your source in the 'src/main/groovy' directory to allow for cross-compiling. They can be separate (java in java, groovy in groovy); however, if there are cross-language dependencies you can run into compilation issues - this is what is seems you are running into.

Also, as a side note, you don't need to apply the java plugin when you apply the groovy plugin - the groovy plugin depends on the java plugin so it will already be available.

Upvotes: 1

Related Questions