Gitpod comes with great support for Java builtin. Still depending on your particular project you might want to further optimize the experience.
A great example for a ready-to-code Java dev environment is our Spring Petclinic Example.
In order to setup your project, you should run
gp init in the terminal first. This will create the two configuration files needed to explain Gitpod what your project needs.
A dev environment is based on a Docker image. But don’t fear you don’t need to understand Docker in order to get started. In most cases the default docker image is enough.
For Java it comes ready equipped with SDKMAN!, which is a version manager that takes care of managing and installing not only different Java versions, but also different version of other JVM langauges such as Scala or Kotlin (both suppoeted in Gitpod, too).
It also installs Maven and Gradle in case you don’t use the wrapper version that is often recommended.
Check out the documentation of SDKMAN! to see how to use it or simply type
sdk help in the terminal.
Although you can use sdk in your terminal, you should put the tools and versions into your Dockerfile, so you and your team get the very same configuration everytime.
To do that you need to alter the generated
.gitpod.Dockerfile. Here’s an example that will install Java 12 with the J9 VM from “Adopt a JDK”.
FROM gitpod/workspace-full RUN bash -c ". /home/gitpod/.sdkman/bin/sdkman-init.sh \ && sdk install java 12.0.1.j9-adpt"
Note that you always need to run the
sdkman-init.shin a bash before you can use sdk.
You can add additional tools and versions like this:
FROM gitpod/workspace-full RUN bash -c ". /home/gitpod/.sdkman/bin/sdkman-init.sh \ && sdk install java 12.0.1.j9-adpt \ && sdk ..."
Gitpod provides disposable dev environments, which means you are getting fresh development environments for every task. So configuring them to be ready-to-code is crucial to get the most out of Gitpod.
In the generated
.gitpod.yml you will find the following section:
# List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/44_config_start_tasks/ tasks: - init: echo 'init script' # runs during prebuild command: echo 'start script'
You can have as many tasks as you which, each will result in an opened terminal when you start a dev environment.
Each task supports multiple phases, most importantly
command. In order to be ready to code a dev environment should not only be in sync with the git repo, but also needs to have the currently checked out state built.
For instance in the Spring Petclinic demo the command section looks like this:
# start up tasks tasks: - init: ./mvnw package -DskipTests command: java -jar target/*.jar
Since we have installed the Gitpod app on that GitHub repository, Gitpod will prebuild any branch as soon as it starts. During a prebuild it will
- start a container based on the docker image,
- clone the repository and checkout the respective branch,
- run the
initparts of every task,
- capture the result and store it
Once you or your team mates start a dev environment, you will get the prebuild state. The log output from
init is still presented in the terminal but it will have two additional lines, e.g.:
🍌 This task ran as part of a workspace prebuild. 🎉 You just saved 4 minutes of watching your code build.
🚧 Please help improve this section by editing it.
- Explain what the extensions do, and how to use them
- Explain how to set up a Java project for debugging
Gitpod comes equipped with the following VS Code extensions:
You can install additional extensions for your project if you want.
Most of the information you find in the Java for VS Code documentation apply to Gitpod as well.