Idea Plugin Dev
Idea Plugin
Idea Plugin 我们一直在用。
当然学起来也不难。本篇提供简单的入门范例。
一些想法
适合:
1)检测
比如代码质量、语法等
2)代码生成
测试用例、文档等
3)其他工具类
依附于 idea 的便利性,开发对应的工具。
Quick Start
Prepare
- Plugin DevKit
【File】=>【Setting】=>【Plugins】=》【Plugin DevKit】
启用这个插件。(默认是开启的)
New Project
直接新建一个插件项目。

- plugin.xml
创建后会在 resources/META-INF
下有一个插件配置文件。内容如下:
com.your.company.unique.plugin.id
Plugin display name here
1.0
YourCompany
most HTML tags may be used
]]>
most HTML tags may be used
]]>
com.intellij.modules.lang
-->
- Build Number Ranges
``
用于指定插件对应的 idea 版本信息。详情见 Build Number Ranges。
Creating an action
- Project Struct
├─resources
│ └─META-INF
│ plugin.xml
│
└─src
└─com
└─ryo
└─idea
└─plugins
TextBoxes.java
- TextBoxes.java
这个类就是我们定义的 Action。内容如下:
package com.ryo.idea.plugins;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
/**
* Created by bbhou on 2017/10/13.
*/
public class TextBoxes extends AnAction {
// If you register the action from Java code, this constructor is used to set the menu item name
// (optionally, you can specify the menu description and an icon to display next to the menu item).
// You can omit this constructor when registering the action in the plugin.xml file.
public TextBoxes() {
// Set the menu item name.
super("Text _Boxes");
// Set the menu item name, description and icon.
// super("Text _Boxes","Item description",IconLoader.getIcon("/Mypackage/icon.png"));
}
public void actionPerformed(AnActionEvent event) {
Project project = event.getData(PlatformDataKeys.PROJECT);
String txt= Messages.showInputDialog(project, "What is your name?", "Input your name", Messages.getQuestionIcon());
Messages.showMessageDialog(project, "Hello, " + txt + "!\n I am glad to see you.", "Information", Messages.getInformationIcon());
}
}
- Register an action
当我们定义好上述 action 之后,需要进行注册。
注册的方式有两种: creating_an_action
(1) 界面操作
In your project, on the context menu of the destination package click New or press Alt + Insert.
On the New menu, click Action.
(2) 修改 plugin.xml
其中 (1) 实际上是 idea 自动帮我们修改了文件。个人偏向后者。
`` 将在主菜单添加对应菜单栏。
``
id
菜单唯一标识。class
为对应的实现类。
Run/Debug
和运行普通项目一样。然后就会启动一个新的 idea 窗口,可以在窗口中看到新添加的主菜单信息。
- To debug a plugin
Select Run/Debug in the main menu, or press Shift + F9.
- To run a plugin
Select Run/Run in the main menu, or press Shift + F10.
Deploying a Plugin
安装的步骤也比较简单:
一、生成
To deploy a plugin:
Make your project by invoking Build / Make Project.
Prepare your plugin for deployment. In the main menu, select Build / Prepare Plugin Module `` for Deployment.
运行完之后,会生成一个 .jar
文件。
二、复制到插件目录
将上述 jar 文件复制到 .IntelliJIDEAx0\config\plugins
, 重启 Idea 将生效。
关于插件文件夹的定位,详情见这里。
三、安装
这个和安装其他插件很类似。打开设置,直接安装即可。
In the main menu, select File/Settings to open the Settings dialog box.
In the Settings dialog box, under IDE Settings, click Plugins.
In the Plugins area, open the Installed tab, and then select the check-box next to your plugin name.
When finished, click OK to close the Settings dialog box.
Restart the IDE so that your changes take effect.
发布
为了更多人方便地使用我们的插件,可以直接发布到官方仓库中。