Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug a spring mvc web app with web.xml on Visual Studio Code?

I am using Visual Studio Code to develop a spring mvc application with maven build tool.

I have maven command line installed on my system. I have also added tomcat plugin to my pom.xml. The entry point for my application is web.xml.

I have created vs code tasks to run maven commands on the integrated terminal.

How should I attach debugger breakpoints for inspecting variable values through the program run.

My task.json looks like this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "verify",
            "type": "shell",
            "command": "mvn -B verify",
            "group": "build"
        },
        {
            "label": "test",
            "type": "shell",
            "command": "mvn -B test",
            "group": "test"
        },
        {
            "label": "create a  new maven webapp",
            "type": "shell",
            "command": "mvn archetype:generate -DgroupId=com.lumen.app -DartifactId=lumen-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false",
            "group": "test"
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "mvn clean",
            "group": "test"
        },
        {
            "label": "install",
            "type": "shell",
            "command": "mvn install",
            "group": "test"
        },
        {
            "label": "run on server",
            "type": "shell",
            "command": "mvn tomcat7:run",
            "group": "test"
        },
        {
            "label": "redeploy on server",
            "type": "shell",
            "command": "mvn tomcat7:redeploy",
            "group": "test"
        },
        {
            "label": "undeploy on server",
            "type": "shell",
            "command": "mvn tomcat7:undeploy",
            "group": "test"
        },
        {
            "label": "deploy on server",
            "type": "shell",
            "command": "mvn tomcat7:deploy",
            "group": "test"
        }
    ]
} 
like image 963
Vikramjeet Avatar asked Jul 07 '26 15:07

Vikramjeet


1 Answers

Considering these facts :- 1. The application is a mvc webapp 2. Debugger is provided by VS Code

this is a simple two step process but you need to understand how would this setup work.

Firstly, configure a task so that your app runs on debug mode. Secondly, attach VS Code debugger to the port which the debug mode listens and responds to.

Debug mode allows any debugger to attach themselves to application over a certain port.

In your tasks.json configure a task like :

{
        "label": "debug on server",
        "type": "shell",
        "command": "export MAVEN_OPTS='-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000';mvn tomcat7:run",
        "group": "test"
}

this allows your code to run in debug mode and then attacj VS Code debugger to the port 8000 .

To attach debugger add below configuration to launch.json :

{
        "type": "java",
        "name": "Debug (Attach)",
        "request": "attach",
        "hostName": "localhost",
        "port": 8000
}

Now attach your debug points after running debug on server task.

like image 132
Vikramjeet Avatar answered Jul 10 '26 06:07

Vikramjeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!