>>>> IntelliJ IDEA 與 IntelliJ Platform
IntelliJ IDEA 簡稱 IDEA,是 Jetbrains 公司旗下的一款 JAVA 開發工具,支持 Java、Scala、Groovy 等語言的開發,同時具備支持目前主流的技術和框架,擅長於企業應用、移動應用和 Web 應用的開發,提供了豐富的功能,智能代碼助手、代碼自動提示、重構、J2EE支持、各類版本工具(git、svn等)、JUnit、CVS整合、代碼分析、 創新的GUI設計等。
IntelliJ Platform 是一個構建 IDE 的開源平台,基於它構建的 IDE 有 IntelliJ IDEA、WebStorm、DataGrip、以及 Android Studio 等等。IDEA 插件也是基於 IntelliJ Platform 開發的。
>>>> 開發環境搭建
本章節介紹 IDEA 插件開發環境的搭建與配置
>>>> 一、開發工具
開發工具使用 Intellij IDEA,下載地址:https://www.jetbrains.com/idea/
IDEA 分為兩個版本:
- 社區版(Community):完全免費,代碼開源,但是缺少一些旗艦版中的高級特性
- 旗艦版(Ultimate):30天免費,支持全部功能,代碼不開源
開發IDEA的插件推薦使用社區版,因為社區版是開源的,在開發插件的時候,可以調試源代碼。
>>>> 二、啟用 Plugin DevKit
Plugin DevKit 是 IntelliJ 的一個插件,它使用 IntelliJ IDEA 自己的構建系統來為開發 IDEA 插件提供支持。開發 IDEA 插件之前需要安裝並啟用 Plugin DevKit 。
打開 IDEA,導航到 Settings | Plugins,若插件列表中沒有 Plugin DevKit,點擊 Install JetBrains plugin,搜索並安裝。

>>>> 三、配置 IntelliJ Platform Plugin SDK
IntelliJ Platform Plugin SDK 就是開發 IntelliJ 平台插件的SDK, 是基於 JDK 之上運行的,類似於開發 Android 應用需要 Android SDK。
- 導航到 File | Project Structure,選擇對話框左側欄 Platform Settings 下的 SDKs
- 點擊 + 按鈕,先選擇 JDK,指定 JDK 的路徑;再創建 IntelliJ Platform Plugin SDK,指定 home path 為 IDEA 的安裝路徑,如圖

- 創建好 IntelliJ Platform Plugin SDK 后,選擇左側欄 Project Settings 下的 Projects,在 Project SDK 下選擇剛創建的 IntelliJ Platform Plugin SDK。

>>>> 四、設置源碼路徑(可選)
- 查看 build 號:打開 IDEA,Help | About,查看版本號及 build 號
- IDEA Community 源碼(https://github.com/JetBrains/intellij-community/):切換到與 build 號相同的分支,點擊 Clone or download 按鈕,選擇 Download ZIP

>>>> 五、Sandbox
IntelliJ IDEA 插件以 Debug/Run 模式運行時是在 SandBox 中進行的,不會影響當前的 IntelliJ IDEA;但是同一台機器同時開發多個插件時默認使用的同一個 sandbox,即在創建 IntelliJ Platform SDK 時默認指定的 Sandbox Home

如果需要每個插件的開發環境是相互獨立的,可以創建多個 IntelliJ Platform SDK,為 Sandbox Home 指定不同的目錄 。
>>>> 開發一個簡單插件
本篇介紹插件的創建、配置、運行、打包流程,以及 action
>>>> 一、創建一個插件工程
選擇 File | New | Project,左側欄中選擇 IntelliJ Platform Plugin 工程類型

點擊 Next,設置工程名稱及位置,點擊 Finish 完成創建。可以到 File | Project Structure 來自定義工程設置。
>>>> 二、插件工程結構
插件工程內容:
PluginDemo/
resources/ META-INF/ plugin.xml src/ com/foo/... ... ...
- src 實現插件功能的classes
- resources/META-INF/plugin.xml 插件的配置文件,指定插件名稱、描述、版本號、支持的 IntelliJ IDEA 版本、插件的 components 和 actions 以及軟件商等信息。
>>>> 三、plugin.xml
下面示例描述了可在 plugin.xml 文件配置的主要元素:
<idea-plugin> <!-- 插件名稱,別人在官方插件庫搜索你的插件時使用的名稱 --> <name>MyPlugin</name> <!-- 插件唯一id,不能和其他插件項目重復,所以推薦使用com.xxx.xxx的格式 插件不同版本之間不能更改,若沒有指定,則與插件名稱相同 --> <id>com.example.plugin.myplugin</id> <!-- 插件的描述 --> <description>my plugin description</description> <!-- 插件版本變更信息,支持HTML標簽; 將展示在 settings | Plugins 對話框和插件倉庫的Web頁面 --> <change-notes>Initial release of the plugin.</change-notes> <!-- 插件版本 --> <version>1.0</version> <!-- 供應商主頁和email--> <vendor url="http://www.jetbrains.com" email="support@jetbrains.com" /> <!-- 插件所依賴的其他插件的id --> <depends>MyFirstPlugin</depends> <!-- 插件兼容IDEA的最大和最小 build 號,兩個屬性可以任選一個或者同時使用 官網詳細介紹:http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html--> <idea-version since-build="3000" until-build="3999"/> <!-- application components --> <application-components> <component> <!-- 組件接口 --> <interface-class>com.foo.Component1Interface</interface-class> <!-- 組件的實現類 --> <implementation-class>com.foo.impl.Component1Impl</implementation-class> </component> </application-components> <!-- project components --> <project-components> <component> <!-- 接口和實現類相同 --> <interface-class>com.foo.Component2</interface-class> </component> </project-components> <!-- module components --> <module-components> <component> <interface-class>com.foo.Component3</interface-class> </component> </module-components> <!-- Actions --> <actions> ... </actions> <!-- 插件定義的擴展點,以供其他插件擴展該插件 --> <extensionPoints