Recently, I’ve been using Gradle as a build tool and I have to say it is pretty amazing! It does so much for you with very little configuration. Some of things it does you don’t realize you need until Gradle actually does it for you. It is convenience stacked upon convenience. I’m going to talk about a few of these things… and hopefully you will gain as much of an appreciation for Gradle as I have. Before I get started, go ahead and and install Gradle on your computer. Once you install Gradle on your computer, you can start building a Gradle file in your project. Normally I name the file build.gradle. Now for those that are used to Maven,the look of this file is very different from a Maven build file. Here is a sample:
There are several things defined here. We have a few settings for Spring Boot since this application is going to run stand alone. We also define Maven repository locations (as you can see, Gradle uses the Maven repository to pull dependencies and compile your code). You can also define a local Maven repo by using the mavenLocal() in the build file. We also use a custom Maven repository in this build file. Finally, we also use mavenCentral() which defines the central Maven repository. The order of these definitions is important because it is the order that dependencies are retrieved. In this example, the build process will look in your local Maven repository first. Then it will try your custom Maven repository and finally try the general Maven repository. After a few tries, I found this order works best for my development. You will also notice several plugins that can be installed with Gradle. These are really helpful in terms of development. For example, the Eclipse plugin when added to the build file allows you to run simple Gradle commands to build the .project file and associated Eclipse file for your project. Instead of selecting the correct project and importing your code (and hoping it will run in eclipse), all you have to do is run the command ‘gradle eclipse’ in your project’s root directory. This will build all the Eclipse files and the .classpath file that will allow you to build and run your project in Eclipse! It is that simple. If for some reason you need to rebuild the Eclipse files in the project, you can run the command ‘gradle cleanEclipse’ to delete those Eclipse settings. You can then run ‘gradle eclipse’ to build the Eclipse settings again. I can tell you from experience that this has saved me lots of time when the .classpath file in Eclipse gets corrupted or the project updates are not showing up in Eclipse. (FYI for all you IntelliJ developers, the same commands exist for IntelliJ IDEA. The command ‘gradle idea’ will build the project settings for IntelliJ IDEA. From the previous code sample, you can see that the plugin is named “idea”.) Now since this application is standalone, you want a way to deploy this application right? No problem! Here a sample of a build.gradle file that will create a jar file for you that is ready to deploy:
This snippet will create an executable jar file and push it to a repository so you can deploy it wherever you like! Finally, if you want to run the jar file on your local machine:
In this example, running the command ‘gradle bootRun’ will start up your application using your main class and you are ready to go. There are several other plugins that you can add to the Gradle build file that will enhance your development. For example, you can add the SonarQube plugin to track the quality of the code in your application. If you have unit tests, you can have your unit tests run after your application has completed building. You can also use a test coverage library such as Jacoco and add that plugin to your build. Gradle builds are very powerful and very easy to customize to what kind of development you are doing. The syntax, in my opinion, is much simpler than Ant or Maven and has many more options that just compiling and deploying an application. I hope this is a blog post encourages you to explore Gradle for your development team. Happy developing!