如何创建在Visual Studio代码中工作的Java / Maven项目?

时间:2023-01-17 10:32:37

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?

有帮助吗?

1 个解决方案

#1


27  

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.
  2. 下载VS Code和Apache Maven并安装它们。
  3. 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.
  4. 安装适用于Java的Visual Studio扩展包 - 例如将此URL粘贴到Web浏览器中:vscode:extension / vscjava.vscode-java-pack,然后在VS Code中打开后单击绿色的Install按钮。
  5. 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).
  6. 如有必要,可以使用Maven快速启动原型在适当的本地文件夹中生成新的Maven项目:mvn archetype:generate -DgroupId = com.companyname.appname-DartifactId = appname-DarchetypeArtifactId = maven-archetype-quickstart -DinteractiveMode =假。这将创建一个带有Maven标准目录布局的appname文件夹(即src / main / java / com / companyname / appname和src / main / test / com / companyname / appname以及一个名为“Hello World!”的示例Java文件appname.java和关联的单元测试名为appnameTest.java)。
  7. Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
  8. 通过“文件”菜单 - >“打开文件夹...”在VS Code中打开Maven项目文件夹,然后选择appname文件夹。
  9. 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.
  10. 打开命令选项板(通过“视图”菜单或右键单击)并键入并选择“任务:配置任务”,然后从模板中选择“创建tasks.json”。
  11. 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:

    选择maven(“执行常见的Maven命令”)。这将创建一个带有“verify”和“test”任务的tasks.json文件。可以添加更多对应于其他Maven构建生命周期阶段的内容。要专门解决您在没有JAR文件的情况下构建类的要求,需要添加“编译”任务,如下所示:

    {
        "label": "compile",
        "type": "shell",
        "command": "mvn -B compile",
        "group": "build"
    },
    
  12. 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,它会在与src文件夹相同的级别创建一个目标文件夹,并在target \ classes文件夹中创建已编译的类文件。

UPDATE (placeholder): How to run/debug a class

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

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 CtrlShiftD).
  2. 显示Debug视图(如果尚未显示)(通过View菜单 - Debug或CtrlShiftD)。
  3. Click on the green arrow in the Debug view and select "Java".
  4. 单击Debug视图中的绿色箭头,然后选择“Java”。
  5. 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.
  6. 假设尚未创建它,则需要一条消息“launch.json来启动调试器。你想现在创建吗?”将出现 - 选择“是”,然后再次选择“Java”。
  7. Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
  8. 在“mainClass”的值中输入主类的完全限定名称(例如com.companyname.appname.App)并保存文件。
  9. Click on the green arrow in the Debug view again.
  10. 再次单击Debug视图中的绿色箭头。

#1


27  

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.
  2. 下载VS Code和Apache Maven并安装它们。
  3. 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.
  4. 安装适用于Java的Visual Studio扩展包 - 例如将此URL粘贴到Web浏览器中:vscode:extension / vscjava.vscode-java-pack,然后在VS Code中打开后单击绿色的Install按钮。
  5. 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).
  6. 如有必要,可以使用Maven快速启动原型在适当的本地文件夹中生成新的Maven项目:mvn archetype:generate -DgroupId = com.companyname.appname-DartifactId = appname-DarchetypeArtifactId = maven-archetype-quickstart -DinteractiveMode =假。这将创建一个带有Maven标准目录布局的appname文件夹(即src / main / java / com / companyname / appname和src / main / test / com / companyname / appname以及一个名为“Hello World!”的示例Java文件appname.java和关联的单元测试名为appnameTest.java)。
  7. Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
  8. 通过“文件”菜单 - >“打开文件夹...”在VS Code中打开Maven项目文件夹,然后选择appname文件夹。
  9. 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.
  10. 打开命令选项板(通过“视图”菜单或右键单击)并键入并选择“任务:配置任务”,然后从模板中选择“创建tasks.json”。
  11. 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:

    选择maven(“执行常见的Maven命令”)。这将创建一个带有“verify”和“test”任务的tasks.json文件。可以添加更多对应于其他Maven构建生命周期阶段的内容。要专门解决您在没有JAR文件的情况下构建类的要求,需要添加“编译”任务,如下所示:

    {
        "label": "compile",
        "type": "shell",
        "command": "mvn -B compile",
        "group": "build"
    },
    
  12. 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,它会在与src文件夹相同的级别创建一个目标文件夹,并在target \ classes文件夹中创建已编译的类文件。

UPDATE (placeholder): How to run/debug a class

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

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 CtrlShiftD).
  2. 显示Debug视图(如果尚未显示)(通过View菜单 - Debug或CtrlShiftD)。
  3. Click on the green arrow in the Debug view and select "Java".
  4. 单击Debug视图中的绿色箭头,然后选择“Java”。
  5. 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.
  6. 假设尚未创建它,则需要一条消息“launch.json来启动调试器。你想现在创建吗?”将出现 - 选择“是”,然后再次选择“Java”。
  7. Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
  8. 在“mainClass”的值中输入主类的完全限定名称(例如com.companyname.appname.App)并保存文件。
  9. Click on the green arrow in the Debug view again.
  10. 再次单击Debug视图中的绿色箭头。