IDEA 2023.1.2新版本开发插件,全新的版本,不同的实现,新版本开发插件跟旧版本的有些API是不一样的,旧版本的某些API已经更新了,因此,初次开发中还是遇到挺多的坑的,记录一下。
https://plugins.jetbrains.com/docs/intellij/welcome.html
一、新建插件项目
官方文档中描述的新建一个项目的步骤,描述的过程基本上就是上图的操作了,选择 IDE Plugin,输入项目名称,选择项目路径,填入GroupId,选择JDK版本,新版本IDEA默认就是选择的17版本,也只能选择17版本。
二、项目结构
my_plugin ├── .run │ └── Run IDE with Plugin.run.xml ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src │ └── main │ ├── kotlin │ └── resources │ └── META-INF │ ├── plugin.xml │ └── pluginIcon.svg ├── .gitignore ├── build.gradle.kts ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle.kts
- 默认的 IntelliJ 平台 build.gradle.kts 文件(见下一节)。
- gradle.properties 文件,包含 Gradle 构建脚本使用的属性。
- settings.gradle.kts 文件,包含 rootProject.name 和所需存储库的定义。
- Gradle Wrapper 文件,特别是 gradle-wrapper.properties 文件,它指定了用于构建插件的 Gradle 版本。如果需要,IntelliJ IDEA Gradle 插件会下载此文件中指定的 Gradle 版本。
- 默认 main 源集下的 META-INF 目录包含插件配置文件和插件图标。
- The Run Plugin run configuration.
三、build.gradle.kts
plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.8.21"
id("org.jetbrains.intellij") version "1.13.3"
}
group = "cn.youhaveme"
version = "1.0.0"
dependencies {
implementation("cn.hutool:hutool-all:5.8.16")
}
repositories {
mavenCentral()
}
// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
version.set("2022.2.5")
type.set("IC") // Target IDE Platform
plugins.set(listOf(/* Plugin Dependencies */))
}
tasks {
// Set the JVM compatibility versions
withType<JavaCompile> {
sourceCompatibility = "17"
targetCompatibility = "17"
}
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}
patchPluginXml {
sinceBuild.set("222")
untilBuild.set("232.*")
}
signPlugin {
certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
privateKey.set(System.getenv("PRIVATE_KEY"))
password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
}
publishPlugin {
token.set(System.getenv("PUBLISH_TOKEN"))
}
}
四、plugin.xml
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<!-- Unique identifier of the plugin. It should be FQN. It cannot be changed between the plugin versions. -->
<id>cn.youhaveme.Comma</id>
<!-- Public plugin name should be written in Title Case.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-name -->
<name>Comma</name>
<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
<vendor email="peichenwan@gmail.com" url="https://comma.youhaveme.cn">YouHaveMe</vendor>
<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
<description><![CDATA[
This is a tool that automatically generates SQL IN statements for multi row query values.<br>
<em>DataGrip users may prefer it.</em>
]]></description>
<!-- Product and plugin compatibility requirements.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<notificationGroup displayType="BALLOON" id="Notice" />
</extensions>
<actions>
<action id="cn.youhaveme.comma.CommaInAction" class="cn.youhaveme.comma.CommaInAction" text="CommaSQL"
description="选中值转IN_SQL语句" icon="/icon/comma.svg">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
<keyboard-shortcut keymap="$default" first-keystroke="meta alt COMMA"/>
</action>
</actions>
</idea-plugin>
五、实现一个事件
新建一个类,继承AnAction类,重写actionPerformed方法,在这个方法中开发自己的插件功能即可。
package cn.youhaveme.comma;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
/**
* 插件开发
* @author youhaveme.cn
*/
public class CommaInAction extends AnAction {
Log log = LogFactory.get();
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
log.info("开始插件之旅...");
}
}
六、打包插件
这个执行完之后会在build文件夹下生成一个插件的压缩包,这个压缩包就是插件包,本地安装或者上传Jetbrains的插件市场都是可以的,插件的整体流程不复杂,真正复杂的是你的功能,多看看官方插件开发文档,其实也就没有想象中的那么难了。