如何为IntelliJ插件添加自定义项目模板并实现模板选择加载功能?
Got it, let's walk through building this IntelliJ plugin step by step. You're already on the right track with the plugin.xml configuration, but let's expand on that and cover all the pieces you need to make this work smoothly.
The <projectTemplate> tag you added registers your template with IntelliJ's built-in project creation system, but let's refine it and add context for multiple templates:
First, make sure you declare the necessary dependencies (e.g., for Java modules) and structure the tags properly:
<idea-plugin> <!-- Basic plugin info (name, id, version) --> <name>Custom Project Templates</name> <id>com.yourcompany.customtemplates</id> <version>1.0</version> <!-- Dependency for Java module support --> <depends>com.intellij.modules.java</depends> <!-- Register your templates --> <projectTemplate projectType="JAVA_MODULE" templatePath="resources/projectTemplates/java-sample.zip" category="My Custom Templates"/> <projectTemplate projectType="MAVEN_MODULE" templatePath="resources/projectTemplates/maven-sample.zip" category="My Custom Templates"/> </idea-plugin>
Let's break down the attributes:
projectType: Maps your template to a specific project/module type (e.g.,JAVA_MODULE,MAVEN_MODULE,GRADLE_MODULE)templatePath: Relative path to your zipped template from the plugin'sresourcesdirectorycategory: Groups your templates under a custom heading in the new project wizard (set tofalseif you don't want grouping)
Your zip file must mirror the structure of a valid IntelliJ project/module. Here's a standard structure for a Java module template:
java-sample.zip/ ├── src/ │ └── main/ │ └── java/ │ └── com/ │ └── example/ │ └── Main.java ├── .idea/ │ └── java-sample.iml └── README.md
Key notes:
- The
.imlfile is mandatory—IntelliJ uses it to recognize the module structure. You can generate this by creating a sample project in IntelliJ, then copying the.imlfile into your zip. - Exclude dynamic files like
.idea/workspace.xmlor.gitfrom the zip, as these are specific to individual environments.
If you want a dedicated UI for your templates instead of using the default new project wizard, you can build a custom dialog using IntelliJ's DialogWrapper:
public class TemplatePickerDialog extends DialogWrapper { private final List<String> templateOptions = Arrays.asList("Java Starter", "Maven Web App"); private JList<String> templateList; protected TemplatePickerDialog(@Nullable Project project) { super(project); init(); setTitle("Choose Your Project Template"); } @Nullable @Override protected JComponent createCenterPanel() { JPanel panel = new JPanel(new BorderLayout()); templateList = new JList<>(templateOptions.toArray(new String[0])); templateList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); templateList.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); panel.add(new JScrollPane(templateList), BorderLayout.CENTER); return panel; } @Override protected void doOKAction() { String selected = templateList.getSelectedValue(); if (selected != null) { // Fetch the registered template and trigger project creation ProjectTemplatesManager manager = ProjectTemplatesManager.getInstance(getProject()); ProjectTemplate matchingTemplate = manager.getTemplates().stream() .filter(t -> t.getName().equals(selected)) .findFirst() .orElse(null); if (matchingTemplate != null) { // Use IntelliJ's API to launch the project creation flow with this template ModuleBuilder builder = matchingTemplate.createModuleBuilder(); // Add logic to set project name, location, etc., then build the module } } } }
You can trigger this dialog from a toolbar action or menu entry by adding an <action> tag in plugin.xml.
- Use IntelliJ's plugin development tools to run your plugin in a sandbox environment.
- To test the default wizard integration: Go to
File > New > Project, select your target project type (e.g., Java Module), and your template should appear under the category you defined. - Select it, follow the wizard steps, and the project structure will generate just like Flutter's templates.
Troubleshooting Tips:
- Double-check the
templatePathin plugin.xml—ensure the zip is in the correct resources subdirectory. - Verify the
.imlfile in your zip has the correct module type settings. - Check IntelliJ's log (Help > Show Log in Explorer) for errors if the template doesn't appear.
内容的提问来源于stack exchange,提问作者Dev

