Search code examples
mavendebuggingvisual-studio-codevscode-debuggermaven-profiles

How do I specify the maven profile name to use when debugging a spring boot app with Visual Studio Code?


This might very well be a duplicate of this question, but the answer there doesn't work.

In my POM file, I have several profiles defined similar to this:

<profiles>
    <profile>
        <id>dev</id>
        <properties> 
            <activated-profile>dev</activated-profile>
        </properties>
    </profile>
    ...
</profiles>

activated-profile is a property used in the application.properties file to tell spring boot what profile to use:

...
spring.profiles.active=@activated-profile@
...

If I run mvn clean install package spring-boot:repackage -Pdev in a cmd prompt, I can see in the cmd prompt window The following profiles are active: dev, indicating that the correct profile is active.

If I try to launch and debug the app from Visual Studio Code, in the terminal I get The following profiles are active: @activated-profile@.

I tried setting args in launch.json:

{
    ...
    "args": ["activated-profile=dev"],
    "vmArgs": ["Dactivated-profile=dev"],
    ...
}

I also tried setting a custom environment variable for maven in settings.json, as well as the answer in the question which you will undoudbtedly tell me I duplicated:

{
    ...
    "maven.executable.options": "-Pdev",    
    "maven.terminal.customEnv": [{
        "environmentVariable": "activated-profile", "value": "dev" 
    }],
    ...
}

I also tried prepending activated-profile with -, --, D, -D or --D, as well as renaming activated-profile to activatedprofile in case the hyphen messes things up somehow.

Nothing works.

The only way I can get VS Code to start debugging is if I specify the default profile in the POM file:

<activation>
    <activeByDefault>true</activeByDefault>
</activation>

However, this is not ideal because everytime I want to build the app with a different profile, I have to remove that piece of code since maven, in it's infinite wisdom, decided not to override that profile if I specify it in the command line, rendering -Pdev or -Pint useless. In other words, if I want Jenkins to be able do build the app, the activeByDefault XML should not exist in the POM.


Solution

  • The following solution assumes you already have the Java Extension Pack installed.

    If not, please proceed with the installation then continue reading (Link to install Java Extension Pack)

    In your launch.json configuration (Run -> Open Configurations) you need to use vmArgs as follows:

    {
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Launch MyApp",
            "request": "launch",
            "mainClass": "com.example.MySpringApplication",
            "projectName": "myproject",
            "vmArgs": " -Dspring.profiles.active=dev"
        }
     ]
    }
    

    You can (of course) use multiple profiles at the same time for example if you already have separated your Spring security config into a different file (for instance devsec-application.yaml):

    "vmArgs": " -Dspring.profiles.active=dev,devsec"