Devpack for Spring¶
This tutorial shows how to use devpack-for-spring to set up a Java development environment, scaffold a Spring Boot project, and run it on Ubuntu. The devpack automates environment setup and project scaffolding, and provides offline library management.
For background on devpacks, see Devpacks. For instructions on installing the Java toolchain manually, see How to set up a development environment for Java on Ubuntu.
Installing the devpack¶
devpack-for-spring is distributed as a classic-confinement snap. Install it with:
dev@ubuntu:~$ sudo snap install devpack-for-spring --classic --channel latest/edgeSetting up the development environment¶
The setup command installs the tools required for Java and Spring Boot development, including OpenJDK, build tools, and container runtimes.
To install OpenJDK 21 non-interactively, run:
dev@ubuntu:~$ devpack-for-spring setup --add openjdk-21-jdkThe --add option accepts a comma-separated list of software items defined in the devpack’s configuration. To see all available items, run devpack-for-spring setup without arguments to launch the interactive menu for selecting and deselecting items individually.
Note
Gradle is not installed separately — the scaffolded project includes the Gradle wrapper, which downloads the required Gradle version automatically.
Note
The list of available software items is defined in the setup-configuration.yaml file. To override the default selection, create a custom configuration file at ~/.config/devpack-for-spring/setup-configuration.yaml or set the SPRING_CLI_SETUP_COMMANDS_CONFIGURATION environment variable to point to your file.
Scaffolding a Spring Boot project¶
The boot start command launches an interactive wizard that scaffolds a new Spring Boot project.
Run the command in the directory where the project is created:
dev@ubuntu:~$devpack-for-spring boot startIn the wizard, select Gradle as the build system and add the Spring Web dependency. Confirm the remaining defaults.
After the wizard completes, a new project directory is created with a standard Spring Boot project structure.
Using the non-interactive CLI¶
To see what the wizard does under the hood, the equivalent non-interactive command is:
dev@ubuntu:~$ devpack-for-spring boot start --path demo --project gradle-project \
> --language java --boot-version 4.1.0 --group com.example \
> --artifact demo --name demo --description demo \
> --package-name com.example.demo --packaging jar --java-version 21 \
> --version 0.0.1 --dependencies webExtracted to <path>/demo
The version numbers and other values are examples — adjust them to match your environment and requirements. This creates a demo/ directory with the same project structure as the wizard. The tool asks for any missing parameters in an interactive way.
Adding a REST endpoint¶
The scaffolded project includes a static index page. To add a simple REST endpoint that returns a greeting, create a controller class:
Create the file
src/main/java/com/example/demo/GreetingController.javainside the project directory:src/main/java/com/example/demo/GreetingController.java¶package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController class GreetingController { @GetMapping("/greeting") String greeting() { return "Hello from Spring Boot on Ubuntu!\n"; } }
The exact package name depends on the group name chosen during scaffolding. Adjust the
packagedeclaration accordingly.
Building and running the application¶
Build the project and start the application using the Gradle wrapper:
dev@ubuntu:~/demo$./gradlew bootRunThe application starts and listens on port 8080.
In another terminal, verify the endpoint:
dev@ubuntu:~$curl http://localhost:8080/greetingHello from Spring Boot on Ubuntu!
Press Ctrl-C in the first terminal to stop the application.
Formatting source code¶
devpack-for-spring includes a format plugin that applies consistent formatting to the project source code. To run it:
dev@ubuntu:~/demo$ devpack-for-spring run formatTo list all available plugins, use devpack-for-spring plugins.
Gradle deprecation warnings
The format command runs a Gradle build internally. Gradle may report deprecated features and generate a problems report at build/reports/problems/problems-report.html. These deprecation warnings originate from Gradle init scripts that the devpack installs — not from the project source code. They are safe to ignore and will be resolved in future devpack releases.
Managing offline library dependencies¶
devpack-for-spring provides Spring Boot libraries as content snaps, which are prebuilt binary packages stored locally. Using content snaps reduces build times by avoiding repeated downloads of dependencies.
To list installed and available libraries:
dev@ubuntu:~$ devpack-for-spring librariesTo install or remove a library, use the install and remove sub-commands respectively.