Getting to know Gradle — Using

First Java Project with Gradle

Rodrigo Mendes
4 min readAug 20, 2018

We can create a Java project, applying the Java plugin. We can do this adding the following line to our file build.gradle:

This is it. We now have a Java project.

The Java plugin adds new conventions (for example, the layout of the standard project), new tasks and new properties for our construction.

Let´s move ahead and take a quick look at the layout for the standard project. The standard layout of a Java project is the following:

  • The directory src/main/java contains the source code of our project.
  • The directory src/main/resources contains the resources (such as property files) of our project.
  • The directory src/test/java contains the class tests.
  • The directory src/test/ resources contain the test resource.

All of the files in our exit compilation are created under the directory of construction. This folder contains the following subdirectories that are relevant until this moment (there are other subdirectories, but we will talk about them more ahead):

  • The directory classes contain the compiles .class files
  • The directory libs contain the JAR or WAR files created by construction

Let´s move ahead and add a simple main class to our project.

Adding a main class to our construction

Let´s add a simple main class, that prints the words “Olá Mundo” FOR THE System.out

The source code of the class HelloWorld is the following:

However, we still need to compile and package our project. Let´s move ahead and take a look at the tasks of a Java project.

The Java plugin adds many tasks to our construction, but the tasks which are relevant for this post are:

  • The task assemble complies the source code of our application and packages in a jar file. This task does not execute unitary tests.
  • The compilation tasks executes a compilation of the total project.
  • The clean task excludes the compilation folder
  • The task compileJava compiles the source-code of our application.

We can also obtain the complete list of executable tasks and its description, executing the following command in the command prompt or with the help of the plugin there is a file (Gradle Tasks) that shows where all of the available tasks for this project are.

When clicking twice in the task, it is executed and its execution can be followd by two distinct folders, the console in Eclispe and the Gradle Executions.

In Gradle Executions, there is the time spent in each activity, this because we spoke about this previously in this article and it is possible to make tasks parallel for big projects, so that the builds of these projects do not take very long, thus gaining agility in the Build tasks which we know will occur many times a day.

To create a JAR, you need to use JAR activity, but to execute the class with a main method it is necessary to create a MANIFEST file. For this tasks there is a configuration we will execute now.

When executing this task, the directory META-INF will be created with the MANIFEST.MF file.

--

--