如何创建在Visual Studio代码中工作的Java / Maven项目? [英] How to create a Java / Maven project that works in Visual Studio Code?

查看:224
本文介绍了如何创建在Visual Studio代码中工作的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.

这就是全部 - 没有war或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代码 Apache Maven 并安装两者。

  2. 安装适用于Java的Visual Studio扩展包 - 例如将此URL粘贴到Web浏览器中: vscode:extension / vscjava.vscode-java-pack 然后在VS代码中打开后单击绿色的安装按钮。

  3. 如有必要,可以使用Maven快速启动原型在适当的本地文件夹中生成新的Maven项目: mvn archetype:generate -DgroupId = com.companyname.appname -DartifactId = 应用程序名称 -DarchetypeArtifactId = maven-archetype-quickstart -DinteractiveMode = false 。这将创建一个 appname 文件夹,其中包含Maven的标准目录布局(即 src / main / java / com / companyname / appname src / main / test / com / companyname / appname 以及名为 appname <$的示例Hello World!Java文件开头c $ c> .java 以及名为 appname <$ strong> Test.java 的关联单元测试。

  4. 通过文件菜单 - >打开文件夹...在VS Code中打开Maven项目文件夹,然后选择 appname 文件夹。

  5. 打开Command Palette(通过View菜单或右键单击)并输入并选择 Tasks:Configure task 然后选择从模板创建tasks.json

  6. 选择maven(执行常见的Maven命令)。这将创建一个带有verify和test任务的tasks.json文件。可以添加更多相应的其他 Maven构建生命周期阶段。要专门解决您在没有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. 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\classes 文件夹。

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

    更新(占位符):如何运行/调试类

    在注释中的问题之后,以下是运行/调试的一些步骤:

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


    1. 显示Debug视图(如果尚未显示)(通过View菜单 - Debug或 Ctrl Shift D )。

    2. 单击Debug视图中的绿色箭头并选择Java。

    3. 假设它尚未创建消息启动调试器需要launch.json。您要立即创建吗?将出现 - 选择是,然后再次选择Java。

    4. 输入主类的完全限定名称(例如 com.companyname.appname.App )在mainClass的值中并保存文件。

    5. 再次单击Debug视图中的绿色箭头。

    1. Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
    2. Click on the green arrow in the Debug view and select "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?" will appear - select "Yes" and then select "Java" again.
    4. Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
    5. Click on the green arrow in the Debug view again.

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

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