Spring-Boot with Visual Studio Code, why not!

It is totally worth trying!

Zied GUESMI
CLL-FST
7 min readMar 17, 2019

--

Running Spring-Boot application in VSCode

Edit: I have been using VSCode as my full time editor for all my java projects.

Visual Studio Code has been my favorite code editor for a while — just like almost 35% of the folks on StackOverflow in 2018.

Stack Overflow’s Survey 2018: Most Popular Development Environments

The reason behind that — at least for me — is the way this editor was designed. It is fast, lightweight and keyboard-centric. Its simple UI reduces the noise and the disturbance for the user, unlike heavy IDEs that display a lot of tools and helpers most of which may not be used for daily development tasks.

Despite being more of a code editor, VSCode has good support for main development tasks like debugging and source control management as well as integrated terminal. It is super hyper lightweight for what it’s able to do. It supports also Live Share, a feature that enables collaboratively editing and debugging with others smoothly and in real time, regardless of programming languages or application types.

It is also one of the most active free & open source projects on Github, which explains the 9000+ extensions available out there. You can check this repository for a curated list of awesome VSCode packages and resources. Enjoy!

First of all, why on earth, would I do this?

Transparency

Honestly, I was not — at any point, a fan of IDEs (it does not mean I don’t use them). I “generally” like to understand what is going under the hood, which is really difficult when one click builds the project, runs tests, generates reports, populates a database, starts the servers, deploys the application and you don’t have any clue what is happening. Of course, they have their advantages, because IDEs are generally productive and save you the pain of repeating some manual stuff over and over, but it also makes it harder to identify the problem when it occurs. Bottom line, it depends a lot on the context, whether you are in the learning process, or trying to close a ticket on a Friday evening.

In both cases, I hate wizards !

Fast & lightweight

On the other hand, VSCode is richer than traditional code editors and faster than traditional IDEs. How can I not fall for it! I can trade features for speed, I can totally do it…

One ring to rule ’em all

You do not have to switch from one editor to another when changing the project or the technology. If you are refactoring your front-end code or adding a new endpoint to your java back-end, all you need is the same editor with the same keyboard combinations.

Disclaimer: I did not say that VSCode can totally replace java IDEs like Eclipse or Intellij, or does everything they do, so be ready to compromise — a lot!

Two extension packs and we are ready to go

The extensions needed to prepare the environment.

First, if you are not already using VSCode, you can give it a try. Install it and enjoy the awesomeness!

Java language support

If you already have it installed (or finished installing it), open up VSCode and go to the extension manager (Ctrl+Shift+P/Cmd+Shift+P for Linux/Mac or F1 for both). First, we need to install the Java Extension Pack which collects some popular extensions to work with Java applications in VSCode. Those are:

Spring-Boot requirements

Once Java is supported, we can go further to develop and run Spring-Boot application. Spring-Boot Extension Pack provides the recipe for that:

  • Spring-Boot Tools that enables developing and troubleshooting Spring-Boot applications as well as editing its configuration properties.
  • Spring Initializr Java Support to generate quickstart Spring-Boot Java projects with Spring Initializer API.
  • The Spring-Boot Dashboard extension which provides an explorer in the sidebar to view all of a workspace’s Spring-Boot projects in one place. It can also be used to quickly start, stop or debug a project.

It comes also with two additional extensions: Cloud Foundry Manifest YML Support and Concourse CI Pipeline Editor which are not in the scope of this tutorial.

Build, Run & Debug…

Once the required extensions are installed, we can either start from scratch and use Spring Initializer to generate a Maven/Gradle project, or we can use an existing project. For the sake of simplicity, we are going to use the prominent Spring PetClinic Sample Application. So without any further talking, let's clone the project and open it in the editor.

Tip: You can clone the repository directly from VSCode using the Git: Clone command. So Ctrl+Shift+P/Cmd+Shift+P, type “clone” and hit Enter. You will be prompted to enter the URL and choose where to save the local repository.

When you open up the project in the editor, it will automatically start the Java Language Server and build the workspace.

The thumbs-up sign at the bottom right corner means that the Java Language Server is up and everything is ready.

Configure build tasks

Since VSCode allows integrating with external tools (like Maven, Gradle, Grunt, Gulp…) via Tasks, we can do more than just running Maven/Gradle from the [integrated] terminal. We can create custom tasks to build, run and test the project and associate them with key bindings. To do that, in the Command Palette, enter Configure Tasks and select Create tasks.json file from templates. Choose “maven and it will generate a tasks.json file containing the default configuration to build or test the spring project using maven.

For Gradle just change the commands from mvn -B verify to gradle build and from mvn -B test to gradle test .

If you would like to use your custom build script, choose “Others” instead of “maven” and follow the guide to create your personalized tasks.json.

That would be enough to get us up and running.

Build the project

Suppose you made changes to the code and wanted to build the project, use Ctrl+Shift+B/Cmd+Shift+B or type Tasks: Run build task in the command prompt. It executes the default build command (isDefault: true) and displays its output in the integrated terminal.

Run the application

CodeLens is a popular feature of VSCode that adds inline pieces of information or clickable elements to perform possible actions according to the context. In our case, it looks automatically for the entry point of the application (main method) and adds the possibility to run/debug the code. You can as well use the Spring-Boot dashboard extension to launch the app.

After hitting run, the application starts and we can follow the logs in the debug console section of the panel. You can also notice that some additional CodeLens are added, allowing to inspect injected beans, get real-time information about the running code, show live request mappings and visit endpoints directly from the editor.

Debug the code

To run the debugger we just need to set our breakpoints and click on debug CodeLens. In the screenshot below, in order to debug the method, processFindForm() we set the breakpoint in line 82 and we hit GET /owners endpoint using curl.

As you can see, the Java debugger supports many key features such as variables, threads, call stacks, evaluating/watching expressions and stepping in/out/over. All of that is displayed in the debugger console in a way that does not hide the code because VSCode comes with an extremely magnificent feature, which is hovering on variables and method parameters to show their current state directly in the editor.

Unit tests

Same as running the application, you can use CodeLens to run unit tests. If something goes wrong and a test fails, you can get the report which contains the assertion message or exception’s stack trace.

Most Java developers cannot stand the idea of working on their projects without their favorite IDEs, but, the only way to discover new (maybe better) tools and technologies is to leave the comfort zone. VSCode still has a long way to go, but it is an exceptionally active project so we can wait for some really good surprises.

--

--