Correct way to import Maven/Gradle project

Answered

We have a old plugin which I am trying to update it to support 2019.x and later. It basically does the following:

1. Creates a Maven or a Gradle project depending on user selection

2. Imports/Opens the project into IntelliJ IDEA

Until now, we had a very simple implementation to do this. We created an instance of either MavenProjectImportProvider or GradleProjectImportProvider. Passed the provider to create an AddModuleWizard instance. Ultimately, making an call to ImportModuleAction.createFromWizard to open a Window which would import the project:

GradleProjectImportBuilder gradleProjectImportBuilder = new GradleProjectImportBuilder(ProjectDataManager.getInstance());
projectImportProvider = new GradleProjectImportProvider(gradleProjectImportBuilder);
AddModuleWizard wizard = new AddModuleWizard(project, baseFile, projectImportProvider);
if (wizard.getStepCount() <= 0 || wizard.showAndGet()) {
ImportModuleAction.createFromWizard(project, wizard);
}

The above code works perfectly in IC-2019.x versions. However, it begins to fail with IC-2020.2.

For Gradle projects, while compiling with IC-2019.x versions, I do get a warning that GradleProjectImportProvider is deprecated and the suggestion is to use JavaGradleProjectImportProvider. However, the documentation says that we shouldn't use JavaGradleProjectImportProvider directly.

For Maven projects, although MavenProjectImportProvider is not deprecated, IC-2020.2 opens the project but won't import the Maven project and just shows the iml and pom.xml files in the Project window.

What is the recommended fix/API to import Maven/Gradle projects?

0
4 comments

Please try

 Use [com.intellij.ide.actions.ImportModuleAction.doImport] to import (attach) a new project.
* Use [com.intellij.ide.impl.ProjectUtil.openOrImport] to open (import) a new project.
0
Avatar
Permanently deleted user

Hi Yann,

Thank you for your reply.

I have already read about these APIs while going through the documentation on JavaGradleProjectImportBuilder. Unfortunately, both these do not work as per our usecase. We want to import and open the Project as a Maven/Gradle project.

If we use ImportModuleAction.doImport and pass an instance of Project to it, then a "Select File or Directory to Import" window opens up instead of just opening the project.



If we use ProjectUtil.openOrImport and pass Project's base path to the API, it creates the Project and opens it. However, it doesn't import it automatically, showing only Maven/Gradle related files in Project windows without any sources or resources.

Previously, we just used to use the ImportModuleAction.createFromWizard and pass it an instance of AddModuleWizard (using either a MavenProjectImportProvider or GradleProjectImportProvider). This used to import and open the project correctly by showing an Import Window.

0
Avatar
Permanently deleted user

I am almost there at least for 2019.x versions. Here is what I did, I use the ImportModuleAction.getProviders to create a list of available providers, which I then pass to the ImportModuleAction.createImportWizard to created an instance of AddModuleWizard.

# baseFile is the path to either pom.xml or build.gradle
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(baseFile);
final List<ProjectImportProvider> providers = ImportModuleAction.getProviders(project);
final ProjectImportProvider[] providersArray = providers.toArray(new ProjectImportProvider[0]);
final AddModuleWizard importWizard = ImportModuleAction.createImportWizard(project, null, virtualFile, providersArray);
ImportModuleAction.createFromWizard(project, importWizard);

This works perfectly for Gradle projects and those are opened in a new Window and imported automatically.

However, this still doesn't work for 2020.2. I am guessing that we are doing something wrong with when to import the project.

To give some background, we have a custom JavaModuleBuilder in which we do the following:

1. Create projects by writing project files to the file system
2. Import the project

public void setupRootModel(ModifiableRootModel rootModel) {
    ...
    StartupManager.getInstance(project).runWhenProjectIsInitialized((DumbAwareRunnable) () -> DumbService.getInstance(project).smartInvokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> createProject(project))));
    StartupManager.getInstance(project).registerPostStartupActivity((DumbAwareRunnable) () -> DumbService.getInstance(project).smartInvokeLater(() -> importProject(project)));
}
0

Please, do not use GradleProjectImportProvider, neither JavaGradleProjectImportProvider directly, this classes are for internal use.

 

ProjectUtil.openOrImport is designed to be used for this.

 

The reason your project does not being imported is unclear. ProjectOpenProcessor extensions are responsible for opening and importing projects from maven/gradle and so on, ProjectOpenProcessor  is chosen by IDEA using some heuristics/

What value do you pass into "file" parameter into ProjectUtil.openOrImport? Both directory and project file (e.g. pom.xml) should work. Are there gradle/maven files exist at the moment in that directory?

 

Also, could you check in debugger, what ProjectOpenProcessor.EXTENSION_POINT_NAME.getExtensionList() returns. Are there MavenProjectOpenProcessor and GradleProjectOpenProcessor in that list?

 

0

Please sign in to leave a comment.