Spring-Boot application run configurations with VScode
Asked Answered
B

2

6

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?

Backstretch answered 25/7, 2019 at 17:1 Comment(1)
you could configure active profile in SpringBoot via Maven,you could refer to this :#42391360Enlightenment
F
8

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.

Flugelhorn answered 29/7, 2019 at 14:51 Comment(0)
G
8

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.

Gallbladder answered 29/11, 2019 at 10:46 Comment(1)
Thank you, was looking for the running port config, not the debug port. Thank you.Larios

© 2022 - 2024 — McMap. All rights reserved.