如何创建可在 Visual Studio Code 中运行的 Java/Maven 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46671308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a Java / Maven project that works in Visual Studio Code?
提问by dashman
I'm trying to create a maven project - so that I can compile Java files in the root folder and output the class files in another folder.
我正在尝试创建一个 maven 项目 - 以便我可以在根文件夹中编译 Java 文件并在另一个文件夹中输出类文件。
I've already downloaded mvn.
我已经下载了 mvn。
I'm trying to integrate with VS Code. My goal is to edit the java files in VS Code and on saving the compiler saves the .class file in the appropriate output folder.
我正在尝试与 VS Code 集成。我的目标是在 VS Code 中编辑 java 文件,并在保存编译器时将 .class 文件保存在适当的输出文件夹中。
That's all - no war or jar files.
这就是全部 - 没有战争或 jar 文件。
Any help?
有什么帮助吗?
采纳答案by Steve Chambers
Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-
这是一个完整的步骤列表 - 您可能不需要步骤 1-3,但为了完整起见,我将它们包括在内:-
- Download VS Codeand Apache Mavenand install both.
- Install the Visual Studio extension pack for Java - e.g. by pasting this URL into a web browser:
vscode:extension/vscjava.vscode-java-pack
and then clicking on the green Install button after it opens in VS Code. - NOTE: See the comment from ADTC for an "Easier GUI version of step 3...(Skip step 4)."If necessary, the Maven quick start archetype could be used to generate a new Maven project in an appropriate local folder:
mvn archetype:generate -DgroupId=
com.companyname.appname-DartifactId=
appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
. This will create an appnamefolder with Maven's Standard Directory Layout(i.e.src/main/java/com/companyname/appname
andsrc/main/test/com/companyname/appname
to begin with and a sample "Hello World!" Java file named appname.java
and associated unit test named appnameTest.java
).* - Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appnamefolder.
- Open the Command Palette (via the View menu or by right-clicking) and type in and select
Tasks: Configure task
then selectCreate tasks.json from template
. Choose maven ("Executes common Maven commands"). This creates a tasks.json file with "verify" and "test" tasks. More can be added corresponding to other Maven Build Lifecyclephases. To specifically address your requirement for classes to be built without a JAR file, a "compile" task would need to be added as follows:
{ "label": "compile", "type": "shell", "command": "mvn -B compile", "group": "build" },
Save the above changes and then open the Command Palette and select "Tasks: Run Build Task" then pick "compile" and then "Continue without scanning the task output". This invokes Maven, which creates a
target
folder at the same level as thesrc
folder with the compiled class files in thetarget\classes
folder.
- 下载 VS Code和Apache Maven并安装它们。
- 安装适用于 Java 的 Visual Studio 扩展包 - 例如,通过将此 URL 粘贴到 Web 浏览器中:
vscode:extension/vscjava.vscode-java-pack
然后在 VS Code 中打开后单击绿色的安装按钮。 - 注意:有关“步骤 3 的更简单 GUI 版本...(跳过步骤 4)”的信息,请参阅 ADTC 的评论。如有必要,可以使用 Maven 快速启动原型在适当的本地文件夹中生成新的 Maven 项目:
mvn archetype:generate -DgroupId=
com.companyname.appname-DartifactId=
appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
。这将创建一个应用程序的名字的文件夹与Maven的标准目录布局(即src/main/java/com/companyname/appname
与src/main/test/com/companyname/appname
用和样品开始的“Hello World!”的Java文件命名为应用程序的名字.java
和相关的命名单元测试应用程序的名字Test.java
)。* - 通过 File 菜单 -> Open Folder... 在 VS Code 中打开 Maven 项目文件夹,然后选择appname文件夹。
- 打开命令面板(通过视图菜单或右键单击)并输入并选择
Tasks: Configure task
然后选择Create tasks.json from template
。 选择 maven(“执行常见的 Maven 命令”)。这将创建一个包含“验证”和“测试”任务的 tasks.json 文件。可以根据其他Maven 构建生命周期阶段添加更多内容。为了专门解决在没有 JAR 文件的情况下构建类的要求,需要添加一个“编译”任务,如下所示:
{ "label": "compile", "type": "shell", "command": "mvn -B compile", "group": "build" },
保存上述更改,然后打开命令面板并选择“任务:运行构建任务”,然后选择“编译”,然后选择“继续而不扫描任务输出”。这将调用 Maven,它会创建一个
target
文件夹,该src
文件夹与文件夹中包含已编译类文件的target\classes
文件夹处于同一级别。
Addendum: How to run/debug a class
附录:如何运行/调试类
Following a question in the comments, here are some steps for running/debugging:-
在评论中提出问题后,以下是运行/调试的一些步骤:-
- Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
- Click on the green arrow in the Debug view and select "Java".
- Assuming it hasn't already been created, a message "launch.json is needed to start the debugger. Do you want to create it now?" will appear - select "Yes" and then select "Java" again.
- Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
- Click on the green arrow in the Debug view again.
- 如果尚未显示,则显示调试视图(通过视图菜单 - 调试或CtrlShiftD)。
- 单击调试视图中的绿色箭头并选择“Java”。
- 假设它还没有被创建,一条消息“启动调试器需要launch.json。你想现在创建它吗?” 将出现 - 选择“是”,然后再次选择“Java”。
- 在“mainClass”的值中输入主类的完全限定名称(例如com.companyname.appname.App)并保存文件。
- 再次单击 Debug 视图中的绿色箭头。
回答by 53c
An alternative way is to install the Maven for Java
plugin and create a maven project within Visual Studio. The steps are described in the official documentation:
另一种方法是安装Maven for Java
插件并在 Visual Studio 中创建一个 Maven 项目。官方文档中描述了这些步骤:
- From the Command Palette (Crtl+Shift+P), select Maven: Generate from Maven Archetype and follow the instructions, or
- Right-click on a folder and select Generate from Maven Archetype.
- 从命令面板 (Crtl+Shift+P) 中,选择 Maven: Generate from Maven Archetype 并按照说明进行操作,或
- 右键单击文件夹并选择从 Maven Archetype 生成。
回答by Chidozie Nnachor
This is not a particularly good answer as it explains how to run your java code n VS Code and not necessarily a Maven
project, but it worked for me because I could not get around to doing the manual configuration myself. I decided to use this method instead since it is easier and faster.
这不是一个特别好的答案,因为它解释了如何运行 Java 代码和 VS Code 而不一定是一个Maven
项目,但它对我有用,因为我无法自己进行手动配置。我决定改用这种方法,因为它更容易、更快捷。
Install VSCode (and for windows, set your environment variables), then install vscode:extension/vscjava.vscode-java-pack
as detailed above, and then install the code runnerextension pack, which basically sets up the whole process (in the background) as explained in the accepted answer above and then provides a play button to run your java code when you're ready.
安装 VSCode(对于 Windows,设置您的环境变量),然后vscode:extension/vscjava.vscode-java-pack
按照上面的详细说明安装,然后安装代码运行器扩展包,它基本上按照上面接受的答案中的说明设置了整个过程(在后台),然后提供一个播放按钮,用于在您准备好时运行您的 Java 代码。
This was all explained in this video.
这一切都在本视频中进行了解释。
Again, this is not the best solution, but if you want to cut to the chase, you may find this answer useful.
同样,这不是最佳解决方案,但如果您想切入正题,您可能会发现此答案很有用。
回答by Sundar Rajan
I surprise no one had mentioned this possible easy approach in visual studio code.
我很惊讶没有人在 Visual Studio 代码中提到这种可能的简单方法。
Install VS Code and Apache maven ( just as mentioned by @Steve Chambers)
安装 VS Code 和 Apache maven(正如@Steve Chambers 提到的)
After installing this extension vscode:extension/vscjava.vscode-java-pack
安装此扩展后 vscode:extension/vscjava.vscode-java-pack
In the java overview page , there is a an option which reads 'Create Maven Project' which further takes to a simple wizard to generate maven project.
在 java 概述页面中,有一个选项为“创建 Maven 项目”,该选项进一步采用简单的向导来生成 Maven 项目。
Its pretty quick which is intutitive enough, even newbies can very well start with a Maven project.
它非常快速且足够直观,即使是新手也可以很好地从 Maven 项目开始。