文章目录
Mac 从 0 到 1 保姆级配置教程目录,点击即可跳转对应文章:
【🚀🚀🚀 教程开源地址】:https://github.com/tonngw/macman
【Mac 从 0 到 1 保姆级配置教程 00】 - 教程说明
【Mac 从 0 到 1 保姆级配置教程 01】 - 安装无敌的 brew
【Mac 从 0 到 1 保姆级配置教程 02】 - 你真的会装软件吗?内含常用软件安装教程
【Mac 从 0 到 1 保姆级配置教程 03】 - 安装配置美化 iterm2 一气呵成,Mac 下经久不衰的终端
【Mac 从 0 到 1 保姆级配置教程 04】 - 手把手教你安装配置 zsh 和 oh my zsh,一看就会!
【Mac 从 0 到 1 保姆级配置教程 05】 - 全网最详细 20+ 张图逐帧安装 powerlevel10k zsh 主题(上)
【Mac 从 0 到 1 保姆级配置教程 05】 - 全网最详细 20+ 张图逐帧安装 powerlevel10k zsh 主题(下)
【Mac 从 0 到 1 保姆级配置教程 06】 - iterm2 常用快捷键和 zsh 别名配置
【Mac 从 0 到 1 保姆级配置教程 07】 - 配置 Git、GitHub、SSH 免密提交以及 Git 常用基础命令
【Mac 从 0 到 1 保姆级配置教程 08】 - 快速配置 Neovim、LazyVim 以及常用开发环境,如果之前有人这么写就好了
【Mac 从 0 到 1 保姆级配置教程 09】 - 快速配置终端复用工具 tmux 和 oh-my-tmux
【Mac 从 0 到 1 保姆级配置教程 10】 - sdkman 几分钟搞定 Java 开发环境,一定不要错过啊!
【Mac 从 0 到 1 保姆级配置教程 11】 - Mac 基础配置 Finder、触控板、常用快捷键等
【Mac 从 0 到 1 保姆级配置教程 12】- 安装配置万能的编辑器 VSCode 以及常用插件
【Mac 从 0 到 1 保姆级配置教程 13】- 详细介绍一款 Mac 下无敌好用的宝藏软件 RayCast
【Mac 从 0 到 1 保姆级配置教程 14】- 那些 Mac 非常常用的高效率工具,肯定有你不知道的
【Mac 从 0 到 1 保姆级配置教程 15】- Python 环境一键安装与配置,就是这么的丝滑
持续更新中…
前言
如果你之前没有接触过 sdkman 的话,那么一定会好奇 sdkman 是啥,我为什么要用它来配置 Java 环境?
sdkman 简介
SDKMAN (Software Development Kit Manager) 是一个用于管理多个软件开发工具包版本的工具。它提供了一个方便的命令行界面,用于安装、切换、删除和管理多个版本的 SDK。
主要特性:
- 多版本管理:可以同时安装多个版本的 Java、Maven、Gradle 等工具,并在它们之间轻松切换
- 简单的命令行接口:所有操作都通过简单的
sdk
命令完成,不需要记忆复杂的语法 - 跨平台支持:支持 macOS、Linux 和 Windows (通过 WSL),提供一致的使用体验
- 自动环境配置:安装后自动配置相关环境变量,无需手动编辑配置文件
- 广泛的工具支持:除了 Java,还支持 Kotlin、Scala、Groovy、Gradle、Maven、Spring Boot 等几十种 JVM 生态工具
为什么选择 SDKMAN:
- 简化开发环境配置:一条命令即可安装和切换开发工具版本
- 避免环境冲突:不同项目可以使用不同版本的 JDK 和构建工具,互不干扰
- 流畅升级体验:新版本发布后可以轻松升级,同时保留旧版本作为备份
- 标准化团队环境:团队成员可以使用相同版本的工具,减少"在我机器上能运行"的问题
总结一句话就是方便,快捷,高效。
安装 sdkman
- 安装 sdkman
curl -s "https://get.sdkman.io" | bash
- 打开一个新的终端窗口,初始化 sdkman
source "$HOME/.sdkman/bin/sdkman-init.sh"
- 查看版本,出现以下内容说明安装成功了。
sdk version
安装 Java 环境
- sdkman 查看可以安装的所有 java 版本
sdk list java
- 这里以 java8 为例进行安装
sdk install java 8.0.402-amzn
- 查看当前生效的 JDK
sdk current java
- 查看 Java 版本
java -version
到这里 Java 环境就配置好了,不需要你配置环境变量。
安装 Maven 环境
- 查看可以安装的 Maven 的版本
sdk list maven
- 安装 3.9.6 版本
sdk install maven 3.9.6
- 查看 maven 版本
mvn -v
到这里 maven 也装好了。
配置 Maven
主要配置一下 maven 的包下载源和依赖下载位置
maven 安装成功之后会告诉你 Maven Home,在 Maven home: /Users/tonngw/.sdkman/candidates/maven/current
进入这个位置 /Users/tonngw/.sdkman/candidates/maven/current/conf ,打开 settings.xml 文件进行配置即可。
找到 conf/settings.xml 文件,配置阿里云镜像源
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
<localRepository>/Users/tonngw/Develop/mvnRepository</localRepository>
完整配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>/Users/tonngw/Develop/mvnRepository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- TODO Since when can proxies be selected as depicted? -->
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<!-- <mirror> -->
<!-- <id>maven-default-http-blocker</id> -->
<!-- <mirrorOf>external:http:*</mirrorOf> -->
<!-- <name>Pseudo repository to mirror external repositories initially using HTTP.</name> -->
<!-- <url>http://0.0.0.0/</url> -->
<!-- <blocked>true</blocked> -->
<!-- </mirror> -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the property 'target-env' with a value of 'dev', which
| provides a specific path to the Tomcat instance. To use this, your plugin configuration might
| hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
安装 jetbrains-toolbox
jetbrains 家的 IDE 工具管理器,可以很方便的下载和管理 IDE。
brew install --cask jetbrains-toolbox
安装完成之后打开
直接通过 toolbox 安装 IDEA
配置 IDEA
基础配置
Java 路径
Maven 路径
Command + Shift + . 打开隐藏文件
进入 .sdkman/
将 candidates 目录拖到左侧边栏,方便进入。
配置好 maven 的安装路径和配置文件路径
新项目默认配置
Maven 配置
Java 配置
有了默认配置之后以后新建项目,环境配置就会默认配好了,不需要我们每次都去手动设置环境。
常用插件
- Maven Helper:Maven 包冲突检测
- GenerateAllSetter:快速生成 set 语句
- Key Promoter X:提示快捷键的
- MybastisX:mapper 助手
- lombok
- GitToolBox:Git 工具箱
可以通过 Jetbrains 账户直接同步过来
原来这里应该是同步配置
全部搞定!
小技巧
我们可以在控制台使用 idea projectName
来快速打开一个项目。
如果你有安装多个 IDEA 版本(比如社区版和旗舰版),那么就需要在终端配置文件中配置一下 IDEA 的路径,来指定具体使用哪个 IDEA 版本来打开项目。
export PATH="/Users/tonngw/Applications/IntelliJ IDEA.app/Contents/MacOS/idea:$PATH"
验证
随便找一个 Java 和 Maven 的项目,可以跑起来,说明我们的环境没有问题。
GitHub Repo 地址:https://github.com/sauce-code/cuckoo
最后
用 sdkman 配置 Java 环境简直不要太爽,真的分分钟搞定,可以把它做一个脚本,一键执行就能搞定!
如果感觉文章对你有所帮助,请三连支持一下博主哦,感谢大家~
最重要的是给博主留一个大大的关注,你的支持是我更新的最大动力!
这个系列会继续更新下去的!
参考资料
系列教程
【🚀🚀🚀 教程开源地址】:https://github.com/tonngw/macman
【Mac 从 0 到 1 保姆级配置教程 00】 - 教程说明
【Mac 从 0 到 1 保姆级配置教程 01】 - 安装无敌的 brew
【Mac 从 0 到 1 保姆级配置教程 02】 - 你真的会装软件吗?内含常用软件安装教程
【Mac 从 0 到 1 保姆级配置教程 03】 - 安装配置美化 iterm2 一气呵成,Mac 下经久不衰的终端
【Mac 从 0 到 1 保姆级配置教程 04】 - 手把手教你安装配置 zsh 和 oh my zsh,一看就会!
【Mac 从 0 到 1 保姆级配置教程 05】 - 全网最详细 20+ 张图逐帧安装 powerlevel10k zsh 主题(上)
【Mac 从 0 到 1 保姆级配置教程 05】 - 全网最详细 20+ 张图逐帧安装 powerlevel10k zsh 主题(下)
【Mac 从 0 到 1 保姆级配置教程 06】 - iterm2 常用快捷键和 zsh 别名配置
【Mac 从 0 到 1 保姆级配置教程 07】 - 配置 Git、GitHub、SSH 免密提交以及 Git 常用基础命令
【Mac 从 0 到 1 保姆级配置教程 08】 - 快速配置 Neovim、LazyVim 以及常用开发环境,如果之前有人这么写就好了
【Mac 从 0 到 1 保姆级配置教程 09】 - 快速配置终端复用工具 tmux 和 oh-my-tmux
【Mac 从 0 到 1 保姆级配置教程 10】 - sdkman 几分钟搞定 Java 开发环境,一定不要错过啊!
【Mac 从 0 到 1 保姆级配置教程 11】 - Mac 基础配置 Finder、触控板、常用快捷键等
【Mac 从 0 到 1 保姆级配置教程 12】- 安装配置万能的编辑器 VSCode 以及常用插件
【Mac 从 0 到 1 保姆级配置教程 13】- 详细介绍一款 Mac 下无敌好用的宝藏软件 RayCast
【Mac 从 0 到 1 保姆级配置教程 14】- 那些 Mac 非常常用的高效率工具,肯定有你不知道的
【Mac 从 0 到 1 保姆级配置教程 15】- Python 环境一键安装与配置,就是这么的丝滑
持续更新中…