In a previous blogpost I wrote about using Makefiles for build automation. Makefiles might be good enough for C and C++ projects, but no Java developer is using them (there are reasons). Instead, there are better build tools for Java that are far better than
make, such as Ant, Maven and Gradle. In this blogpost, I’m going to show you the basics of Gradle usage.
Why using Gradle
Gradle is a real polyglot: Java is not the only language it speaks. You can use it to build C/C++/Objective-C projects, or for other JVM languages such as Scala or Groovy (Gradle is build with Groovy), and even for web-oriented build automation!
When using Gradle, you can choose what to compile. This means that you aren’t obligated to build all your project, but even small fractions of it. Also, since Gradle can cache things such as test results and artifacts, it saves time by not doing work that is already done.
While Ant and Maven write their logic in XML [configuration] files, Gradle uses real source code to express its logic. This code-over-configuration philosophy makes it easier to write concise and powerful build files. And yet, it is extendable by a great amount of plugins.
Build with Gradle
I used Gradle to build a small, “Hello World” application. It is made of two source files (check the 01 branch):
Gradle requires a project strucure like:
src/main/java directory is the one where you put the source files. So there I put the
demo package with the code.
build.gradle file, you can simply put:
This one says to Gradle to build the application and to use the
App.java file as the main file to run the executable. When you run
gradle build (make sure to install Gradle first), you will see a long list of reports, finishing with “BUILD SUCCESSFUL”. It will create a new
build directory, with the compiled and packaged files in there. You can run the executable by executing
If your app is using third-party libraries (a.k.a dependencies), you can get them using Gradle (check the 02 branch). I am appending these lines in the
You can now use the Joda Time library in your app. By adding
repositories, we say to Gradle that it can obtain necessary files from the Maven repository, and then we declare which one we need to obtain at
Create the wrapper
when you are working on a team, maybe not all of the members will have Gradle installed, and not all of them must have it installed in order to use it. Instead, one of the members can create a Gradle wrapper which can then be used as a legit Gradle build tool. Append this task in the
When you run
gradle wrapper, some files will be generated to allow others use gradle (check the 03 branch), such as gradlew.sh for Linux/OSX users and gradlew.bat for Windows users. Make sure to include the added files in Version Control!
So that was all. I hope you enjoyed trying Gradle and probably are thinking of using it as the build tool of choice. You can check out the repository here