Building with Gradle
Relevant source files
The following files were used as context for generating this wiki page:
This document provides a technical overview of the Gradle build system as configured in the gs-rest-service project. The project utilizes the Gradle Wrapper, which ensures a consistent and reproducible build environment by using a specific version of Gradle without requiring a manual installation. This allows developers to compile, test, and package the application using simple commands.
The build process is initiated via the gradlew (for Unix-like systems) or gradlew.bat (for Windows) scripts. These scripts handle the setup of the Java environment and execute the Gradle Wrapper Java application, which in turn performs the build tasks. The primary output of the build is an executable JAR file.
Sources: README.adoc:28-30, complete/gradlew.bat, test/run.sh:26
Gradle Wrapper Scripts
The project includes wrapper scripts for both Windows (gradlew.bat) and Unix-like (gradlew) operating systems. These scripts are the entry points for all Gradle tasks.
Sources: complete/gradlew, complete/gradlew.bat, initial/gradlew.bat
Script Execution Flow
The wrapper scripts perform several key steps before executing Gradle:
- Environment Setup: The script sets up its home directory (
APP_HOME). - Java Virtual Machine (JVM) Configuration: It defines default JVM options, such as memory allocation (
-Xmx64m,-Xms64m). These can be overridden byJAVA_OPTSorGRADLE_OPTSenvironment variables. - Java Executable Discovery: The script searches for a Java executable. It first checks if the
JAVA_HOMEenvironment variable is set and valid. If not, it attempts to use thejavacommand from the system'sPATH. - Gradle Wrapper Execution: Once Java is found, the script executes the
gradle-wrapper.jarfile, passing along any command-line arguments. The main class executed isorg.gradle.wrapper.GradleWrapperMain.
Sources: complete/gradlew.bat:26-60, initial/gradlew.bat:26-60
The following diagram illustrates the execution flow of the gradlew.bat script.
Sources: complete/gradlew.bat:31-51
Environment Variables
The wrapper scripts can be configured using several environment variables.
| Variable | Description | Source File |
|---|---|---|
JAVA_HOME | Specifies the location of the JDK installation. If not set, the script searches for java in the system's PATH. | complete/gradlew.bat:31-32 |
JAVA_OPTS | Used to pass additional JVM options to the Gradle process. | complete/gradlew.bat:60 |
GRADLE_OPTS | Also used to pass JVM options to the Gradle process. | complete/gradlew.bat:60 |
DEBUG | If set, script execution commands are echoed to the console. | initial/gradlew.bat:20 |
Build and Test Lifecycle
The project's build lifecycle is managed through Gradle tasks. The test automation script test/run.sh demonstrates the primary commands used for building and cleaning the project.
Sources: test/run.sh, README.adoc
Key Gradle Tasks
./gradlew build: This is the main command to build the project. It compiles the source code, runs tests, and packages the application into an executable JAR file. Thetest/run.shscript executes this command on thecompleteversion of the project../gradlew compileJava: This task, used intest/run.shon theinitialproject, compiles the Java source files without running tests or packaging the application../gradlew clean: While not explicitly shown as a command, thetest/run.shscript simulates a clean by manually removing thebuilddirectory (rm -rf build), which is the standard output directory for Gradle builds.
Sources: test/run.sh:10,13,26,32, README.adoc:103
The following sequence diagram shows the interaction when a developer runs the build command.
Sources: complete/gradlew.bat:60, test/run.sh:26
Build Artifacts
The primary output of the build task is a JAR file located in the build/libs directory. The complete/manifest.yml file, used for deployment, specifies the exact path to this artifact.
- Artifact Path:
build/libs/gs-rest-service-0.1.0.jar
This JAR is an executable "fat jar" containing all necessary dependencies to run the Spring Boot application.
Sources: complete/manifest.yml:7, test/run.sh:10,32
The build process can be summarized as taking source files and producing a runnable application.
Sources: README.adoc:100-103, complete/manifest.yml:7
Summary
The gs-rest-service project relies on the Gradle Wrapper for a standardized and portable build system. By using the gradlew and gradlew.bat scripts, developers can easily compile, test, and package the REST service into an executable JAR. The process is self-contained, only requiring a JDK to be available on the host system, which simplifies the development and continuous integration workflow.
Sources: complete/gradlew.bat, README.adoc