以下是使用 Maven 框架搭建 IDEA 插件项目的步骤:
-
打开 IDEA,点击 File -> New -> Project,选择 Maven。
-
在弹出的 New Project 窗口中,选择 Maven,然后选择 Create from archetype,找到 Maven 插件开发的 archetypes,选择 IntelliJ IDEA Plugin Archetype。
-
填写 GroupId、ArtifactId、Version 等基本信息,然后点击 Next。
-
在下一个窗口中,可以选择插件类型,如 Action、Tool Window、Inspection 等,选择合适的类型并填写信息,然后点击 Next。
-
在下一个窗口中,可以选择项目配置,如 JDK 版本、项目名称、包名等,填写好后点击 Next。
-
最后一步是确认信息,检查无误后点击 Finish,等待 IDEA 创建项目。
-
项目创建完毕后,可以在 pom.xml 文件中添加插件依赖,配置插件信息等。
现在,你已经成功搭建了一个 IDEA 插件项目,可以开始编写代码了。
步骤如下:
-
在IDEA中新建一个Maven项目,选择
Create from archetype
,选择org.jetbrains.intellij.idea-plugin
,点击Next
。 -
输入你的项目名称,点击
Next
。 -
输入你的
groupId
和artifactId
,点击Next
。 -
点击
Finish
,完成项目的创建。 -
在
pom.xml
文件中,添加需要的依赖。例如,如果你需要使用JUnit
进行测试,则需要添加以下依赖:
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>
-
在
src/main
目录下创建一个resources/META-INF/plugin.xml
文件,这个文件包含了你的插件的配置信息。 -
在
src/main
目录下创建你的插件代码,在这里可以实现你需要的功能。 -
在
pom.xml
文件中添加maven-jar-plugin
插件,以便能够生成插件文件:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.4</version><configuration><archive><manifest><mainClass>com.intellij.idea.plugin.hello.HelloWorldApplicationComponent</mainClass><id>com.intellij</id><name>hello-idea-plugin</name><version>1.0-SNAPSHOT</version></manifest></archive></configuration></plugin></plugins>
</build>
- 在
pom.xml
文件中添加maven-assembly-plugin
和maven-shade-plugin
插件,以便能够将所有的依赖项打包到一个jar文件中:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version><configuration><archive><manifest><mainClass>com.intellij.idea.plugin.hello.HelloWorldApplicationComponent</mainClass><id>com.intellij</id><name>hello-idea-plugin</name><version>1.0-SNAPSHOT</version></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>2.4.3</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><includes><include>*:*</include></includes></artifactSet><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.intellij.idea.plugin.hello.HelloWorldApplicationComponent</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins>
</build>
-
执行
mvn clean package
命令,生成插件的jar文件。 -
在IDEA中选择
File
>Settings
,选择Plugins
,点击Install plugin from disk
,选择你生成的插件的jar文件,点击OK
。
搭建步骤如下:
- 创建一个Maven项目,选择idea-plugin-archetype作为项目模板。
mvn archetype:generate
-DarchetypeGroupId=org.apache.maven.archetypes
-DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeVersion=1.4
-DgroupId=com.example
-DartifactId=my-idea-plugin
-DinteractiveMode=false
-DarchetypeCatalog=local
- 修改pom.xml文件,添加以下插件依赖:
<dependencies><!--添加依赖--><dependency><groupId>com.intellij</groupId><artifactId>ideaIU</artifactId><version>2019.3.4</version><scope>provided</scope></dependency>
</dependencies><build><plugins><!--添加插件--><plugin><groupId>org.jetbrains.intellij</groupId><artifactId>idea-plugin</artifactId><version>0.4.17</version><extensions>true</extensions><configuration><id>${project.artifactId}</id><name>${project.name}</name><description>${project.description}</description><vendor>Email</vendor><changeNotes>${project.version}</changeNotes><productDescriptor><code>Java</code><since-build>201.0</since-build><until-build>201.*</until-build></productDescriptor></configuration></plugin></plugins>
</build>
-
在src/main目录下创建resources/META-INF/plugin.xml文件。
-
编写插件功能代码。
-
在IDEA中打开项目,右键点击pom.xml文件,选择Maven -> Generate Sources and Update Folders。
-
在IDEA中选择菜单栏中的Run -> Edit Configurations,然后点击左上角的“+”号,选择“Plugin”来创建一个新的运行配置。
-
点击运行按钮来启动插件。在启动后的IDEA中,可以看到插件已经成功安装并开始工作。
以上就是使用Maven框架搭建一个IDEA插件项目的步骤。