How to compile Groovy before Java in a Gradle Build

I had an interesting problem come up at work last week and I would like to share it with you today. A coworker was having an issue with a Gradle build. Whenever he tried to compile the project it threw an error saying it couldn’t find the Groovy class he was trying to call from Java.

compile groovy before java

In this article, we are going to walk through setting up a project that will illustrate this problem. We will then examine what normal behavior is in a Gradle build and how we can make a change to fix this problem that we were facing. 

Create a new Gradle Project

The first thing we are going to do is create a new project using Gradle. This is going to give us a standard Gradle & Java project layout. 

New Gradle Project

Gradle Wrapper

The next thing we need to do is add support to our IDE for Groovy.

Adding Groovy Support

With that in place, we are going to add a new groovy folder to our main directory. 

Create a new Groovy class

Now that a lot of our setup is done we get to finally write some code. I created a new package “com.foo” under my Groovy src folder and created a simple class called Developer.

Next, we will modify our Gradle build. We are going to add the Groovy dependency and a couple plugins. The groovy plugin gives us the ability to compile the Groovy code. 

We should now be able to run the compileGroovy task from Gradle. 

Compile Groovy before Java

Creating the Java Application

It’s time to focus on the Java side of this project and create our main application. I have created a very simple application that simply creates a new developer. 

If we try and run a Gradle build now it will fail and this is the problem we were facing at work. 

Compiling Groovy before Java

The reason this fails is that the default action for our Gradle build is to compile the Java class before it compiles the Groovy class. When this takes our Java class will have no idea what our Developer class is because it hasn’t been compiled yet. No worries though, we can change this behavior with a little bit of code in our Gradle build. 

The Groovy plugin actually extends the Java plugin so that we can compile Groovy along with Java. What we are doing here is just telling Groovy to compile everything for us. When you understand what is going on behind the scenes this makes a lot of sense. 

Now if you were trying to call a Java class from Groovy the normal behavior would work out just fine. 

Conclusion

This is a pretty common mistake when you try and mix both Java and Groovy. I loving mixing Java & Groovy in projects whenever I get the chance so hopefully, this taught you something and you will do the same in the near future.

Question: Are you using Java & Groovy in your projects today? 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 17+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 22,000 students.

When I am not writing software I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.