<a id="devpack-for-spring"></a>

# 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](https://documentation.ubuntu.com/ubuntu-for-developers/explanation/devpacks.md#devpacks). For instructions on installing the Java toolchain manually, see [How to set up a development environment for Java on Ubuntu](https://documentation.ubuntu.com/ubuntu-for-developers/howto/java-setup.md#install-java).

## 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/edge`

## Setting 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-jdk`

The `--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.

1. Run the command in the directory where the project is created:
   `dev@ubuntu:~$ ``devpack-for-spring boot start`
2. In the wizard, select **Gradle** as the build system and add the **Spring Web** dependency. Confirm the remaining defaults.
3. 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 web`
```text
Extracted 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:

1. Create the file `src/main/java/com/example/demo/GreetingController.java` inside the project directory:
   ```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 `package` declaration accordingly.

### Building and running the application

1. Build the project and start the application using the Gradle wrapper:
   `dev@ubuntu:~/demo$ ``./gradlew bootRun`

   The application starts and listens on port 8080.
2. In another terminal, verify the endpoint:
   `dev@ubuntu:~$ ``curl http://localhost:8080/greeting`
   ```text
   Hello from Spring Boot on Ubuntu!
   ```
3. 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 format`

To list all available plugins, use `devpack-for-spring plugins`.

## 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 libraries`

To install or remove a library, use the `install` and `remove` sub-commands respectively.

## What next

- [How to set up a development environment for Java on Ubuntu](https://documentation.ubuntu.com/ubuntu-for-developers/howto/java-setup.md#install-java)
- [Compiling Spring Boot applications to native executables](https://documentation.ubuntu.com/ubuntu-for-developers/tutorials/java-advanced/graalvm-use.md#use-graalvm)
- [Fast start for Spring Boot apps using OpenJDK CRaC](https://documentation.ubuntu.com/ubuntu-for-developers/tutorials/java-advanced/crac-use.md#use-crac)

## Additional resources

- [devpack-for-spring README](https://github.com/canonical/devpack-for-spring/blob/main/devpack-for-spring/README.md)
