Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure VSCode to run Java MAVEN applications with parameters?

I have a code without main method. The project was executed using the following command:

$mvn clean install -Dparam1="folder" -Dparam2="path"

In Eclipse or IntelliJ, I just need to create a maven executor, define the goas as clean install and pass the maven parameters using the -Dparam format.

In VSCode, I have saw 3 different approaches and experimented the following:

  1. Create a lunch.json file calling the command mvn or mvnDebug in the preLaunchTask.
{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "java",
        "request": "launch",
        ...
        "preLaunchTask": "mvnDebug",
        "vmArgs": [ "clean", "install", "-Dparam1=\"blabla\"", "-Dparam2=\"blablabla\"" ]

    }
  ]
}

I also have tested passing all the commands in the preLaunchTask without the vmArgs. And did not work.

  1. Creating a task.json file passing the script and a launch.json file which will call in preLaunchTask the task created with the name defined in the parameter taskName of task.json.
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "request": "launch",
            "preLaunchTask": "Debug",
            "name": "Launch Program",
            "sourceMaps": true
        }
    ]

And the tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Debug",
            "type": "java",
            "script": "mvnDebug clean install -Dparam1=\"folderName\" -Dparam2=\"blablabla\"",
            "problemMatcher": []
        }
    ]
}
  1. The 3rd approache I've tried was using settings.json:
{
    "maven.terminal.favorites": [
        {
            "alias": "CLEAN COMPILE",
            "command": "clean install -Dparam1=\"value\" -Dparam2=\"blabla\"",
            "debug": true
        },
    ]
}

For all of them I got the message in the terminal:

Listening for transport dt_socket at address: 56787

What I need from you, guys, is:

  1. What does this message means?
  2. Why is it waiting for a port?
  3. How does VSCode use a socket to do it?
  4. What is the best approach and why?
  5. THE MOST IMPORTANT ONE: How to run my JAVA MAVEN parameterized without a main function code using VSCode?

OBSERVATION: My JAVA version is JDK11, but I've tried with JDK 8 too.

Thank you a lot.

like image 244
Aristotle Avatar asked Sep 17 '25 15:09

Aristotle


1 Answers

1.In launch.json, the attribute vmArgs is

The extra options and system properties for the JVM (for example -Xms -Xmx -D=), it accepts a string or an array of string. debugging-launch

so it won't work for your application.

2.In tasks.json, the attribute command is for the command to execute;

Custome Tasks

3.It's recomended to use the setting maven.executable.options , which specifies default options for all mvn commands.

vscode-maven

enter image description here

like image 63
Molly Wang-MSFT Avatar answered Sep 19 '25 05:09

Molly Wang-MSFT