[英]How to create a Java / Maven project that works in Visual Studio Code?
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.我正在尝试与 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 中编辑 java 文件,并在保存编译器时将 .class 文件保存在适当的输出文件夹中。
That's all - no war or jar files.这就是全部 - 没有战争或 jar 文件。
Any help?有什么帮助吗?
Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-这是一个完整的步骤列表 - 您可能不需要步骤 1-3,但为了完整起见,我将它们包括在内:-
vscode:extension/vscjava.vscode-java-pack
and then clicking on the green Install button after it opens in VS Code.vscode:extension/vscjava.vscode-java-pack
,然后在它在 VS Code 中打开后单击绿色的安装按钮。mvn archetype:generate -DgroupId=
com.companyname.appname -DartifactId=
appname -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
.mvn archetype:generate -DgroupId=
com.companyname.appname -DartifactId=
appname -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
。 This will create an appname folder with Maven's Standard Directory Layout (ie src/main/java/com/companyname/appname
and src/main/test/com/companyname/appname
to begin with and a sample "Hello World!" Java file named appname .java
and associated unit test named appname Test.java
).*src/main/java/com/companyname/appname
和src/main/test/com/companyname/appname
开头,以及一个名为“Hello World!”的示例 Java 文件appname .java
和关联的单元测试名为appname Test.java
).*Tasks: Configure task
then select Create tasks.json from template
.Tasks: Configure task
然后选择Create tasks.json from template
。 Choose maven ("Executes common Maven commands").选择 maven(“执行常见的 Maven 命令”)。 This creates a tasks.json file with "verify" and "test" tasks.
这将创建一个包含“验证”和“测试”任务的 tasks.json 文件。 More can be added corresponding to other Maven Build Lifecycle phases.
可以根据其他Maven 构建生命周期阶段添加更多内容。 To specifically address your requirement for classes to be built without a JAR file, a "compile" task would need to be added as follows:
为了专门解决在没有 JAR 文件的情况下构建类的要求,需要添加一个“编译”任务,如下所示:
{ "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 the src
folder with the compiled class files in the target\\classes
folder.这将调用 Maven,它会在
target\\classes
文件夹中创建一个与src
文件夹相同级别的target
文件夹,其中包含已编译的类文件。
Addendum: How to run/debug a class附录:如何运行/调试类
Following a question in the comments, here are some steps for running/debugging:-在评论中提出问题后,以下是运行/调试的一些步骤:-
An alternative way is to install the Maven for Java
plugin and create a maven project within Visual Studio.另一种方法是安装
Maven for Java
插件并在 Visual Studio 中创建一个 Maven 项目。 The steps are described in the official documentation : 官方文档中描述了这些步骤:
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.这不是一个特别好的答案,因为它解释了如何运行 Java 代码和 VS Code 而不一定是
Maven
项目,但它对我Maven
,因为我无法自己进行手动配置。 I decided to use this method instead since it is easier and faster.我决定改用这种方法,因为它更容易、更快捷。
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 runner extension 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
如上所述,然后安装code runner扩展包,它基本上设置了整个过程(在后台) 如上面接受的答案中所述,然后提供一个播放按钮来在您准备好时运行您的 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.同样,这不是最佳解决方案,但如果您想切入正题,您可能会发现此答案很有用。
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 项目开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.