Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS Code 创建 Maven 项目 #148

Open
Qingquan-Li opened this issue Mar 11, 2020 · 1 comment
Open

VS Code 创建 Maven 项目 #148

Qingquan-Li opened this issue Mar 11, 2020 · 1 comment
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Mar 11, 2020

环境:

  • macOS (10.12.6)
  • VS Code (1.43.0)
  • Java 8
  • Maven 3.6.2


在使用 VS Code 创建 Maven 项目之前,需要先安装 VS Code完成 Java 安装和环境配置


一、安装 Maven

Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。

——维基百科

这里使用 Homebrew 安装 Maven ,也可以从官网下载安装:https://maven.apache.org

$ brew install maven # 安装最新版 Maven
$ mvn -version # 查看 Maven 版本

maven-install


二、VS Code 安装 Java 拓展插件

在 VS Code 上安装 Java 拓展插件包:Java Extension Pack (可以不装这个插件包,只安装 Language Support for Java(TM) by Red Hat ,但是不建议这样做),安装该插件包会包含以下 4 个拓展插件:

  1. Language Support for Java(TM) by Red Hat (运行 Java 代码插件,其他插件都可不装这个必装)
  2. Debugger for Java (用作调试)
  3. Java Test Runner (用作单元测试)
  4. Maven for Java ( Maven 是在 Java 环境下构建应用程序的软件)

除此之外,还建议安装以下拓展插件:

  • Java Dependency Viewer (Manage Java Dependencies(依赖) in VSCode)
  • Spring Boot Extension Pack(A collection of extensions for developing Spring Boot applications),现代 Java 项目少不了 Spring 框架,所以建议安装这个拓展包。该拓展包含了 Spring Initializr Java Support(用于生成快速启动的Spring Boot Java项目)在内的几个插件。

三、创建 Maven 项目

参考:bilibili 视频 - (Windows 10)通过visual studio code 创建maven项目并使用junit进行测试


vscode-cmd-panel



vscode-cmd-panel-maven-start01



vscode-cmd-panel-maven-start02



vscode-cmd-panel-maven-start03



vscode-cmd-panel-maven-start04



vscode-maven-creating

这一步建议使用“梯子”加速,不然可能会卡在"Generating project in Interactive mode"。
关于 groupId 和 artifactId 的命名,参考:Maven项目命名规范

  • groupId 在这里命名为 cn.marchone ,在 .java 文件顶部就要这样写:package cn.marchone;
  • artifactId 命名后会生成一个文件夹,当前创建的项目(模块)都会放在这个文件夹中。推荐“项目名称-模块名称”,在这里直接使用项目名称 maven-test 进行命名。
  • 之后的其他选项,如果不需要特别设置,回车即可。


vscode-maven-project

如果 VS Code 终端中输出以下的类似问题:

Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace that are strictly compatible with this environment.
The compiler compliance specified is 1.5 but a JRE 1.8 is used

需要在 pom.xml 中添加下面 2 行代码指定版本:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 添加指定版本 -->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
@PGokc
Copy link

PGokc commented Jul 23, 2023

同样的操作, 这样的问题是什么原因呢?
最初我以为是Maven版本太高插件不兼容的问题, 后面降到了低版本还是不行;
以下为报错信息:

  • 正在执行任务: "/Library/apache-maven-3.8.4/bin/mvn.cmd" org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate -DarchetypeArtifactId="maven-archetype-quickstart" -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeVersion="1.1" -DgroupId="com.example" -DartifactId="demo"

/Library/apache-maven-3.8.4/bin/mvn.cmd: line 1: syntax error near unexpected token (' /Library/apache-maven-3.8.4/bin/mvn.cmd: line 1: @Rem Licensed to the Apache Software Foun'ation (ASF) under one

  • 终端进程“/bin/zsh '-l', '-c', '"/Library/apache-maven-3.8.4/bin/mvn.cmd" org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate -DarchetypeArtifactId="maven-archetype-quickstart" -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeVersion="1.1" -DgroupId="com.example" -DartifactId="demo"'”启动失败(退出代码: 2)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants