Developing a Spring Boot Project Using Google Project IDX

In modern software development, cloud-based development environments are becoming increasingly popular. Google Project IDX is a powerful tool that helps developers efficiently develop and deploy applications. This article will guide you through using Google Project IDX to develop a Spring Boot project, covering the necessary configurations and usage. If you want to try it out immediately, you can open it directly using my GitHub at https://github.com/samzhu/demo-project-idx.

cover

Opening Google Project IDX

First, open Google Project IDX and log in with your Google account. Once inside Project IDX, you can choose to create a new development project or import an existing one.

Creating a New Blank Project

  1. After logging in, click the “New Project” button to create a new project.
  2. In the pop-up window, name your project and select “Blank Project” as the template.
  3. Click “Create” to create your blank project.

With this, you have successfully created a blank development project and can begin your Spring Boot development journey.

Downloading a Spring Boot Project

To download and extract a new Spring Boot project, use the following command:

1
2
curl -G https://start.spring.io/starter.tgz -d dependencies=web,actuator \
-d type=gradle-project | tar -xzvf -

This command will generate a Spring Boot project with basic configurations, including the Web and Actuator dependencies.

Here’s what the command does:

  • curl -G https://start.spring.io/starter.tgz: Downloads the Spring Boot project as a tar.gz archive.
  • -d dependencies=web,actuator: Specifies the dependencies to include in the project. In this case, it includes the Web and Actuator dependencies.
  • -d type=gradle-project: Specifies the build system to use. Here, it sets the project to use Gradle.
  • | tar -xzvf -: Extracts the downloaded tar.gz archive.

This will create a directory structure for a Spring Boot project with the specified dependencies, ready for further development.

Configuring the Development Environment

Next, we need to configure the development environment. Google Project IDX uses Nix to manage the development environment. Here’s how you can set it up:

Create a .idx/dev.nix file in the root directory of your project and add the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{ pkgs, ... }: {
channel = "stable-23.11"; # or "unstable"
services.docker.enable = true;
packages = [
pkgs.apt
pkgs.jdk21
];
env = {};
idx = {
extensions = [
"redhat.java"
"vscjava.vscode-java-debug"
"vscjava.vscode-java-dependency"
"vscjava.vscode-java-pack"
"vscjava.vscode-java-test"
"vscjava.vscode-maven"
"Pivotal.vscode-boot-dev-pack"
"vmware.vscode-spring-boot"
"vscjava.vscode-spring-boot-dashboard"
"vscjava.vscode-spring-initializr"
"java-extension-pack-jdk"
];
previews = {
enable = false;
};
workspace = {
onCreate = {};
onStart = {};
};
};
}

Explanation of the Configuration File

  • channel = "stable-23.11";: Specifies the Nixpkgs channel to use. You can choose between “stable” and “unstable” versions.
  • services.docker.enable = true;: Enables Docker services within the development environment. By setting this to true, you allow the use of Rootless Docker, which is a version of Docker that does not require root privileges.
  • packages = [ pkgs.apt pkgs.jdk21 ];: Lists the packages to be included in the environment. Here, it includes the apt package manager and JDK 21.
  • env = {};: Sets environment variables in the workspace. Currently, it is empty.
  • idx = { extensions = [ ... ]; }: Specifies the extensions to be installed in the IDE. This example includes various Java and Spring Boot extensions.
  • previews = { enable = false; }: Disables the web preview feature. You can enable and configure it if needed.
  • workspace = { onCreate = { ... }; onStart = { ... }; }: Defines lifecycle hooks for the workspace. You can specify commands to run when the workspace is created or started.

For more details on how to customize the environment using Nix, refer to the Google Project IDX guides.

Starting the Project

After completing the configuration, you can start your Spring Boot project in Google Project IDX. Use the following command to start the project:

1
./gradlew bootRun

This will start the Spring Boot application and allow you to preview the application in your browser.

Previewing Your Application in Google Project IDX

To preview your application in Google Project IDX, follow these steps. The preview feature allows you to run and view your application within the Google Project IDX environment.

Step-by-Step Guide to Setting Up Previews

  1. Enable Preview Feature

    In your .idx/dev.nix file, ensure the preview feature is enabled. If not, update the file as follows:

    1
    2
    3
    previews = {
    enable = true;
    };
  2. Configure the Preview Command

    You need to specify the command to start your application for preview. Add the following configuration within the previews section:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    previews = {
    enable = true;
    web = {
    command = ["./gradlew" ":bootRun" "--args='--server.port=$PORT'"];
    manager = "web";
    env = {
    PORT = "$PORT";
    };
    };
    };
    • command: Specifies the command to run your application. Here, it uses Gradle to run the Spring Boot application and sets the server port to $PORT, dynamically assigned by the IDX environment.
    • manager: Indicates this is a web preview.
    • env: Sets the environment variables for the server, with PORT dynamically assigned.
  3. Open the Required Port

    Ensure the port used by your application is open and not restricted. The following example opens port 4000:

    1
    2
    3
    export port=4000 # Don't use restricted ports (8000, 9000-9002)
    export API_SERVICE="https://$port-$WEB_HOST"
    echo $API_SERVICE

    This sets the API_SERVICE environment variable to point to the correct port, ensuring that the frontend can access the backend services.

  4. Start the Application

    Use the following command to start your Spring Boot application:

    1
    ./gradlew bootRun
  5. Access the Web Preview

    • Open the command palette in Google Project IDX (Cmd+Shift+P on Mac or Ctrl+Shift+P on ChromeOS, Windows, or Linux).
    • Select Project IDX: Show Web Preview to open the web preview.
  6. Refresh the Preview

    If the preview does not update correctly, perform a Hard Restart:

    • Open the command palette (Cmd+Shift+P on Mac or Ctrl+Shift+P on ChromeOS, Windows, or Linux).
    • Select Hard Restart under the IDX category.

References