Shell 脚本切换 Maven 的 settings.xml

前言

在公司都用公司内部的 Maven 私服,在家只能用公网的 Maven 仓库,在家和在公司的配置又都不一样。虽然可以通过修改 setting.xml 中 activeProfile 来设置,或者通过配置多个仓库来解决问题。不过为了清晰起见,我还是喜欢将配置分开来。

但我这人又懒,每次打开隐藏目录去切换设置又不现实。为了方便,就写个脚本来切换吧。

脚本

首先先将 settings.xml 文件复制两份,文件名分别命名为

  • settings_work.xml
  • settings_home.xml

分别对应公司和家里的 Maven 配置(如果你自定义名字了,记得修改下文中对应的文件名)

然后在 ~ 目录创建 switch-maven-setting.sh,内容如下:

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
#!/bin/bash

### Please set these properties for yourself
base_dir=~/.m2
setting_home=settings_home.xml
setting_work=settings_work.xml

PS3='Please enter the number of your choice: '
options=("home" "work" "quit")
select opt in "${options[@]}"
do
case $opt in
"home")
ln -sfn ${base_dir}/${setting_home} ${base_dir}/settings.xml
echo "Switched setting.xml to home!"
break
;;
"work")
ln -sfn ${base_dir}/${setting_work} ${base_dir}/settings.xml
echo "Switched setting.xml to work!"
break
;;
"quit")
break
;;
*) echo invalid option;;
esac
done

其中,将base_dir修改到你对应的目录,默认即为 Maven 的默认配置。脚本中使用软连接的方式来切换 settings.xml,无毒无害请放心食用。

食用方法:

1
2
3
4
5
6
7
$ chmod +x switch-maven-setting.sh
$ ./switch-maven-setting.sh
1) home
2) work
3) quit
Please enter the number of your choice: 1
Switched setting.xml to home!

Shell 脚本切换 Maven 的 settings.xml
https://www.haoyizebo.com/posts/d621449/
作者
一博
发布于
2018年4月14日
许可协议