How to build Java with Gradle

I made a project with Gradle because I wanted to build Java code with Editor. I used the Gradle wrapper, and the wrapper can fix the gradle version and can be used on Linux or Windows. You can build without modifying your code. For this reason, Android Project also uses wrappers.

My development environment is as follows.

  • Ubuntu 17.04
  • Java 1.8
  • Gradle 4.5

Create Project

First you need to create a source podler.

$ mkdir -p src/main/java/hello

And I created a source code that simply prints Hello World.

package hello;

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

The Dir structure is as shown below.

$ tree
.
└── src
    └── main
        └── java
            └── hello
                └── HelloWorld.java

Now we need to generate the Gradle wrapper file. If you enter ./gradlew in the top-level dir of the project, wrapper files are created.

  • gradlew is a script used in Unix-like Linux and Mac.
  • gradlew.bat is a script used in Windows series.
  • gradle/wrapper/ contains jars and properties files.
$ ./gradlew
$ tree
.
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
    └── main
        └── java
            └── hello
                └── HelloWorld.java

After the gradle file is created, you need to define the build-related contents by creating a bulid.gradle file in the project dir.

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

mainClassName = 'hello.HelloWorld'


repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile "junit:junit:4.12"
}

Build & Run

Now everything is ready to build. You can build with ./gradlew build. If you look at the tree after the build is complete, a class file has been created.

$ ./gradlew build

$ tree
.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           └── hello
│   │               └── HelloWorld.class
│   ├── libs
│   │   └── build_with_gradle.jar
│   └── tmp
│       ├── compileJava
│       └── jar
│           └── MANIFEST.MF
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
    └── main
        └── java
            └── hello
                └── HelloWorld.java

You can also run compiled Java programs with ./gradlew run.

$ ./gradlew run

> Task :run
Hello World!

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

bulid.gradle

I tried from building to running, but in fact, if I dont define a script in build.gradle, it doesnt work as expected.

If you are building in Java, you need to define it as follows.

apply plugin: 'java'

If you want to run a Java program compiled with Gradle, you need to define it as follows.

apply plugin: 'application'
mainClassName = 'hello.HelloWorld'

If you want to use a 3rd party dependency library, you need to define it as follows.

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile "junit:junit:4.12"
}

If you are using a local file, you can set the dependency with compile fileTree or compile files.

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile files('libs/library.jar').
}

Reference

codechachaCopyright ©2019 codechacha