0%

Alfred realizes one-click switching of Maven settings

Because the company project needs to use the company’s internal Maven resources, it is not required at home, so it is time-consuming to switch settings back and forth. To achieve automation, consider making a script to switch sources.

Scripting - Preliminary Scheme

I saw an article on the Internet - script switching, which gave me inspiration. Paste the script here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

base_dir=~/.m2
setting_home=settings_home.xml
setting_work=settings_work.xml

PS3='Please enter the number of your choice: '
options=("home" "work")
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
;;

esac
done

As above, you can implement interactive script execution to switch. But this solution still needs to execute the script every time.
Is there a way to do one-click switching? Yes! Alfred can.

Create Alfred Workflow-Advanced Solution

How?

Add input keyword

Add list filter

Add script execution

For the above script, make the following improvements.

1
2
3
4
5
6
7
8
9
10
base_dir=~/.m2
setting_home=settings_home.xml
setting_work=setting_work.xml

if [ "{query}" == "home" ];
then
ln -sfn ${base_dir}/${setting_home} ${base_dir}/settings.xml
else
ln -sfn ${base_dir}/${setting_work} ${base_dir}/settings.xml
fi

mvn setting default configuration

Here is the maven default settings.xml configuration

Click Here

Add notification

In order to improve the experience, each time the switch is successful, you will be notified.

Final effect

So we can switch Maven settings with one click now. Perfect.

Reference