【Java专题】开发环境准备

本篇主要记录 jdk / jenv / maven 的安装和配置。

开发机

macbook m1 pro 26.3.1 (25D2128)

下载开发工具

开发工具使用经典的 IntelliJ IDEA
下载地址:https://www.jetbrains.com/zh-cn/idea/download/?section=mac

下载java jdk

本项目中使用的 java jdk 版本为 11

1
2
3
4
5
6
7
8
9
# 下载 java 11
brew install openjdk@11

# 链接以便暴露给 java_home(如果已安装其他版本的openjdk则新安装的需要手动链接)
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

# 验证 java 版本
java --version
# javac 11.0.30

下载 java 版本管理工具 jenv

jEnv是一个用于管理和切换多个Java版本的命令行工具。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
brew install jenv

# 配置环境变量
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

# 启用 jenv 插件
eval "$(jenv init -)"
jenv enable-plugin export

jenv -h
# jenv 0.6.0-20-ga771525
# Usage: jenv <command> [<args>]

# Some useful jenv commands are:

# Management commands:
# add Add JDK into jenv. By default aliases will be generated by parsing "java -# version"
# global Set or show the global Java version
# local Set or show the local application-specific Java version
# remove Remove JDK installations
# shell Set or show the shell-specific Java version

# Informational commands:
# commands List all available jenv commands
# version Show the current Java version and its origin
# versions List all Java versions available to jenv
# whence List all Java versions that contain the given executable
# which Display the full path to an executable

# Utility commands:
# doctor Show information about which command will be executed
# rehash Rehash jenv shims (run this after installing executables)
# with Execute command with specific Java version

# See `jenv help <command>' for information on a specific command.
# For full documentation, see: https://github.com/jenv/jenv/blob/master/README.md
  • 可以管理多个 JDK 版本(需要先手动安装对应版本jdk) -> jenv add xxx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
      # 通过 macOS 的 Java 管理工具查找已安装jdk
    /usr/libexec/java_home -V
    # 11.0.30 (arm64) "Homebrew" - "OpenJDK 11.0.30" /opt/homebrew/Cellar/openjdk@11/11.0.30/libexec/openjdk.jdk/Contents/Home # 新增
    # 1.8.0_372 (arm64) "Amazon" - "Amazon Corretto 8" /Users/liyanyan/Library/Java/JavaVirtualMachines/corretto-1.8.0_372/Contents/Home
    # /opt/homebrew/Cellar/openjdk@11/11.0.30/libexec/openjdk.jdk/Contents/Home

    # 查询当前jenv管理的版本
    jenv versions
    # * system (set by /Users/liyanyan/.jenv/version)

    # 添加 java 11
    jenv add /opt/homebrew/Cellar/openjdk@11/11.0.30/libexec/openjdk.jdk/Contents/Home # 上文brew安装地址
    # openjdk64-11.0.30 added
    # 11.0.30 added
    # 11.0 added
    # 11 added

    # 验证
    jenv versions
    # * system (set by /Users/liyanyan/.jenv/version)
    # 11
    # 11.0
    # 11.0.30
    # openjdk64-11.0.30
  • 在不同项目间自动切换 Java 版本(通过.java-version文件)

    1
    2
    # 只改本项目版本
    jenv local 11
  • 全局设置默认 Java 版本

    1
    2
    # 设置全局版本
    jenv global 1.8

下载 java 包管理器 maven

1
2
# 下载安装
brew install maven

附:brew 换源

遇到了安装包特别慢的问题,换源步骤如下

1
2
3
4
5
6
7
8
9
10
11
# 1. 换源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 如果报错可略过
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 2. 设置二进制包镜像
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

# 3. 更新
brew update

引用链接