简体   繁体   English

IntelliJ Idea:如何在开发的插件中公开类,接口和注释

[英]IntelliJ Idea: how to expose classes, interfaces, annotations in developed plugins

I have created a plugin for IntelliJ Idea. 我为IntelliJ Idea创建了一个插件。 In the plugin I have defined an annotation I want to use in my projects, but it doesn't seem to be accessible. 在插件中,我已经定义了一个我希望在我的项目中使用的注释,但它似乎不可访问。 How should I specify in the plugin.xml file the packages I want to expose? 我应该如何在plugin.xml文件中指定我想要公开的包?

When you install a plugin, it will be on a certain place - eg C:\\Users\\xxx\\.IdeaIC14\\config\\plugins\\... 当您安装插件时,它将位于某个位置 - 例如C:\\Users\\xxx\\.IdeaIC14\\config\\plugins\\...

Now that you know where your jar file is, you can add it as a dependency to your project. 既然知道了jar文件的位置,就可以将它作为依赖项添加到项目中。 If you use Maven, you can add something like this to your pom: 如果你使用Maven,你可以在你的pom中添加这样的东西:

<dependency>
    <groupId>yourplugin</groupId>
    <artifactId>yourplugin</artifactId>
    <version>1</version>
    <systemPath>C:\Users\xxx\.IdeaIC14\config\plugins\yourplugin.jar</systemPath>
    <scope>system</scope>
</dependency>

Or you can install the jar into your local repository and then use it as normal maven dependency. 或者您可以将jar安装到本地存储库中 ,然后将其用作普通的maven依赖项。

If not, then add the dependency directly in the project settings as it was any other jar. 如果没有,那么直接在项目设置中添加依赖项,因为它是任何其他jar。

plugin.xml has nothing to do with any of this, this is all about jars and classpath. plugin.xml与任何这一点无关,这都是关于jar和classpath的。 What you could do in your plugin is some user friendly inspections and actions, which would add the dependency for you. 您可以在插件中执行的操作是一些用户友好的检查和操作,这将为您添加依赖项。

By default, plugins can automatically read and access public members of any other plugin installed on the same IDE (ie. your plugin can read public variables, call public functions - everything goes on the same classpath). 默认情况下,插件可以自动读取和访问安装在同一IDE上的任何其他插件的公共成员(即,您的插件可以读取公共变量,调用公共函数 - 所有内容都在同一个类路径上)。 If you depend on another plugin, you must first add it as an explicit dependency within the plugin configuration file , so that the end user's IDE will know to download and install your plugin's required plugin dependencies if they are missing. 如果您依赖于另一个插件,则必须首先将其添加为插件配置文件中显式依赖项 ,以便最终用户的IDE知道如果缺少插件所需的插件依赖项,则下载并安装它们。

During development, you should now be using Gradle. 在开发过程中,您现在应该使用Gradle。 In your project's build.gradle (or build.gradle.kts ) file, the intellij.plugins property of the gradle-intellij-plugin will let you specify the the id and version of the plugin dependency. 在项目的build.gradle (或build.gradle.kts )文件中, gradle-intellij-pluginintellij.plugins属性将允许您指定插件依赖项的idversion The values for these attributes can be found on the Plugin Repository , cf. 可以在插件存储库中找到这些属性的值,参见 Plugin XML ID ). Plugin XML ID )。 Subsequently, the gradle-intellij-plugin will add the desired IntelliJ Platform Plugin to your project as an External Library , thereby allowing you to get code completion, static analysis and test your plugin alongside its dependencies inside the plugin sandbox (via ./gradlew runIde ). 随后, gradle-intellij-plugin将所需的IntelliJ平台插件作为外部库添加到项目中,从而允许您获得代码完成,静态分析并测试插件以及插件沙箱中的依赖项(通过./gradlew runIde )。

Plugins should avoid using other plugins' internal classes for stability reasons. 出于稳定性原因,插件应避免使用其他插件的内部类。 If you wish to enable other plugins to use your plugin programmatically (ie. suppose you want to provide an API), then the IntelliJ Platform has the concept of so-called, Extension Points . 如果您希望启用其他插件以编程方式使用您的插件(即假设您想提供API),那么IntelliJ平台具有所谓的扩展点的概念。 These will allow you to define a concrete interface, or contract for other plugins to access the functionality of your plugin, without needing to know much about its source code. 这些将允许您定义具体的接口,或其他插件的合同 ,以访问您的插件的功能,而无需了解其源代码。 Using extension points has the added benefit of decoupling those plugins from any internal plugin refactoring. 使用扩展点具有将这些插件与任何内部插件重构分离的额外好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM