英文:
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种不同的方法,并尝试了以下操作:
- 创建一个名为
launch.json
的文件,调用命令mvn
或mvnDebug
在preLaunchTask
中。
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"request": "launch",
...
"preLaunchTask": "mvnDebug",
"vmArgs": [ "clean", "install", "-Dparam1=\"blabla\"", "-Dparam2=\"blablabla\"" ]
}
]
}
我还尝试了将所有命令都传递给preLaunchTask
而没有使用vmArgs
。但是并未成功。
- 创建一个名为
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": []
}
]
}
- 我尝试的第三种方法是使用
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="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:
- Create a
lunch.json
file calling the commandmvn
ormvnDebug
in thepreLaunchTask
.
{
"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.
- Creating a
task.json
file passing the script and alaunch.json
file which will call inpreLaunchTask
the task created with the name defined in the parametertaskName
oftask.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": []
}
]
}
- 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:
- What does this message means?
- Why is it waiting for a port?
- How does VSCode use a socket to do it?
- What is the best approach and why?
- 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
- 在
launch.json
中,属性vmArgs
是
> 用于 JVM 的额外选项和系统属性(例如 -Xms<size> -Xmx<size> -D<name>=<value>),它可以接受一个字符串或字符串数组。
debugging-launch
因此它对于你的应用程序不起作用。
- 在
tasks.json
中,属性command
用于指定要执行的命令;
> 自定义任务
- 建议使用设置
maven.executable.options
,它为所有 mvn 命令指定了默认选项。
> vscode-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;
3.It's recomended to use the setting maven.executable.options
, which specifies default options for all mvn commands.
>vscode-maven
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论