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
MyJavaClass
public class MyJavaClass {
public void x() { MyGroovyClass.test(); }
}
MyGroovyClass
class MyGroovyClass {
static void test() { ... }
}
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
/.
This solution worked and is pretty quick. Not really the right one but for the moment I can work it out using this.
"Not really the right one but for the moment I can work it out using this." - I am glad you got it working. The reason that we have
grails create-app
createsrc/main/groovy/
and notsrc/main/java/
is the preferred convention is to do that.The issue is with the default build config,
src/main/groovy/
andsrc/main/java/
are compiles separately which means one has to be compiled first which means that one cannot have references to the other one, because it isn't compiled yet. Putting it all insrc/main/groovy/
allows it all to be compiled together. Groovy provides a joint compiler which allows Groovy and Java sources to be compiled as 1 unit, allowing bidirectional dependencies.An alternative is to add
src/main/java/
to thesourceSets
forcompileGroovy
so those sources will be compiled at the same time as the sources undersrc/main/groovy/
. If you do that, it can be made to work, but is more build config since you are deviating from the defaults, and is a non-standard thing.