命令行快速打开idea工程

每次用 idea 打开 java 工程时,都要手动选择路径,特别麻烦。如果能在命令行中,cd到工程目录下,直接打开就好了。可惜 idea 本身并不提供这样的命令行工具,所以还是用脚本执行吧。

在目录~/.myscripts中新建脚本文件openIdea.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh

# check for where the latest version of IDEA is installed
IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
wd=`pwd`

# were we given a directory?
if [ -d "$1" ]; then
# echo "checking for things in the working dir given"
wd=`ls -1d "$1" | head -n1`
fi

# were we given a file?
if [ -f "$1" ]; then
# echo "opening '$1'"
open -a "$IDEA" "$1"
else
# let's check for stuff in our working directory.
pushd $wd > /dev/null

# does our working dir have an .idea directory?
if [ -d ".idea" ]; then
# echo "opening via the .idea dir"
open -a "$IDEA" .

# is there an IDEA project file?
elif [ -f *.ipr ]; then
# echo "opening via the project file"
open -a "$IDEA" `ls -1d *.ipr | head -n1`

# Is there a pom.xml?
elif [ -f pom.xml ]; then
# echo "importing from pom"
open -a "$IDEA" "pom.xml"

# can't do anything smart; just open IDEA
else
# echo 'cbf'
open "$IDEA"
fi

popd > /dev/null
fi

命令行执行

1
2
3
chmod 777 ~/.myscripts/openIdea.sh
echo "alias idea='sh ~/.myscripts/openIdea.sh'" >> ~/.profile
source ~/.profile

使用方式

cd到工程目录下,命令行执行idea即可。或者命令行直接执行idea XXXXXX是项目路径。

ref