Groovy has a unique quality that other dynamic/scripting languages does not – you can write plain Java code in a Groovy class.
This attribute makes it an excellent choice for Java developers trying to pick up this dynamic language – start by writing Java and ease into Groovy’s style as you practice.
To make the transition even smoother, you can mix Java classes with Groovy classes in Maven projects. All you need is to configure your project’s
pom.xml to “recognize” Groovy classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Some tips when doing this:
If you are new to Groovy, it is good to know the differences from Java.
Start with writing unit tests in Groovy where its syntactic sugar will make writing unit tests much more concise and enjoyable. This feature alone can justify enabling Groovy support in a Maven project.
Turn any existing Java class into a Groovy class by just renaming the file to .groovy. Note: This can be done via IntelliJ’s refactor “Rename File…” action.
If you are not familiar with the dynamic nature of Groovy, just code in plain Java. No need to stress yourself out trying to be a pro-Groovy coder quickly.
IntelliJ’s refactoring feature “Convert to Java” can be useful to convert Groovy class into Java class. While this does not always produce 100% usable/runnable Java code, it is still useful for learning purposes.
If you are using Spring Framework, know that all your beans/classes can be created as a Groovy class including things like
@Serviceand other components – Groovy is a first class citizen in Spring based application.
Using this in conjunction with IntelliJ’s excellent Groovy support, it is hard to understand why this not the default configuration for all Maven based Java projects.