Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot application run configurations with VScode

Within the Spring-Tools-suite (customised version of eclipse), there is an option to define multiple run configurations for the same application and then to run them.

For example, when testing a Eureka Server and running multiple instances of the same application with different port and name definitions to check registration.

Does anyone know of a method to define similar run configurations using Spring and Java Extensions with Visual Studio Code?

like image 384
Eogcloud Avatar asked Jul 25 '19 17:07

Eogcloud


People also ask

How do I run an existing spring boot application in VS Code?

To install, launch VS Code and from the Extensions view (Ctrl+Shift+X), search for vscode-spring-initializr . Once you have the extension installed, open the Command Palette (Ctrl+Shift+P) and type Spring Initializr to start generating a Maven or Gradle project and then follow the wizard.

How is run configuration set in VS Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I run an application in VS Code?

Pressing F1 and then choosing “Run Code” also works. If you want to type it out after pressing F1, you're free to do that as well. Users can right-click the text editor and then select “Run Code” in the context menu. With only two clicks, your code will run.

Where is Java configuration runtimes in VS Code?

You can access the setting by clicking menu “File->Preferences->Settings”, searching “java. configuration. runtimes” on “User” tab, and then opening the setting. You can set multiple JDK versions, and VS Code will pick the best version per your default or project's source level.


2 Answers

I tested this in VSCode-1.40.2 using Java-1.8.0_231-b11

You will need the following to launch launch.json: https://code.visualstudio.com/docs/java/java-debugging

For STS development you can download from: https://marketplace.visualstudio.com/items?itemName=Pivotal.vscode-boot-dev-pack

Here is my launch.json settings which I use to spawn two instances of a microservice I developed using VSCode. Note how I am setting the server port in vmArgs to serve on 8000 & 8001

{
"configurations": [
    {
        "type": "java",
        "name": "CodeLens (Launch-8000) - CurrencyExchangeServiceApplication",
        "request": "launch",
        "mainClass": "com.microservices.currencyexchangeservice.CurrencyExchangeServiceApplication",
        "projectName": "currency-exchange-service",
        "vmArgs": "-Dserver.port=8000"
    },
    {
        "type": "java",
        "name": "CodeLens (Launch-8001) - CurrencyExchangeServiceApplication",
        "request": "launch",
        "mainClass": "com.microservices.currencyexchangeservice.CurrencyExchangeServiceApplication",
        "projectName": "currency-exchange-service",
        "vmArgs": "-Dserver.port=8001"
    }
]
}

You will end up with two configurations in your editor like so: enter image description here

Hope this helps.

like image 50
Priyesh Diukar Avatar answered Sep 28 '22 08:09

Priyesh Diukar


In direct answer to your question, the function you may be looking for is:

Debugging with Launch Configurations

To debug a simple app in VS Code, press F5 and VS Code will try to debug your currently active file.

However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.

The documentation is quite extensive on that function. As in Eclipse this is built in relation to debugging (where launching the apps from within Eclipse don't necessarily attach to the debugger). You will be most familiar with Launch configurations (as opposed to Attach) which are analogous to Run Configurations. Add a new configuration illustrates how to build a launch.json file via snippets instead of via a wizard as with Eclipse.

launch.json wizard construction

Regarding Spring Boot specifically, Pivotal has a couple of extensions for you: Spring Boot Tools and Spring Initializr support which give you some extra functionality along with Launches.

Run Configurations and Launch configurations are good in a pinch, but as Leo Zhu mentioned in the comments, Maven profiles (Configure active profile in SpringBoot via Maven / docs) or Gradle's equivalent simple use of if statements to control environments (Maven profiles equivalent of Gradle / Gradle blog entry) are more portable and are IDE independent. These methods have my personal vote.

like image 32
Jon Sampson Avatar answered Sep 28 '22 07:09

Jon Sampson