From 3e08f46f230b53adc167137f640babe779c357e4 Mon Sep 17 00:00:00 2001 From: Alexander Doroshko Date: Fri, 20 Jan 2012 20:23:11 +0400 Subject: [PATCH] Maven importer should be able to create module of any type --- .../idea/maven/importing/MavenImporter.java | 8 +++ .../maven/importing/MavenProjectImporter.java | 66 +++++++++++++------ .../idea/maven/project/MavenProject.java | 17 +++++ .../project/MavenProjectsManagerWatcher.java | 9 +++ .../main/resources/ProjectBundle.properties | 9 ++- 5 files changed, 83 insertions(+), 26 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenImporter.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenImporter.java index 2e3b6dd3f1ba..b46464c7fd95 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenImporter.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenImporter.java @@ -17,9 +17,12 @@ package org.jetbrains.idea.maven.importing; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.module.StdModuleTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import org.jdom.Element; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.maven.model.MavenArtifact; import org.jetbrains.idea.maven.project.*; @@ -57,6 +60,11 @@ public abstract class MavenImporter { return mavenProject.findPlugin(myPluginGroupID, myPluginArtifactID) != null; } + @NotNull + public ModuleType getModuleType() { + return StdModuleTypes.JAVA; + } + public void getSupportedPackagings(Collection result) { } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenProjectImporter.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenProjectImporter.java index 32c3051362cf..af639dae5c95 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenProjectImporter.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenProjectImporter.java @@ -176,50 +176,74 @@ public class MavenProjectImporter { } private boolean deleteIncompatibleModules() { - final List> incompatible = collectIncompatibleModulesWithProjects(); - if (incompatible.isEmpty()) return false; + final Pair>, List>> incompatible = collectIncompatibleModulesWithProjects(); + final List> incompatibleMavenized = incompatible.first; + final List> incompatibleNotMavenized = incompatible.second; + + if (incompatibleMavenized.isEmpty() && incompatibleNotMavenized.isEmpty()) return false; + + boolean changed = false; + + // For already mavenized modules the type may change because maven project plugins were resolved and MavenImporter asked to create a module of a different type. + // In such cases we must change module type silently. + for (Pair each : incompatibleMavenized) { + myFileToModuleMapping.remove(each.first.getFile()); + myModuleModel.disposeModule(each.second); + changed |= true; + } + + if (incompatibleNotMavenized.isEmpty()) return changed; final int[] result = new int[1]; MavenUtil.invokeAndWait(myProject, myModelsProvider.getModalityStateForQuestionDialogs(), new Runnable() { public void run() { String message = ProjectBundle.message("maven.import.incompatible.modules", - formatProjectsWithModules(incompatible), - incompatible.size() == 1 ? "" : "s"); + incompatibleNotMavenized.size(), + formatProjectsWithModules(incompatibleNotMavenized)); String[] options = { ProjectBundle.message("maven.import.incompatible.modules.recreate"), ProjectBundle.message("maven.import.incompatible.modules.ignore") }; result[0] = Messages.showOkCancelDialog(myProject, message, - ProjectBundle.message("maven.tab.importing"), - options[0], options[1], Messages.getQuestionIcon()); + ProjectBundle.message("maven.project.import.title"), + options[0], options[1], Messages.getQuestionIcon()); } }); if (result[0] == 0) { - for (Pair each : incompatible) { + for (Pair each : incompatibleNotMavenized) { myFileToModuleMapping.remove(each.first.getFile()); myModuleModel.disposeModule(each.second); } - return true; + changed |= true; } else { - myProjectsTree.setIgnoredState(MavenUtil.collectFirsts(incompatible), true, true); - return false; + myProjectsTree.setIgnoredState(MavenUtil.collectFirsts(incompatibleNotMavenized), true, true); + changed |= false; } + + return changed; } - private List> collectIncompatibleModulesWithProjects() { - List> incompatible = new ArrayList>(); + /** + * Collects modules that need to change module type + * @return the first List in returned Pair contains already mavenized modules, the second List - not mavenized + */ + private Pair>, List>> collectIncompatibleModulesWithProjects() { + List> incompatibleMavenized = new ArrayList>(); + List> incompatibleNotMavenized = new ArrayList>(); + + MavenProjectsManager manager = MavenProjectsManager.getInstance(myProject); for (MavenProject each : myAllProjects) { Module module = myFileToModuleMapping.get(each.getFile()); if (module == null) continue; - if (shouldCreateModuleFor(each) && !(ModuleType.get(module) instanceof JavaModuleType)) { - incompatible.add(Pair.create(each, module)); + if (shouldCreateModuleFor(each) && !(ModuleType.get(module).equals(each.getModuleType()))) { + (manager.isMavenizedModule(module) ? incompatibleMavenized : incompatibleNotMavenized).add(Pair.create(each, module)); } } - return incompatible; + return Pair.create(incompatibleMavenized, incompatibleNotMavenized); } private static String formatProjectsWithModules(List> projectsWithModules) { @@ -230,9 +254,8 @@ public class MavenProjectImporter { return ModuleType.get(module).getName() + " '" + module.getName() + - "' for Maven project '" + - project.getMavenId().getDisplayString() + - "'"; + "' for Maven project " + + project.getMavenId().getDisplayString(); } }, "
"); } @@ -248,7 +271,7 @@ public class MavenProjectImporter { public void run() { result[0] = Messages.showYesNoDialog(myProject, ProjectBundle.message("maven.import.message.delete.obsolete", formatModules(obsoleteModules)), - ProjectBundle.message("maven.tab.importing"), + ProjectBundle.message("maven.project.import.title"), Messages.getQuestionIcon()); } }); @@ -271,8 +294,9 @@ public class MavenProjectImporter { } List obsolete = new ArrayList(); + final MavenProjectsManager manager = MavenProjectsManager.getInstance(myProject); for (Module each : remainingModules) { - if (MavenProjectsManager.getInstance(myProject).isMavenizedModule(each)) { + if (manager.isMavenizedModule(each)) { obsolete.add(each); } } @@ -440,7 +464,7 @@ public class MavenProjectImporter { // have to remove it beforehand. deleteExistingImlFile(path); - final Module module = myModuleModel.newModule(path, StdModuleTypes.JAVA); + final Module module = myModuleModel.newModule(path, project.getModuleType()); myMavenProjectToModule.put(project, module); myCreatedModules.add(module); return true; diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java index 69f03c707e9e..cb5a81b73cdd 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProject.java @@ -15,6 +15,8 @@ */ package org.jetbrains.idea.maven.project; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.module.StdModuleTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Condition; @@ -796,6 +798,21 @@ public class MavenProject { return MavenImporter.getSuitableImporters(this); } + @NotNull + public ModuleType getModuleType() { + ModuleType typeFromImporter = null; + for (MavenImporter each : getSuitableImporters()) { + final ModuleType moduleType = each.getModuleType(); + if (typeFromImporter != null && !typeFromImporter.equals(moduleType)) { + MavenLog.LOG.error("Incompatible plugins: " + each.getClass().getName() + " wants to create " + + moduleType.getName() + " for project " + getName() + " whereas some other importer requires " + + typeFromImporter.getName()); + } + typeFromImporter = moduleType; + } + return typeFromImporter != null ? typeFromImporter : StdModuleTypes.JAVA; + } + @NotNull public Pair getClassifierAndExtension(@NotNull MavenArtifact artifact, @NotNull MavenExtraArtifactType type) { for (MavenImporter each : getSuitableImporters()) { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsManagerWatcher.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsManagerWatcher.java index 13ce80de07a1..4e57e68faecf 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsManagerWatcher.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectsManagerWatcher.java @@ -111,6 +111,15 @@ public class MavenProjectsManagerWatcher { MavenProject mavenProject = myManager.findProject(module); if (mavenProject != null) myManager.setIgnoredState(Collections.singletonList(mavenProject), true); } + + public void moduleAdded(final Project project, final Module module) { + // this method is needed to return non-ignored status for modules that were deleted (and thus ignored) and then created again with a different module type + if (myManager.isMavenizedModule(module)) { + MavenProject mavenProject = myManager.findProject(module); + if (mavenProject != null) myManager.setIgnoredState(Collections.singletonList(mavenProject), false); + } + + } }); DocumentAdapter myDocumentListener = new DocumentAdapter() { diff --git a/plugins/maven/src/main/resources/ProjectBundle.properties b/plugins/maven/src/main/resources/ProjectBundle.properties index bfe49297fd4f..d5d6c4c9b4bb 100644 --- a/plugins/maven/src/main/resources/ProjectBundle.properties +++ b/plugins/maven/src/main/resources/ProjectBundle.properties @@ -24,6 +24,7 @@ maven.project.changed=Maven projects need to be imported maven.project.importChanged=Import Changes maven.project.enableAutoImport=Enable Auto-Import maven.project.importing=Importing Maven projects +maven.project.import.title=Import Maven Projects maven.project.importing.finished=Finished importing Maven projects maven.project.import.waiting.for.active.process=Waiting for an active process maven.post.processing=Configuring Maven projects @@ -37,11 +38,9 @@ maven.import.environment.settings.title=Maven environment maven.import.title.module.dir=Select directory for IDEA module files (*.iml) maven.import.incompatible.modules=\ - Maven import needs to recreate
{0}

\ - Maven Integration supports only Java modules. Your modules will be recreated as
\ - Java modules and configured according to settings in your pom.xml files.
\ - If you do not want the modules to be recreated, corresponding Maven projects will be marked as
\ - ''Ignored'' and you can unignore them afterwards in the Maven Projects tool window. + The following {0,choice,1#module has|2#modules have} incorrect type and {0,choice,1#needs|2#need} to be recreated:

{1}

\ + If you do not want the {0,choice,1#module|2#modules} to be recreated, corresponding Maven projects will be marked as ignored,
\ + you will be able to unignore them afterwards in the Maven Projects tool window. maven.import.incompatible.modules.recreate=Recreate maven.import.incompatible.modules.ignore=Ignore These Projects