Building an Executable JAR
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the process for building the gs-rest-service application into a standalone, executable JAR file. The project is configured to use either Maven or Gradle, leveraging the Spring Boot framework to package the application with all its dependencies into a single runnable artifact. This allows the service to be started with a simple java -jar command.
The core of the application is the RestServiceApplication class, which serves as the main entry point. The build process, managed by Spring Boot plugins, ensures that this class is configured to run when the JAR is executed.
Sources: README.adoc, complete/src/main/java/com/example/restservice/RestServiceApplication.java
Build Configuration and Tools
The project can be built using either Maven or Gradle. Both build systems are configured to produce an executable JAR. Wrapper scripts (mvnw.cmd, gradlew.bat) are included to ensure a consistent build environment without requiring a local installation of Maven or Gradle.
Sources: README.adoc, complete/mvnw.cmd, complete/gradlew.bat
Key Build Files
The following table summarizes the essential files involved in the build process.
| File | Description |
|---|---|
complete/pom.xml | The Maven Project Object Model file. It defines project dependencies, properties, and the spring-boot-maven-plugin for building the executable JAR. |
test/run.sh | A shell script that demonstrates the build commands for both Maven (./mvnw clean package) and Gradle (./gradlew build). |
complete/mvnw.cmd | The Maven wrapper script for Windows. It downloads and uses the correct Maven version for the project. |
complete/gradlew.bat | The Gradle wrapper script for Windows. It ensures the correct Gradle version is used for the build. |
complete/manifest.yml | A Cloud Foundry deployment manifest that specifies the path to the built JAR (build/libs/gs-rest-service-0.1.0.jar), indicating the expected output of the Gradle build. |
Sources: complete/pom.xml, test/run.sh, complete/mvnw.cmd, complete/gradlew.bat, complete/manifest.yml
Maven Build Process
The Maven build is configured in pom.xml. It uses the spring-boot-starter-parent to inherit sensible defaults for a Spring Boot application. The key component for creating the executable JAR is the spring-boot-maven-plugin.
Sources: complete/pom.xml:6-10, complete/pom.xml:38-42
Maven Dependencies
The project requires the following dependencies for building and running the REST service:
spring-boot-starter-web: Provides support for building web applications, including RESTful applications, using Spring MVC. It includes an embedded Tomcat server by default.spring-boot-starter-test: Provides utilities for testing Spring Boot applications.
Sources: complete/pom.xml:22-32
Building with Maven
To build the executable JAR using Maven, execute the following command from the complete directory:
./mvnw clean package
This command, found in the run.sh script, instructs Maven to first clean the project (delete the target directory) and then package the application. The spring-boot-maven-plugin will then create the executable JAR in the target/ directory.
The following diagram illustrates the Maven build flow.
Sources: test/run.sh:18, complete/pom.xml:38-42
spring-boot-maven-plugin
This plugin is essential for packaging the application. It collects all the application classes and dependency JARs into a single, runnable "fat" JAR.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Sources: complete/pom.xml:37-44
Gradle Build Process
The project also supports building with Gradle. The run.sh script includes a command to trigger the Gradle build.
Sources: test/run.sh:28
Building with Gradle
To build the executable JAR using Gradle, execute the following command from the complete directory:
./gradlew build
This command tells Gradle to compile the code, run tests, and assemble the application into an executable JAR. The output is typically placed in the build/libs/ directory. The manifest.yml file confirms this location, specifying build/libs/gs-rest-service-0.1.0.jar as the application path for deployment.
Sources: test/run.sh:28, complete/manifest.yml:7
The diagram below shows the high-level build orchestration from the run.sh script.
Sources: test/run.sh
Running the Application
Once the executable JAR has been built by either Maven or Gradle, it can be run from the command line using java -jar.
For a Maven build, the command would be:
java -jar target/rest-service-complete-0.0.1-SNAPSHOT.jar
For a Gradle build, the command would be:
java -jar build/libs/gs-rest-service-0.1.0.jar
Upon execution, the Spring Boot application starts, launching the embedded web server. The service will then be available at http://localhost:8080.
Sources: README.adoc, complete/manifest.yml:7, complete/pom.xml:14
Summary
The gs-rest-service project is configured for easy packaging into a standalone executable JAR using either Maven or Gradle. The spring-boot-maven-plugin (for Maven) and corresponding Gradle plugins handle the heavy lifting of bundling the application code and all its dependencies. The included wrapper scripts ensure that developers can build the project with a consistent environment, and the final artifact can be run simply with java -jar, making development, testing, and deployment straightforward.
Sources: complete/pom.xml, test/run.sh, README.adoc