First gradle build (Java)

About this article

This is a very simple tutorial for building a simple Java app with Gradle. The purpose is to get a feel for Gradle. It also explains how to install and use Gradle Wrapper.

I won't explain the build script in Groovy, this grammar in this sense, or anything like that [^ 1]. But I think you can get a feel for it. [^ 1]: Not detailed enough to explain in the first place

Assumed reader

Prerequisites

It is a condition that JDK6 or higher is downloaded. You don't need an IDE, just a text editor.

Project creation

First, create a Java project to build with Gradle. Keep the project as simple as possible to focus on Gradle's description.

Directory structure

Create the following directory structure under your favorite directory.

└── src
    └── main
        └── java
            └── hello

Source code

Create two classes,'HelloWorld.java' and'Greeter.java', under the src / main / java / hello directory.

src/main/java/hello/HelloWorld.java


package hello;

public class HelloWorld {
  public static void main(String[] args) {
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

src/main/java/hello/Greeter.java


package hello;

public class Greeter {
  public String sayHello() {
    return "Hello world!";
  }
}

Gradle installation

Let's install Gradle.

You can download the binaries from http://www.gradle.org/downloads. DL-extract gradle-version-bin.zip and put the path to the bin folder.

You can also use the following installer.

Run the following command to see if it was installed

gradle

If the installation is successful, you will see a welcome message.

>gradle
Starting a Gradle Daemon (subsequent builds will be faster)
:help

Welcome to Gradle 3.2.1.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL

Total time: 8.667 secs

This completes the Gradle installation.

gradle tasks Execute the following command under the project directory.

gradle tasks

The gradle tasks command displays the tasks you can perform with gradle. At this point you should see the following basic tasks:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'hello'.
components - Displays the components produced by root project 'hello'. [incubating]
dependencies - Displays all dependencies declared in root project 'hello'.
dependencyInsight - Displays the insight into a specific dependency in root project 'hello'.
dependentComponents - Displays the dependent components of components in root project 'hello'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'hello'. [incubating]
projects - Displays the sub-projects of root project 'hello'.
properties - Displays the properties of root project 'hello'.
tasks - Displays the tasks runnable from root project 'hello'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL

Total time: 1.244 secs

Well, it doesn't make much sense in the initial state, but if you want to know what kind of task can be executed, it is a good idea to keep in mind that this command is used.

Java code build

Create a build.gradle file directly under the project folder.

build.gradle


apply plugin: 'java'

This line indicates that you are building this project using the Java plugin.

Run gradle tasks again. You'll see new tasks added, such as building the project, creating the JavaDoc, and running the tests.

Let's build it right away.

gradle build

gradle build will be used frequently from now on. As the name implies, it is a command to execute a build. After a few seconds, you will see "BUILD SUCCESS FUL" indicating that the build is complete.

Take a look at the build folder to see the build results. There are several folders.

classes Contains the .class file when the Java code was compiled. This time HelloWorld.class'and'Greeter.class' should have been created

dependency-cache Contains the .jar of the dependent module. It is empty at this time.

libs The created library (usually a jar or war) is stored.

Project execution

Now that the build is complete, let's run it. It is possible to execute it using the java command, but since it is a big deal, let's execute it from gradle. Change build.gradle as follows.

build.gradle


apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

The application plugin allows you to start an application with the gradle run command.

>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Hello world!

BUILD SUCCESSFUL

Total time: 1.786 secs

Dependency resolution

The samples so far do not depend on any external library, so there is not much taste in using gradle.

So, let's change it so that not only "Hello World!" But also the current date and time are displayed. You can use the Java date-time API of the Java standard library, but I'll use Joda Time libraries for illustration.

First, modify HelloWorld.java as follows:

HelloWorld.java


package hello;

import org.joda.time.LocalTime;

public class HelloWorld {
  public static void main(String[] args) {
    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);

    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

Hello World uses Joda Time's Local Time to get and display the current time.

Now run gradle build. It fails because Joda Time cannot be resolved.

Now let's have gradle resolve the dependency. Please change build.gradle.

build.gradle


apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

repositories {
    mavenCentral()
}

jar {
    baseName = 'gs-gradle'
    version =  '0.1.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile "joda-time:joda-time:2.2"
    testCompile "junit:junit:4.12"
}

The repositories block specifies that the Maven central repository will be used for the repository.

The jar block specifies the name of the jar. In this case it would be gs-gradle-0.1.0.jar.

The dependencies block declares dependencies on Joda Time. Specifically, the joda-time library in the joda-time group, version 2.2.

compile indicates that the dependency is valid only at compile time. It's the compile scope in Maven. Other dependency types

Now, run gradle build again. gradle resolves the Joda Time dependency from the Mave central repository and the build succeeds.

The project should run fine.

>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
The current local time is: 01:56:47.665
Hello world!

BUILD SUCCESSFUL

Total time: 1.308 secs

Gradle Wapper When it comes to using this kind of tool in a project consisting of multiple people, it is troublesome to prepare an installation procedure manual for other members and to use different versions.

That's where Gradle Wapper comes in. By installing Gradle Wapper in your project, anyone can build a project with the same version of gradle regardless of the environment such as Windows, Mac, Linux.

Installing Gradle Wapper is easy, just execute the following command directly under the project.

$ gradle wrapper --gradle-version 2.13

When the task is completed, several folders and directories will be created.

└── <project folder>
    └── gradlew
    └── gradlew.bat
    └── gradle
        └── wrapper
            └── gradle-wrapper.jar
            └── gradle-wrapper.properties

This completes the installation of Gradle Wrapper. It's easy to use, just execute "gradlew (.bat)" directly under the project instead of the gradle command.

./gradlew build

Of course, you can run other commands as well.

./gradlew run

Just commit the file created with gradle init to git or svn (and just hit this command and write a line in the readme) and no one will have to do it via gradlew You can use gradle with. It's well thought out.

At the end

With this, I think you can get a feel for Gradle. It is not possible to build a full-scale project with the contents of this article alone. However, the hurdle to read other Gradle-related articles should have been lowered. Get the information you need and get the most out of Gradle.

I hope this article has given you a chance to start using gradle.

Recommended Posts

First gradle build (Java)
Build a Java project with Gradle
CICS-Run Java applications-(3) Build management with Gradle
I first touched Java ②
[Gradle] Build operations often performed in Java projects
I first touched Java ③
I first touched Java ④
Build Java x Spring x VSCode x Gradle on Docker (1)
Using Gradle with VS Code, build Java → run
I first touched Java
Build Java with Wercker
Build VS Code + WSL + Java + Gradle environment from scratch
Java multi-project creation with Gradle
Spring + Gradle + Java Quick Start
[Gradle] About Java plug-in tasks
First Java development in Eclipse
Using Docker from Java Gradle
JAVA (First step: Git Bush edition)
Do HelloWorld in Java / IntelliJ / Gradle
Gradle
Java build with mac vs code
Build Java development environment (for Mac)
Java
Java
Hello world in Java and Gradle
[Gradle] Build a Java project with a configuration different from the convention
Spring Boot gradle build with Docker
GraalVM for Java Performance (Windows Developer Build)
When Gradle build stops when importing on Mac
[Java] Create an executable module with Gradle
Introduction to java for the first time # 2
First steps for deep learning in Java
Step by Step: Java + gradle + mybatis + postgresql + log4j
Build Spring Boot + Docker image in Gradle
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
CICS-Run Java applications-(2) Build management with Maven
Learning for the first time java [Introduction]
Build OpenCV with Java Wrapper on Ubuntu 18.04
Basic Java grammar you should know first
Java automated test implementation with JUnit 5 + Gradle
Build Web Application Server (Java) on VPS