Windos使用gradle开发idea插件

安装gradle

进入gradle官网下载zip安装包,解压到指定目录,然后设置环境变量

  • GRADLE_USER_HOME=E:/gradle_repo // 本地仓库
  • GRADLE_HOME=D:\gradle-5.4.1
  • path=%GRADLE_HOME%\bin

Mac电脑上使用brew安装gradle,home目录是/usr/local/opt/gradle/libexec

在命令行中检验设置

gradle -version

在这里插入图片描述

新建Plugin工程

以IntelliJ IDEA为例。File - New - Project > Gradle,选中Java、IntelliJ Platform Plugin

在这里插入图片描述
在这里插入图片描述
这里官方推荐选择Use default gradle wrapper,我这里选择刚才自己安装的gradle。
接下来IDEA会根据插件工程模板生成一些目录结构和配置文件,这里先不不用管。

创建弹窗

src/main/下面新建java目录,设为source root,并创建自己的包路径。比如com.free.helloworld
新建一个HelloWorld类集成AnAction类,实现actionPerformed方法

package com.free.helloworld;

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;

public class HelloWorld extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getData(PlatformDataKeys.PROJECT);
        Messages.showMessageDialog(project, "第一个idea插件", "Hello World", Messages.getInformationIcon());
    }
}

上面创建了一个名称为Hello World的弹窗,弹窗的内容是“第一个idea插件”。
下面我们需要告诉idea怎么触发这个弹窗。
编辑src/main/resources/META-INF/plugin.xml<actions>

<idea-plugin>

    <!-- 原有的内容省略 -->
    <actions>
        <action class="com.free.helloworld.HelloWorld" id="helloWorld" text="HelloWorld">
            <add-to-group group-id="WindowMenu" anchor="last"/>
        </action>
    </actions>
</idea-plugin>

上面把HelloWorld的菜单项添加到了Window菜单的最后面。 目的就是点击Window - HelloWorld时触发弹窗。
至此,一个demo插件开发完毕!

运行调试HelloWorld

默认IDEA已经在Run选项中添加好了Plugin运行项,如果没有,自己手动配置一下:
在这里插入图片描述
点击运行按钮,就会启动一个新的IDEA,在这个IDEA界面中就可以点击刚才添加的按钮了。
在这里插入图片描述
最终效果如下
在这里插入图片描述
如果中文出现乱码,编辑build.gradle文件,在末尾添加

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

参考

http://www.jetbrains.org/intellij/sdk/docs/tutorials/build_system/prerequisites.html

问题

  1. 远端仓库存在的jar,可是项目总是找不到:
Plugin [id: 'org.jetbrains.intellij', version: '0.4.10'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.intellij:xxx')
Searched in the following repositories:
  Gradle Central Plugin Repository

解决方法:确认远端仓库配置是否正确;确认Build>Gradle>Offline work是否被勾选了,如果你勾选了,那只会在本地仓库找,不会搜索远端仓库。

发布了36 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Cmainlove/article/details/90110204