如何配置VSCode以使用参数运行Java MAVEN应用程序?

huangapple go评论79阅读模式
英文:

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

问题

我有一个没有主方法的代码。该项目是使用以下命令执行的:

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

在Eclipse或IntelliJ中,我只需创建一个Maven执行器,将goas定义为clean install,并使用-Dparam格式传递Maven参数。

在VSCode中,我看到了3种不同的方法,并尝试了以下操作:

  1. 创建一个名为launch.json的文件,调用命令mvnmvnDebugpreLaunchTask中。
{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "java",
        "request": "launch",
        ...
        "preLaunchTask": "mvnDebug",
        "vmArgs": [ "clean", "install", "-Dparam1=\"blabla\"", "-Dparam2=\"blablabla\"" ]

    }
  ]
}

我还尝试了将所有命令都传递给preLaunchTask而没有使用vmArgs。但是并未成功。

  1. 创建一个名为task.json的文件,传递脚本,以及一个launch.json文件,在其中在preLaunchTask中调用以task.json中的taskName参数定义的任务。
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "request": "launch",
            "preLaunchTask": "Debug",
            "name": "Launch Program",
            "sourceMaps": true
        }
    ]
}

以及tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Debug",
            "type": "java",
            "script": "mvnDebug clean install -Dparam1=\"folderName\" -Dparam2=\"blablabla\"",
            "problemMatcher": []
        }
    ]
}
  1. 我尝试的第三种方法是使用settings.json
{
    "maven.terminal.favorites": [
        {
            "alias": "CLEAN COMPILE",
            "command": "clean install -Dparam1=\"value\" -Dparam2=\"blabla\"",
            "debug": true
        },
    ]
}

对于所有这些方法,我在终端中得到了以下消息:

Listening for transport dt_socket at address: 56787

我需要从你这里获得的信息是:

1)这个消息的含义是什么?
2)为什么它在等待端口?
3)VSCode如何使用套接字进行此操作?
4)最佳方法是什么,为什么?
5)最重要的问题:如何在没有主函数的情况下在VSCode中运行带有JAVA MAVEN参数的代码?

注意:我的JAVA版本是JDK11,但我也尝试过JDK 8。

非常感谢。


<details>
<summary>英文:</summary>

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

```shellscript
$mvn clean install -Dparam1=&quot;folder&quot; -Dparam2=&quot;path&quot;

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.
{
  &quot;version&quot;: &quot;0.2.0&quot;,
  &quot;configurations&quot;: [
    {
        &quot;type&quot;: &quot;java&quot;,
        &quot;request&quot;: &quot;launch&quot;,
        ...
        &quot;preLaunchTask&quot;: &quot;mvnDebug&quot;,
        &quot;vmArgs&quot;: [ &quot;clean&quot;, &quot;install&quot;, &quot;-Dparam1=\&quot;blabla\&quot;&quot;, &quot;-Dparam2=\&quot;blablabla\&quot;&quot; ]

    }
  ]
}

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.
{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;type&quot;: &quot;java&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;preLaunchTask&quot;: &quot;Debug&quot;,
            &quot;name&quot;: &quot;Launch Program&quot;,
            &quot;sourceMaps&quot;: true
        }
    ]

And the tasks.json:

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

答案1

得分: 5

  1. launch.json 中,属性 vmArgs

> 用于 JVM 的额外选项和系统属性(例如 -Xms<size> -Xmx<size> -D<name>=<value>),它可以接受一个字符串或字符串数组。
debugging-launch

因此它对于你的应用程序不起作用。

  1. tasks.json 中,属性 command 用于指定要执行的命令;

> 自定义任务

  1. 建议使用设置 maven.executable.options,它为所有 mvn 命令指定了默认选项。
    > vscode-maven

如何配置VSCode以使用参数运行Java MAVEN应用程序?

英文:

1.In launch.json, the attribute vmArgs is

> The extra options and system properties for the JVM (for example
> -Xms<size> -Xmx<size> -D<name>=<value>), 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

如何配置VSCode以使用参数运行Java MAVEN应用程序?

huangapple
  • 本文由 发表于 2020年10月17日 01:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64393921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定