Gradle

In the previous post, we learned about Gradle, which supplements advantages of Ant and disadvantages of Maven. In this post, learning about methods for creating Gradle projects, structure of created projects, and Gradle Wrapper that enables building projects even in environments where Gradle is not installed.


Project Initialization

Gradle projects can start through the init command. Executing this command automatically creates basic Gradle-related files at the current location. When creating, you can specify project type with the --type option.

Project Types

  • basic: Default type. If you don’t give the --type option, it’s specified by default.
  • java-application: Java application project type. App.java is created by default.
  • java-library: Java library project type.
  • groovy-application: Groovy application project.
  • There are also scala-library, pom, etc.
$ gradle init --type java-application

BUILD SUCCESSFUL in 7s
2 actionable tasks: 2 executed

In the example, I specified a Java application project with the --type option for simple Java project creation. App.java file is created by default.

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package temp;

public class App {
    public String getGreeting() {
        return "Hello world.";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

Building and executing simply. Build through the build command, and for Java execution, give the -jar option to the java command. When building, jar files are created under the build/libs path.

# Build project
$ gradle build

BUILD SUCCESSFUL in 1s
7 actionable tasks: 7 executed

# Execute jar file
$ java -jar build/libs/temp.jar
build/libs/temp.jar has no default Manifest attributes.

But when executing, an error occurs saying Manifest attributes don’t exist. You must declare manifest attributes at the bottom of the build.gradle file. It’s the same as making an executable jar file.

jar {
    manifest {
        attributes 'Main-Class': 'temp.App'
    }
}

After modifying the build.gradle file, build and execute again.

# Build project
$ gradle build

BUILD SUCCESSFUL in 625ms
7 actionable tasks: 7 up-to-date

# Execute jar file
$ jar -jar build/libs/temp.jar
Hello world.


Gradle Wrapper

To use Gradle, it basically must be installed in the local environment. Especially when building existing projects, you sometimes need to match Java versions. But using Gradle Wrapper, you can build projects regardless of Gradle installation or Java version.

Gradle Wrapper enables executing Gradle Tasks even without Gradle installation. By building through this, you can use Gradle of the same version as the project. Therefore, using Gradle Wrapper is recommended.

As an example, building the Gradle project created earlier on another Java version. Using gradle build basically uses Gradle installed locally.

# Build project
$ gradle build

BUILD SUCCESSFUL in 625ms
7 actionable tasks: 7 up-to-date

# Execute jar file
$ java -jar build/libs/temp.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError:
  temp/App has been compiled by a more recent version of the Java Runtime (class file version 57.0),
  this version of the Java Runtime only recognizes class file versions up to 52.0

In such situations, using Gradle Wrapper. Building with wrapper is done by entering ./gradlew build.

# Build project
$ ./gradlew build

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

# Execute jar file
$ java -jar build/libs/temp.jar
Hello world.

Like the execution result, you can resolve issues that can occur when project versions differ.


Project Structure

Checking the project structure through the tree command in the directory where Gradle-related files are created. You can confirm the following structure.

$ tree
.
β”œβ”€β”€ build.gradle
β”œβ”€β”€ gradle
β”‚   └── wrapper
β”‚       β”œβ”€β”€ gradle-wrapper.jar
β”‚       └── gradle-wrapper.properties
β”œβ”€β”€ gradlew
β”œβ”€β”€ gradlew.bat
β”œβ”€β”€ settings.gradle
└── src
    β”œβ”€β”€ main
    β”‚   β”œβ”€β”€ java
    β”‚   β”‚   └── temp
    β”‚   β”‚       └── App.java
    β”‚   └── resources
    └── test
        β”œβ”€β”€ java
        β”‚   └── temp
        β”‚       └── AppTest.java
        └── resources

build.gradle

Gradle’s basic build file. This file specifies content about project builds.

gradle/wrapper/gradle-wrapper.jar

Gradle Wrapper file. When building projects with the gradlew command, this file is referenced to configure settings files, so unlike gradle build, there’s no impact on new environments.

gradle/wrapper/gradle-wrapper.properties

Gradle Wrapper configuration file.

gradlew

Execution script for Unix.

gradlew.bat

Execution script for Windows.

settings.gradle

Project configuration information file. When configuring multi-projects, relationships with sub-projects must be described here. Projects are configured based on specified information.

src Folder

Becomes the project’s source folder. Like directory names src/main/java and src/main/test, looking at the structure, it’s identical to maven.