0x00 前言
没写过 Java,得配个带 Maven 的编码环境,不太明白,试试看顺便记录一下
0x01 配置过程
安装 jdk1.8 后,找到安装位置:
(base) dian@CD-Ali doraemon % /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.381.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_291 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
在 .zshrc 配置环境:
# java env
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH:.
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export JAVA_HOME
export PATH
export CLASSPATH
下载 Maven:
在 Index of /dist/maven/maven-3 里找到所需的 Maven 下载使用,
由于 jdk 版本常年是 1.8.0,这里选择了 3.6.0 的较老对应版本下载:
- download: apache-maven-3.6.0-bin.zip
- file: 📎apache-maven-3.6.0-bin.zip
解压在自己好找的路径里,比如我放在了 home 目录下,
在公司工作的话,也需要顺便把手手相传的用户自定义设置文件,放在这个目录里:
(base) dian@CD-Ali apache-maven-3.6.0 % pwd
/Users/dian/apache-maven-3.6.0
0x02 VSCode 中 Maven for Java 配置
在 VSCode 的扩展商店里安装 Maven for Java,打开"扩展设置"
在扩展设置的 Path 里填入 maven 路径和 settings file 路径,将使用 JAVA_HOME 的勾打上:
上述在扩展设置中的三步,也等效于直接在 VSCode 的 settings.json 中加入这三行:
之后,在 git 项目下(此处为 gul/doraemon)找到 pom.xml,右键该文件调出菜单栏
菜单栏里会有一个 "显示 Effective POM",点击后将开始自动下载构建,
经过长长的 Downloading / Downloaded 列表之后,最终出现 BUILD SUCCESS,即环境搭建完成。
bash
Spawn {"command":"\"/Users/dian/apache-maven-3.6.0/bin/mvn\"","args":["-s","\"/Users/dian/apache-maven-3.6.0/settings.xml\"","-B","-Doutput=\"/Users/dian/Library/Application Support/Code/User/workspaceStorage/e938a1dccd1b83f57865abcc26501355/vscjava.vscode-maven/f8ceb7d138c4f5e7c5ab7995a1292267.epom\"","help:effective-pom","-f","\"/Users/dian/Github/doraemon/doraemon-solution/pom.xml\""]}
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.xxx.gul:doraemon-solution:jar:1.0.2-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ com.xxx.gul:doraemon:1.0.2-SNAPSHOT, /Users/dian/Github/doraemon/pom.xml, line 340, column 21
[WARNING] The expression ${parent.version} is deprecated. Please use ${project.parent.version} instead.
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] Downloading from tbmirror: http://repo.xxx-inc.com/mvn/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom
[INFO] Downloaded from tbmirror: http://repo.xxx-inc.com/mvn/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom (10 kB at 33 kB/s)
......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.584 s
[INFO] Finished at: 2023-08-15T14:30:36+08:00
[INFO] ------------------------------------------------------------------------
0xFF 权限安装问题
安装过程中,可能会出现这种由于无法验证开发者不给安装的情况:
此时打开 Mac 的系统设置中,隐私与安全性的标签页,向下拉动,
在安全性这里会出现这个 libjansi.jnilib 是否允许安装,选择"打开",这一步就可以正常安装了。
完成后,可以新建或打开 Maven 项目
为某个项目安装依赖(该项目下有 pom.xml):
安装完成后,在资源管理器下面会出现 MAVEN 窗口(如图)
右键需要安装的项目,选择 "Run Maven Commands",先点选 clean ,再点选 install,就会开始根据 settings.xml 里的配置进行安装: