IDEA插件开发入门(一)Hello World

IDEA插件开发
placeholder image
admin 发布于:2024-01-21 15:12:42
阅读:loading

1. 背景介绍

早在多年前我还在使用Eclipse时,经常思考着想学学它的插件开发,但一直没有实践(主要也不会,也懒得搞),也不是完全没有实践,曾经买了一本书叫《深入理解Java虚拟机》中翻到了一个示例,是编写了一个Eclipse启动耗时插件,按着示例来还真就运行成功了(该插件包也在博客园中被一个大佬写的性能分析的文章所引用),也就仅限于此了。对我个人来讲IntelliJ IDEA已经彻底完全的取代Eclipse了,所以本次又花了数天的时间来摸索着从0到1的IDEA插件开发,掌握的细节与知识还非常的粗浅,但是作为专业程度上的入门来说,还是非常科学的。

2. 创建项目

由于是从0基础开始的,就从IDEA创建一个插件项目开始,我的IDEA版本是“IntelliJ IDEA 2023.2 (Ultimate Edition)”,新建Project选择IDE Plugin类型,参考如下图所示:

image.png

项目创建完毕后得到一个基本的插件项目结构,但是编译时报“Connect timed out”,仔细查找后发现是“gradle-wrapper.properties”文件中的gradle依赖没有被下载下来所致,我这里是使用浏览器自行下载,下载完毕后放入任意目录无需解压,本次存放的目录和其它参考见下图:

image.png

3.项目说明

(1)IDEA插件开发需要JDK17及以上版本,新手最好是有外网环境,所以前提需要准备:IDEA(2023.2)、JDK(jdk-17.0.9)、Gradle(gradle-8.1.1);

(2)新建Project 选择 IDE Plugin类型,可以选择项目类型是Plugin或Theme(本次以Plugin为例),并选择JDK版本,输入项目名称等各个参数后保存确定;

(3)创建完成后的插件项目,本次命名为“chendd-idea-plugins”,默认在项目路径的配置文件中“chendd-idea-plugin\gradle\wrapper\gradle-wrapper.properties”指定了gradle的在线地址,由于此次使用了本地的gradle,所以修改参数为本地的路径,参考配置如下:

#注释默认的distributionUrl参数,设置为本地路径

#distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

distributionUrl=file:///E:/dev-tools/Java/gradle-8.1.1-bin.zip

(4)修改完毕后可以点击刷新按钮进行项目环境刷新,刷新项目默认为下载许许多多的项目以来,比如:kotlin-compiler-embeddable、jetbrains/intellij/plugins等等,默认从外网下载,可以通过修改build.gradle.kts文件的配置来增加私服地址。

(5)相关的Gradle的配置可在下图中设置:

image.png

(6)项目初始构建的控制台全量输出:

Download file:///E:/dev-tools/Java/gradle-8.1.1-bin.zip, took 188 ms (124.64 MB)
Starting Gradle Daemon...
Gradle Daemon started in 1 s 624 ms
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.10/kotlin-stdlib-1.8.10.pom, took 192 ms (1.55 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.10/kotlin-stdlib-common-1.8.10.pom, took 778 ms (1.16 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.10/kotlin-stdlib-common-1.8.10.jar, took 13 s 29 ms (217.44 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.10/kotlin-stdlib-1.8.10.jar, took 2 m 25 s 565 ms (1.64 MB)
> Task :prepareKotlinBuildScriptModel UP-TO-DATE
Download https://repo.maven.apache.org/maven2/org/jetbrains/annotations/24.0.0/annotations-24.0.0.pom, took 450 ms (1.32 kB)
Download https://cache-redirector.jetbrains.com/intellij-jbr/jbr_jcef-17.0.6-windows-x64-b469.82.tar.gz, took 1 m 30 s 690 ms (176.66 MB)
Download https://www.jetbrains.com/updates/updates.xml, took 870 ms (0 B)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.8.22/kotlin-gradle-plugin-1.8.22-sources.jar, took 1 m 0 s 1 ms (1.01 MB)
Download https://plugins.gradle.org/m2/org/jetbrains/intellij/plugins/gradle-intellij-plugin/1.14.1/gradle-intellij-plugin-1.14.1-sources.jar, took 2 s 375 ms (119.3 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin-model/1.8.22/kotlin-gradle-plugin-model-1.8.22-sources.jar, took 670 ms (7.93 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin-api/1.8.22/kotlin-gradle-plugin-api-1.8.22-sources.jar, took 4 s 289 ms (49.92 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin-idea-proto/1.8.22/kotlin-gradle-plugin-idea-proto-1.8.22-sources.jar, took 16 s 63 ms (252.19 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin-idea/1.8.22/kotlin-gradle-plugin-idea-1.8.22-sources.jar, took 1 s 957 ms (25.06 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-project-model/1.8.22/kotlin-project-model-1.8.22-sources.jar, took 542 ms (10.02 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-tooling-core/1.8.22/kotlin-tooling-core-1.8.22-sources.jar, took 505 ms (12.54 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-util-klib/1.8.22/kotlin-util-klib-1.8.22-sources.jar, took 1 s 10 ms (26.73 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-klib-commonizer-api/1.8.22/kotlin-klib-commonizer-api-1.8.22-sources.jar, took 522 ms (10.76 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-android-extensions/1.8.22/kotlin-android-extensions-1.8.22-sources.jar, took 8 s 11 ms (97.3 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-compiler-runner/1.8.22/kotlin-compiler-runner-1.8.22-sources.jar, took 860 ms (7.1 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-compiler-embeddable/1.8.22/kotlin-compiler-embeddable-1.8.22-sources.jar, took 12 m 37 s 262 ms (10.04 MB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-scripting-compiler-embeddable/1.8.22/kotlin-scripting-compiler-embeddable-1.8.22-sources.jar, took 1 s 134 ms (81.3 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-scripting-compiler-impl-embeddable/1.8.22/kotlin-scripting-compiler-impl-embeddable-1.8.22-sources.jar, took 4 s 570 ms (48.93 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/intellij/plugin-repository-rest-client/2.0.32/plugin-repository-rest-client-2.0.32-sources.jar, took 2 s 46 ms (28.28 kB)
Download https://repo.maven.apache.org/maven2/com/squareup/retrofit2/retrofit/2.9.0/retrofit-2.9.0-sources.jar, took 6 s 301 ms (75.28 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/intellij/plugins/structure-intellij/3.259/structure-intellij-3.259-sources.jar, took 5 s 292 ms (52.78 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/intellij/plugins/structure-base/3.259/structure-base-3.259-sources.jar, took 3 s 645 ms (25.23 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar, took 1 s 743 ms (17.57 kB)
Download https://repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.15.2/jackson-databind-2.15.2-sources.jar, took 1 m 33 s 485 ms (1.2 MB)
Download https://repo.maven.apache.org/maven2/javax/xml/bind/jaxb-api/2.4.0-b180830.0359/jaxb-api-2.4.0-b180830.0359-sources.jar, took 18 s 499 ms (270.93 kB)
Download https://repo.maven.apache.org/maven2/com/googlecode/plist/dd-plist/1.27/dd-plist-1.27-sources.jar, took 10 s 963 ms (139.05 kB)
Download https://plugins.gradle.org/m2/gradle/plugin/org/jetbrains/gradle/plugin/idea-ext/gradle-idea-ext/1.1.7/gradle-idea-ext-1.1.7-sources.jar, took 757 ms (23.1 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin-annotations/1.8.22/kotlin-gradle-plugin-annotations-1.8.22-sources.jar, took 428 ms (2.24 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-native-utils/1.8.22/kotlin-native-utils-1.8.22-sources.jar, took 2 s 20 ms (13 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-util-io/1.8.22/kotlin-util-io-1.8.22-sources.jar, took 2 s 272 ms (10.86 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-daemon-client/1.8.22/kotlin-daemon-client-1.8.22-sources.jar, took 3 s 909 ms (41.97 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.5.0/kotlinx-coroutines-core-jvm-1.5.0-sources.jar, took 30 s 127 ms (357.25 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-daemon-embeddable/1.8.22/kotlin-daemon-embeddable-1.8.22-sources.jar, took 369 ms (39.54 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/intellij/deps/trove4j/1.0.20200330/trove4j-1.0.20200330-sources.jar, took 25 s 525 ms (477.1 kB)
Download https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.6.0/jna-5.6.0-sources.jar, took 12 s 761 ms (157.35 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-scripting-jvm/1.8.22/kotlin-scripting-jvm-1.8.22-sources.jar, took 2 s 792 ms (29.26 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-scripting-common/1.8.22/kotlin-scripting-common-1.8.22-sources.jar, took 3 s 86 ms (31.11 kB)
Download https://repo.maven.apache.org/maven2/com/squareup/retrofit2/converter-jaxb/2.9.0/converter-jaxb-2.9.0-sources.jar, took 431 ms (4.19 kB)
Download https://repo.maven.apache.org/maven2/com/squareup/retrofit2/converter-jackson/2.9.0/converter-jackson-2.9.0-sources.jar, took 606 ms (3.88 kB)
Download https://repo.maven.apache.org/maven2/com/squareup/okhttp3/okhttp/4.4.1/okhttp-4.4.1-sources.jar, took 31 s 473 ms (346.86 kB)
Download https://repo.maven.apache.org/maven2/com/fasterxml/jackson/module/jackson-module-kotlin/2.15.2/jackson-module-kotlin-2.15.2-sources.jar, took 2 s 53 ms (32.55 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/intellij/blockmap/1.0.5/blockmap-1.0.5-sources.jar, took 864 ms (6.75 kB)
Download https://repo.maven.apache.org/maven2/com/sun/xml/bind/jaxb-impl/2.3.2/jaxb-impl-2.3.2-sources.jar, took 56 s 851 ms (759.99 kB)
Download https://repo.maven.apache.org/maven2/com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0-sources.jar, took 19 s 659 ms (345.96 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-serialization-json-jvm/1.5.1/kotlinx-serialization-json-jvm-1.5.1-sources.jar, took 4 s 345 ms (79.73 kB)
Download https://repo.maven.apache.org/maven2/org/glassfish/jaxb/jaxb-runtime/2.3.1/jaxb-runtime-2.3.1-sources.jar, took 1 m 19 s 939 ms (1.13 MB)
Download https://repo.maven.apache.org/maven2/org/jsoup/jsoup/1.16.1/jsoup-1.16.1-sources.jar, took 5 s 462 ms (236.63 kB)
Download https://repo.maven.apache.org/maven2/org/jdom/jdom2/2.0.6.1/jdom2-2.0.6.1-sources.jar, took 32 s 354 ms (488.76 kB)
Download https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.23.0/commons-compress-1.23.0-sources.jar, took 1 m 37 s 701 ms (904.42 kB)
Download https://repo.maven.apache.org/maven2/org/tukaani/xz/1.9/xz-1.9-sources.jar, took 10 s 591 ms (136.94 kB)
Download https://repo.maven.apache.org/maven2/org/atteo/evo-inflector/1.3/evo-inflector-1.3-sources.jar, took 1 s 50 ms (8.61 kB)
Download https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7-sources.jar, took 5 s 396 ms (71.83 kB)
Download https://repo.maven.apache.org/maven2/commons-io/commons-io/2.10.0/commons-io-2.10.0-sources.jar, took 45 s 987 ms (397.66 kB)
Download https://repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.15.2/jackson-annotations-2.15.2-sources.jar, took 2 s 434 ms (81.83 kB)
Download https://repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jackson-core/2.15.2/jackson-core-2.15.2-sources.jar, took 32 s 601 ms (465.56 kB)
Download https://repo.maven.apache.org/maven2/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0-sources.jar, took 5 s 780 ms (78.9 kB)
Download https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6-sources.jar, took 15 s 655 ms (155.55 kB)
Download https://repo.maven.apache.org/maven2/com/google/guava/guava/28.2-jre/guava-28.2-jre-sources.jar, took 1 m 46 s 54 ms (1.67 MB)
Download https://repo.maven.apache.org/maven2/com/squareup/okio/okio/2.4.3/okio-2.4.3-sources.jar, took 6 s 420 ms (109.39 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.10/kotlin-stdlib-1.8.10-sources.jar, took 13 s 429 ms (250.22 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.10/kotlin-stdlib-common-1.8.10-sources.jar, took 2 s 444 ms (315.04 kB)
Download https://repo.maven.apache.org/maven2/org/glassfish/jaxb/txw2/2.3.1/txw2-2.3.1-sources.jar, took 3 s 53 ms (100.54 kB)
Download https://repo.maven.apache.org/maven2/com/sun/istack/istack-commons-runtime/3.0.7/istack-commons-runtime-3.0.7-sources.jar, took 4 s 380 ms (38.05 kB)
Download https://repo.maven.apache.org/maven2/org/jvnet/staxex/stax-ex/1.8/stax-ex-1.8-sources.jar, took 2 s 867 ms (41.88 kB)
Download https://repo.maven.apache.org/maven2/com/sun/xml/fastinfoset/FastInfoset/1.2.15/FastInfoset-1.2.15-sources.jar, took 15 s 92 ms (257.75 kB)
Download https://repo.maven.apache.org/maven2/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1-sources.jar, took 260 ms (3.49 kB)
Download https://repo.maven.apache.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2-sources.jar, took 3 s 429 ms (18.1 kB)
Download https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/2.10.0/checker-qual-2.10.0-sources.jar, took 11 s 780 ms (227.35 kB)
Download https://repo.maven.apache.org/maven2/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4-sources.jar, took 2 s 852 ms (27.02 kB)
Download https://repo.maven.apache.org/maven2/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3-sources.jar, took 1 s 161 ms (10.48 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-serialization-core-jvm/1.5.1/kotlinx-serialization-core-jvm-1.5.1-sources.jar, took 6 s 464 ms (104.27 kB)
Download https://services.gradle.org/distributions/gradle-8.1.1-src.zip, took 51 m 32 s 879 ms (44.56 MB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.10/kotlin-stdlib-jdk8-1.8.10.pom, took 700 ms (1.58 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-reflect/1.8.10/kotlin-reflect-1.8.10.pom, took 136 ms (1.38 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-reflect/1.8.10/kotlin-reflect-1.8.10-sources.jar, took 35 s 960 ms (713.76 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/annotations/24.0.0/annotations-24.0.0.jar, took 2 s 41 ms (30.54 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/annotations/24.0.0/annotations-24.0.0-sources.jar, took 1 s 188 ms (38.54 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.22/kotlin-stdlib-1.8.22.pom, took 1 s 615 ms (1.55 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.22/kotlin-stdlib-common-1.8.22.pom, took 142 ms (1.16 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-script-runtime/1.8.22/kotlin-script-runtime-1.8.22.pom, took 481 ms (1.16 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-script-runtime/1.8.22/kotlin-script-runtime-1.8.22.jar, took 934 ms (42.83 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.22/kotlin-stdlib-common-1.8.22.jar, took 4 s 984 ms (221.49 kB)
Download https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.22/kotlin-stdlib-1.8.22.jar, took 1 m 35 s 346 ms (1.67 MB)

BUILD SUCCESSFUL in 1h 29m 39s

(7)编译后完整的项目目录结构如下图所示:

image.png

(8)运行插件示例会打开一个新的IDEA窗口,跟初次开启IDEA是一样的,主要还包含了“Start Tour”和“Start Leaning”,前者是启动一个IDEA插件项目的介绍,与第一次进入一个系统一样分为各个功能点的下一步下一步的介绍;后者是启动打开一个IDEA学习项目,一般可以选择这个来验证插件的编写结果,参考如下图所示:

image.png

(自定义插件介绍)

image.png

(项目介绍)

image.png

(学习项目)

(10)项目完整代码与完整的运行示例gif图可点击此处下载(后文系列章节的源码均从此处下载):源码下载与运行过程.zip

 点赞


 发表评论

当前回复:作者

 评论列表


留言区