Windos使用gradle开发idea插件

本文介绍了在Windows上如何利用Gradle搭建IntelliJ IDEA插件开发环境,从安装Gradle开始,详细讲解新建Plugin工程、创建弹窗、运行调试HelloWorld的步骤,并提供了参考资料及解决可能出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装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是否被勾选了,如果你勾选了,那只会在本地仓库找,不会搜索远端仓库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值