As you can see, the classes sub-folder contains two compiled .class files we previously created. The distributions sub-folder contains an archived version of the application jar package. And libs keeps the jar file of our application.

Usually, in reports, there are files that are generated when running JUnit tests.

Now everything is ready to run the Java project by typing gradle run.The result of executing the application on exit:

4. Add Java Dependencies and Run a Simple Test

First, in our configuration file, we need to set a remote repository from where we download dependency jars. Most often, these repositories are either mavenCentral() or jcenter(). Let's choose the second one:

repositories {
jcenter()
}

With our repositories created, we can then specify which dependencies to download. In this example, we are adding Apache Commons and JUnit library. To implement, add testImplementation and testRuntime parts in the dependencies configuration.

Similar to before, let's run a gradle clean test from the command line and the test should pass without issue.

5. Java Project Initialization Using Gradle

In this section, we'll explain the steps for creating and building a Java application that we have gone through so far. The difference is that this time, we work with the help of the Gradle Build Init Plugin.

Create a new project folder and name it gradle-java-example. Then, switch to that empty project folder and run the init script:

> gradle init

Gradle will ask us with few questions and offer options for creating a project. The first question is what type of project we want to generate:

6. Conclusion

In this article, we've presented two ways to create and build a Java application using Gradle. The fact is, we did the manual work and it took time to start compiling and building applications from the command line. In this case, we should pay attention to the import of some required packages and classes if the application uses multiple libraries.

On the other side, the Gradle init script has features that generate a light skeleton of our project, as well as some of the configuration files associated with Gradle.