简体   繁体   English

如何创建可在 Visual Studio Code 中运行的 Java/Maven 项目?

[英]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,但为了完整起见,我将它们包括在内:-

  1. Download VS Code and Apache Maven and install both.下载 VS CodeApache Maven并安装它们。
  2. Install the Visual Studio extension pack for Java - eg 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.安装适用于 Java 的 Visual Studio 扩展包 - 例如,通过将此 URL 粘贴到 Web 浏览器中: vscode:extension/vscjava.vscode-java-pack ,然后在它在 VS Code 中打开后单击绿色的安装按钮。
  3. NOTE: See the comment from ADTC for an "Easier GUI version of step 3...(Skip step 4)."注意:有关“步骤 3 的更简单 GUI 版本...(跳过步骤 4)”的信息,请参阅 ADTC 的评论。 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 .如有必要,可以使用 Maven 快速启动原型在适当的本地文件夹中生成新的 Maven 项目: 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 ).*这将创建一个具有 Maven 标准目录布局appname文件夹(即src/main/java/com/companyname/appnamesrc/main/test/com/companyname/appname开头,以及一个名为“Hello World!”的示例 Java 文件appname .java和关联的单元测试名为appname Test.java ).*
  4. Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.通过 File 菜单 -> Open Folder... 在 VS Code 中打开 Maven 项目文件夹,然后选择appname文件夹。
  5. Open the Command Palette (via the View menu or by right-clicking) and type in and select Tasks: Configure task then select Create tasks.json from template .打开命令面板(通过视图菜单或右键单击)并输入并选择Tasks: Configure task然后选择Create tasks.json from template
  6. 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" },
  7. 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:-在评论中提出问题后,以下是运行/调试的一些步骤:-

  1. Show the Debug view if it is not already shown (via View menu - Debug or Ctrl Shift D ).如果尚未显示,则显示 Debug 视图(通过 View 菜单 - Debug 或Ctrl Shift D )。
  2. Click on the green arrow in the Debug view and select "Java".单击调试视图中的绿色箭头并选择“Java”。
  3. Assuming it hasn't already been created, a message "launch.json is needed to start the debugger. Do you want to create it now?"假设它还没有被创建,一条消息“启动调试器需要launch.json。你想现在创建它吗?” will appear - select "Yes" and then select "Java" again.将出现 - 选择“是”,然后再次选择“Java”。
  4. Enter the fully qualified name of the main class (eg com.companyname.appname.App ) in the value for "mainClass" and save the file.在“mainClass”的值中输入主类的完全限定名称(例如com.companyname.appname.App )并保存文件。
  5. Click on the green arrow in the Debug view again.再次单击 Debug 视图中的绿色箭头。

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 : 官方文档中描述了这些步骤

  1. From the Command Palette (Crtl+Shift+P), select Maven: Generate from Maven Archetype and follow the instructions, or从命令面板 (Crtl+Shift+P) 中,选择 Maven: Generate from Maven Archetype 并按照说明进行操作,或
  2. Right-click on a folder and select Generate from Maven Archetype.右键单击文件夹并选择从 Maven Archetype 生成。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM