如何开发可通过CLI或Web服务触发IntelliJ动作的插件?
Hey there! Let's walk through how to build that IntelliJ plugin you need—one that lets you trigger IDE actions from the CLI or a simple web service, perfect for automating post-Gradle build setup without clicking around. Here's a practical, step-by-step guide:
First, a quick primer on the key pieces you'll work with:
- IntelliJ Action System: Every button click or menu operation in the IDE is an
AnActionclass. You can either reuse existing actions (like "Refresh All Gradle Projects") or build custom ones for your specific setup tasks. - Remote/External Triggers: To trigger these actions from outside the IDE, you'll either use IntelliJ's built-in remote API (the easiest path) or add a custom listener/server to your plugin (for more flexibility).
Start by creating a new IntelliJ plugin project using the official tools:
- Open IntelliJ, go to
File > New > Project, select IntelliJ Platform Plugin from the project templates. - Pick an SDK that matches your target IntelliJ version (e.g., IntelliJ IDEA 2024.1) and finish the setup.
- Keep an eye on your
plugin.xmlfile—this is where you'll register actions, components, and other plugin extensions.
IntelliJ has a built-in HTTP API that lets you trigger actions directly, no custom server needed:
- Enable the remote server: Go to
Settings > Build, Execution, Deployment > Remote Development > Remote Server, check "Enable remote server" and set a port (e.g., 63342, the default). - Find your action ID: To trigger an existing action, go to
Settings > Keymap, right-click the action you want (like "Refresh All Gradle Projects"), select "Copy Reference"—the last segment of the copied string is your action ID (e.g.,Gradle.RefreshAll). - Trigger via CLI: Use
curlor any HTTP client to send a POST request to the API endpoint:curl -X POST http://localhost:63342/api/action/Gradle.RefreshAll - Custom actions: If you build a custom
AnAction, register it inplugin.xmlwith an<action>tag and give it anidattribute—then use that ID in your API call.
If you need more flexibility (like handling custom parameters or complex logic), add a lightweight server to your plugin:
- Add dependencies: Use a simple HTTP library like Spark Java (it's lightweight and plays nicely with IntelliJ's classpath). You can add it to your
build.gradleorpom.xmlif using Maven. - Start the server in your plugin: Use an
ApplicationComponentorProjectComponentto start the server when the IDE launches. Make sure to run actions on the Event Dispatch Thread (EDT)—IntelliJ's UI thread—otherwise you'll get errors. Example code:import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.actionSystem.ActionManager; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; import spark.Spark; public class MyPluginComponent implements com.intellij.openapi.components.ApplicationComponent { @Override public void initComponent() { // Start a simple server on port 8080 Spark.port(8080); // Endpoint to trigger Gradle refresh Spark.post("/trigger/refresh-gradle", (req, res) -> { // Run the action on the EDT to avoid UI thread issues ApplicationManager.getApplication().invokeLater(() -> { AnAction refreshAction = ActionManager.getInstance().getAction("Gradle.RefreshAll"); if (refreshAction != null) { refreshAction.actionPerformed(new AnActionEvent( null, DataContext.EMPTY, "", ActionManager.getInstance().getActionPresentation("Gradle.RefreshAll"), 0 )); } }); return "Action triggered successfully!"; }); } @Override public void disposeComponent() { Spark.stop(); // Stop the server when the IDE closes } } - Trigger via CLI: Call your custom endpoint with
curl:curl -X POST http://localhost:8080/trigger/refresh-gradle
To tie this directly to your Gradle build, add a task that triggers the action once the build completes:
// In your build.gradle file task triggerIntelliJSetup { dependsOn build // Run after the build task finishes doLast { exec { // Use the built-in API endpoint or your custom one commandLine 'curl', '-X', 'POST', 'http://localhost:63342/api/action/Gradle.RefreshAll' } } }
Now running ./gradlew triggerIntelliJSetup will build your project and automatically trigger the IDE setup action.
- Thread Safety: Always run IntelliJ actions on the EDT using
Application.invokeLater()—never run UI-related code on a background thread. - Security: If you're exposing an HTTP server, add authentication (like API keys) to prevent unauthorized access, especially on shared machines.
- Testing: Use IntelliJ's Plugin Development Run Configuration to test your plugin in a sandbox IDE—this lets you debug and test trigger flows safely.
- Action IDs Double-Check: Make sure your action IDs are correct—if the action isn't triggering, verify the ID via the Keymap settings.
内容的提问来源于stack exchange,提问作者Nicholas DiPiazza




