从命令行打开IntelliJ IDEA及IntelliJ IDEA CE

假设我们有一个springboot项目,使用Maven构建的。当我们使用git clone xxx.git将项目clone到本地之后,我们怎么打开项目?
打开idea,新建empty project ,然后import from desk ? 太麻烦了。通过配置快捷命令,可以使用idea pom.xml打开项目。

在用户目录创建一个.idea.sh ,然后输入下面的一段脚本。

cd ~/
touch .idea.sh
vim .idea.sh

输入如下内容,保存退出。

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

创建命令别名

vim ~/.zshrc
输入 alias idea="sh ~/.idea.sh"

刷新配置,使生效:

source  ~/.zshrc

测试命令行打开Maven项目

cd /path/to/maven_project
idea pom.xml

以上配置无误的话,此时立马就能看到idea启动了,Maven项目已打开。

如果你像我一样,电脑上安装了IDEA CE的话,可以再配置一个ideace的别名。
跟上面的步骤一样,新建一个 .ideace.sh,内容跟.idea.sh的内容一样,只需要改变一行:

IDEA=`ls -1d /Applications/IntelliJ\ IDEA\ CE* | tail -n1`

然后创建一个别名alias ideace="sh ~/.ideace.sh"

接下来,便可以使用 ideace pom.xml来打开项目了。

posted @ 2020-12-21 19:08  逃离沙漠  阅读(2278)  评论(0编辑  收藏  举报