Warm tip: This article is reproduced from stackoverflow.com, please click

problems when trying to compile groovy class inside a java class

发布于 2020-03-27 10:19:21

We currently need to migrate an application from Grails 2.x to Grails 3.x

We do not know much about this application but in the past, in v2, it used Groovy classes inside Java classes.

After upgrading to v3, this doesn't seem to work anymore. Having Groovy code inside a Java class will return this

...: error: cannot find symbol
      line of code with the error
  symbol:   variable MyGroovyClass
  location: class MyJavaClass


public class MyJavaClass {
  public void x() { MyGroovyClass.test(); }


class MyGroovyClass {
  static void test() { ... }
Jeff Scott Brown 2019-07-03 21:47

they are placed under src/main/java and src/main/groovy

The simple thing to do, and the thing that most Grails apps do, is put all of the Java and Groovy source code under src/main/groovy/. That allows the Groovy joint compiler to compile all of it together as a single unit so you can have bidirectional dependencies between the Groovy and Java classes. It is possible to configure support for separate directories but it involves some complexity that doesn't really buy you anything, which is why the convention is to use src/main/groovy/.