"Unable to resolve class" after moving a class from src/main/groovy to src/main/java


(Mark Petrovic) #1

I moved a couple files from src/main/groovy/ to src/main/java/, followed by changing the associated file suffixes from .groovy to .java. My project would not build until I removed the .gradle/ cache in the project current working directory. The compiler complained that it could not find the files/classes that had moved.

Is this to be expected?


(Peter Niederwieser) #2

This shouldn’t happen. Can you provide the exact error message, stack trace, etc.?


(Mark Petrovic) #3

Here is the error message from my console:

/Users/petrovic/Projects/acme/qa/lib/atf-core/geb-browser-parent/geb-dpsbrowser/src/main/groovy/com/acme/qa/dpsbrowser/DPSBrowserImpl.groovy: 162: unable to resolve class ServiceType
  @ line 162, column 73.
   imit(String partnerShortName, ServiceTyp
                                               ^

I will try to reproduce in a test project. If I cannot, this is all I can offer at the moment.


(Mark Petrovic) #4

Ok, I am able to reproduce this in a test project.

Here is my console output:

mb:gradle-rename-enum> find .
.
./build.gradle
./src
./src/main
./src/main/groovy
./src/main/groovy/somepackage
./src/main/groovy/somepackage/AScript.groovy
./src/main/groovy/somepackage/SomeEnum.groovy
./src/main/java
./src/main/java/somepackage
  mb:gradle-rename-enum> gradle -q build
  mb:gradle-rename-enum> mv -v src/main/groovy/somepackage/SomeEnum.groovy src/main/java/somepackage/SomeEnum.java
src/main/groovy/somepackage/SomeEnum.groovy -> src/main/java/somepackage/SomeEnum.java
  mb:gradle-rename-enum> gradle -q build
startup failed:
/Users/petrovic/Projects/gradle-rename-enum/src/main/groovy/somepackage/AScript.groovy: 6: unable to resolve class SomeEnum
  @ line 6, column 18.
           SomeEnum someEnum = SomeEnum.A
                    ^
  1 error
    FAILURE: Build failed with an exception.
  * What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  mb:gradle-rename-enum> rm -rf .gradle/
  mb:gradle-rename-enum> gradle -q build
  # no problems

Here is my build script:

apply plugin: 'groovy'
  repositories {
    mavenCentral()
}
  dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.0.5'
}

And here are the source files:

An enum that I move from src/main/groovy to src/main/java:

package somepackage;
  public enum SomeEnum {
    A, B
}

The client code:

package somepackage
  class AScript {
      def foo() {
        SomeEnum someEnum = SomeEnum.A
    }
}

My Gradle version:

gradle -v
  ------------------------------------------------------------
Gradle 1.2
------------------------------------------------------------
  Gradle build time: Wednesday, September 12, 2012 10:46:02 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.6.0_37 (Apple Inc. 20.12-b01-434)
OS: Mac OS X 10.6.8 x86_64

(Mark Petrovic) #5

I re-ran the scenario with Gradle 1.3. It behaves the same as 1.2.


(Mark Petrovic) #6

The project on github:

https://github.com/ae6rt/gradle-rename-enum.git


(Peter Niederwieser) #7

Thanks for the sample project. I can reproduce the problem. It’s caused by Gradle’s compilation tasks deleting all class files they produced last time before they compile again. This prevents stale class files issues but causes some new issues like the one you discovered. I’ve created GRADLE-2574 for this.


(Peter Niederwieser) #8

Note that you can avoid this problem by using Java Groovy joint compilation - either put Java files into src/main/groovy, or configure src/main/java to be a Groovy source dir (and remove it as a Java source dir). Then ‘compileGroovy’ will compile both your Java and Groovy classes, and you can have arbitrary dependencies between them.