如何创建可在 Visual Studio Code 中运行的 Java/Maven 项目? [英] How to create a Java / Maven project that works in Visual Studio Code?

查看:261
本文介绍了如何创建可在 Visual Studio Code 中运行的 Java/Maven 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 maven 项目 - 以便我可以在根文件夹中编译 Java 文件并在另一个文件夹中输出类文件.

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.

我已经下载了 mvn.

I've already downloaded mvn.

我正在尝试与 VS Code 集成.我的目标是在 VS Code 中编辑 java 文件,并在保存编译器时将 .class 文件保存在适当的输出文件夹中.

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.

仅此而已 - 没有战争或 jar 文件.

That's all - no war or jar files.

有什么帮助吗?

推荐答案

这是一个完整的步骤列表 - 您可能不需要步骤 1-3,但为了完整起见,我将它们包括在内:-

Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-

  1. 下载 VS CodeApache Maven 并安装两者.
  2. 安装适用于 Java 的 Visual Studio 扩展包 - 例如将此 URL 粘贴到 Web 浏览器中:vscode:extension/vscjava.vscode-java-pack,然后在 VS Code 中打开后单击绿色的安装按钮.
  3. 注意:请参阅 ADTC 的评论,了解第 3 步的更简单 GUI 版本...(跳过第 4 步)." 如有必要,可使用 Maven 快速启动原型生成一个在适当的本地文件夹中新建 Maven 项目:mvn archetype:generate -DgroupId=com.companyname.appname-DartifactId=appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false.这将使用 Maven 的 标准目录布局(即 src/main/java/com/companyname/appnamesrc/main/test/com/companyname/appname 开头和一个名为 appname.java 的示例Hello World!"Java 文件和名为的关联单元测试appnameTest.java).*
  4. 在 VS Code 中通过 File 菜单打开 Maven 项目文件夹 -> Open Folder... 并选择 appname 文件夹.
  5. 打开命令面板(通过视图菜单或右键单击)并输入并选择Tasks:Configure task,然后选择Create tasks.json from template.
  6. 选择 maven(执行常见的 Maven 命令").这将创建一个带有验证"和测试"任务的 tasks.json 文件.可以添加更多对应于其他 Maven Build Lifecycle 阶段.为了专门满足您在没有 JAR 文件的情况下构建类的要求,需要添加一个编译"任务,如下所示:

  1. Download VS Code and Apache Maven and install both.
  2. 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.
  3. 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 appname folder with Maven's Standard Directory Layout (i.e. 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 appnameTest.java).*
  4. Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
  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.
  6. 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 Lifecycle phases. 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"
},

  • 保存上述更改,然后打开命令面板并选择任务:运行构建任务",然后选择编译",然后选择继续而不扫描任务输出".这将调用 Maven,它会在 src 文件夹的同一级别创建一个 target 文件夹,其中包含 targetclasses 文件夹中的已编译类文件.

  • 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 targetclasses folder.

    <小时>

    附录:如何运行/调试类

    根据评论中的问题,以下是运行/调试的一些步骤:-

    Following a question in the comments, here are some steps for running/debugging:-

    1. 如果尚未显示调试视图,则显示它(通过视图菜单 - 调试或 CtrlShiftD).
    2. 单击调试"视图中的绿色箭头并选择Java".
    3. 假设它尚未创建,则会显示一条消息启动调试器需要launch.json.您想现在创建它吗?"将出现 - 选择是",然后再次选择Java".
    4. 在mainClass"的值中输入主类的完全限定名称(例如 com.companyname.appname.App)并保存文件.莉>
    5. 再次单击调试"视图中的绿色箭头.

    这篇关于如何创建可在 Visual Studio Code 中运行的 Java/Maven 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆