How to attach a facet on new project creation?

I am trying to create an intellij plugin which automatically attaches a facet to my root project module whenever a new project is created.
I have tried to create the facet in the setupProject step of my NewProjectWizardStep but there was no modules available (Presumably the root module has yet to be created). I am using the new project wizard API if that matters.

val moduleManager = ModuleManager.getInstance(project)
val modules = moduleManager.modules // This is empty so the code below doesn't run
if (modules.isNotEmpty()) {
val module = modules[0]
val facetManager = FacetManager.getInstance(module)
val facetModel = facetManager.createModifiableModel()
val facetType = FacetType.findInstance(GhidraFacetType::class.java)
val facet = facetManager.createFacet(facetType, "Ghidra", null)
facetModel.addFacet(facet)
facetModel.commit()
}

How can I do this?

I would recommend not designing the settings this way. Facets should be created by users only when some manual customization is needed.

There are numerous ways how modules appear in project:

  • created via UI
  • imported from Gradle/Maven
  • loaded from .iml on VCS update

It is super hard to cover all those cases and make something consistent.

Previously, IntelliJ IDEA used some auto-import of facets, for Spring and JPA technologies. It was implemented as import handler for Gradle/Maven to make sure it always in sync with build settings. Nowadays, we don’t do that, we auto-discover and cache such automatic settings if there are no facets - making features always available without excessive configuration. Facets are considered only user-overrides / customizations then.

We also do not introduce any new facet types now, prefer technology-native configuration or Project settings instead.

As you may notice Facets exist only in IntelliJ IDEA and do not exist in other JetBrains IDEs

If so, then what is your recommendation to implement logic where a user creates a new custom project and is able to automatically have some SDKs loaded?
Will it be sufficient to create a root module and library during new project creation and add the library as a dependency to the root module?

Appreciate the advice :+1:

It is unclear, what kind of SDK, it is your SDK type? SDKs also usually a Project-level entity, or even global ones

Sorry got a little confused previously. I was referring to a bunch of library (JAR) files. The library files are essential for development of the Java framework the plugin is for. Ideally i will want to automatically include these jar files during project creation so the user do not have to do so manually.

The previous developer of the plugin added the JAR files via a facet but considering what you mentioned it seems that it wouldn’t be necessary.

Thanks again

As far as I know you do not need facet to attach JARs and class directories to Java module .iml configuration. You probably can use ModuleRootManager.getModifiableModel() and also check how IntelliJ IDEA Community Java support adds new class directories from UI.

Some leads:

  • com.intellij.openapi.roots.ModuleRootModificationUtil#addDependency
  • com.intellij.openapi.roots.ModuleRootModificationUtil#setModuleSdk

NewProjectWizardStep.setupProject seems correct place for that, but no any imported modules exist at this stage, such as Maven/Gradle

This is interesting! Need to look at it closer

Ok got it, thanks for all the advice Yuiry. it helps alot especially for people who are just getting their hands wet with intellij plugin dev :grinning_face: