Using Gradle to Create Java Projects under Windows

Keywords: Gradle Java Junit Maven

Create a project

First, you need to create a Java project with the following directory structure: / src/main/java/hello. You can create several Java classes under the Hello directory. Here, HelloWorld.java and Greeter.java are created for convenience. The code is as follows:

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!";
  }
}

Install Grandle

click http://www.gradle.org/downloads Download gradle and add bin directory to environment variables.
Test whether gradle was installed successfully
Enter gradle in the cmd window. If the following appears, the installation is successful.

> Task :help

Welcome to Gradle 4.0.

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 in 1s
1 actionable task: 1 executed

So far, the installation of gradle has been completed.

Building Java code

Create a basic build.gradle file in the project directory with the following line of code
apply plugin: 'java'
Then enter the gradle build command to complete the code construction. A BUILD SUCCESSFUL appears later to indicate that the code was successfully constructed.

Declarative dependencies

The changes to HelloWorld.java are as follows

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());
  }
}

Here HelloWorld uses Joda Time's LocalTime class to get the current printing time. If you use gradle build to build a project now, you will fail because Joda Time is not declared as a compilation dependency in the build. Therefore, third-party dependency libraries need to be added.

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

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

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

Now, if you run a gradle build, Gradle should parse Joda Time dependencies from the Maven Central repository, and the build will succeed.

Use Gradle Wrapper to build projects

gradle wrapper --gradle-version 4.0
gradlew build

To make this code work, we can use gradle's application plug-in. Add it to your build.gradle file.

apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

Then you can allow the program

gradlew run

> Task :run
The current local time is:14:12:25.209
Hello world!


BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date

This completes Gradle's Java project.

Posted by jax_15 on Fri, 21 Jun 2019 16:06:58 -0700