From ae2859983375b33f6615ec2c81c0d516b8a5e129 Mon Sep 17 00:00:00 2001 From: Anton Makeev Date: Thu, 30 Jun 2022 16:07:33 +0200 Subject: [PATCH] [maven] Workspace import: replace content of pre-existing modules when importing them as Maven modules (IDEA-288545) GitOrigin-RevId: 473b6d9a0ffce08ea8034da4da53d4c0d6e027c0 --- .../MavenProjectImporterToWorkspace.kt | 17 +++++- .../maven/importing/FoldersImportingTest.java | 6 +-- .../maven/importing/MiscImportingTest.java | 3 -- .../importing/StructureImportingTest.java | 52 +++++++++++++++++++ .../testFramework/MavenImportingTestCase.java | 6 +-- 5 files changed, 71 insertions(+), 13 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/workspaceModel/MavenProjectImporterToWorkspace.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/workspaceModel/MavenProjectImporterToWorkspace.kt index 558f5d21c522..c8e18e20e2c9 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/workspaceModel/MavenProjectImporterToWorkspace.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/workspaceModel/MavenProjectImporterToWorkspace.kt @@ -10,8 +10,10 @@ import com.intellij.workspaceModel.ide.JpsImportedEntitySource import com.intellij.workspaceModel.ide.WorkspaceModel import com.intellij.workspaceModel.ide.getInstance import com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModuleManagerBridgeImpl.Companion.findModuleByEntity +import com.intellij.workspaceModel.storage.EntitySource import com.intellij.workspaceModel.storage.MutableEntityStorage import com.intellij.workspaceModel.storage.bridgeEntities.api.ExternalSystemModuleOptionsEntity +import com.intellij.workspaceModel.storage.bridgeEntities.api.ModuleEntity import com.intellij.workspaceModel.storage.bridgeEntities.api.ModuleId import com.intellij.workspaceModel.storage.url.VirtualFileUrlManager import org.jetbrains.idea.maven.importing.MavenModelUtil @@ -137,8 +139,16 @@ class MavenProjectImporterToWorkspace( val importModuleData = mutableListOf() MavenUtil.invokeAndWaitWriteAction(myProject) { WorkspaceModel.getInstance(myProject).updateProjectModel { current -> - current.replaceBySource( - { (it as? JpsImportedEntitySource)?.externalSystemId == WorkspaceModuleImporter.EXTERNAL_SOURCE_ID }, builder) + // remove modules which should be replaced with Maven modules, in order to clean them from pre-existing sources, dependencies etc. + // It's needed since otherwise 'replaceBySource' will merge pre-existing Module content with imported module content, resulting in + // unexpected module configuration. + val importedModuleNames = createdModules.mapTo(mutableSetOf()) { it.moduleId.name } + current + .entities(ModuleEntity::class.java) + .filter { !isMainEntity(it.entitySource) && it.name in importedModuleNames } + .forEach { current.removeEntity(it) } + + current.replaceBySource({ isMainEntity(it) }, builder) } val storage = WorkspaceModel.getInstance(myProject).entityStorage.current for ((moduleId, mavenProject, moduleType) in createdModules) { @@ -154,6 +164,9 @@ class MavenProjectImporterToWorkspace( return importModuleData } + private fun isMainEntity(it: EntitySource) = + (it as? JpsImportedEntitySource)?.externalSystemId == WorkspaceModuleImporter.EXTERNAL_SOURCE_ID + private fun finalizeImport(modules: List, moduleNameByProject: Map, projectChanges: Map, diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/FoldersImportingTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/FoldersImportingTest.java index 3386307f49e7..4c7ccbd126cf 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/FoldersImportingTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/FoldersImportingTest.java @@ -217,7 +217,7 @@ public class FoldersImportingTest extends MavenMultiVersionImportingTestCase { ""); resolveFoldersAndImport(); - if (supportsKeepingFoldersFromPreviousImport()) { + if (supportsLegacyKeepingFoldersFromPreviousImport()) { assertSources("project", "src2", "src1"); } else { @@ -1560,9 +1560,9 @@ public class FoldersImportingTest extends MavenMultiVersionImportingTestCase { testAssertions.accept(true); importProject(); - testAssertions.accept(supportsKeepingFoldersFromPreviousImport()); + testAssertions.accept(supportsLegacyKeepingFoldersFromPreviousImport()); resolveFoldersAndImport(); - testAssertions.accept(supportsKeepingFoldersFromPreviousImport()); + testAssertions.accept(supportsLegacyKeepingFoldersFromPreviousImport()); } @Test diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/MiscImportingTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/MiscImportingTest.java index c7452fa41e71..f0875f51e75d 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/MiscImportingTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/MiscImportingTest.java @@ -19,7 +19,6 @@ import org.jetbrains.idea.maven.MavenCustomRepositoryHelper; import org.jetbrains.idea.maven.model.MavenId; import org.jetbrains.idea.maven.project.MavenProject; import org.jetbrains.idea.maven.server.MavenServerManager; -import org.junit.Assume; import org.junit.Test; import java.io.File; @@ -101,8 +100,6 @@ public class MiscImportingTest extends MavenMultiVersionImportingTestCase { @Test public void testImportingAllAvailableFilesIfNotInitialized() { - Assume.assumeTrue(supportsSeveralProjectsInSameFolders()); - createModule("m1"); createModule("m2"); createProjectSubDirs("m1/src/main/java", diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/StructureImportingTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/StructureImportingTest.java index 6f08b0a56bee..1b5d86f5d193 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/StructureImportingTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/StructureImportingTest.java @@ -26,6 +26,7 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.java.LanguageLevel; import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.PsiTestUtil; import org.jetbrains.idea.maven.project.MavenGeneralSettings; import org.jetbrains.idea.maven.project.MavenProject; import org.junit.Assume; @@ -65,6 +66,57 @@ public class StructureImportingTest extends MavenMultiVersionImportingTestCase { } } + @Test + public void testImportWithAlreadyExistingModules() throws IOException { + createModule("m1"); + createModule("m2"); + createModule("m3"); + + PsiTestUtil.addSourceRoot(getModule("m1"), createProjectSubFile("m1/user-sources")); + PsiTestUtil.addSourceRoot(getModule("m2"), createProjectSubFile("m2/user-sources")); + PsiTestUtil.addSourceRoot(getModule("m3"), createProjectSubFile("m3/user-sources")); + + assertModules("m1", "m2", "m3"); + assertSources("m1", "user-sources"); + assertSources("m2", "user-sources"); + assertSources("m3", "user-sources"); + + createProjectPom("test" + + "project" + + "pom" + + "1" + + + "" + + " m1" + + " m2" + + ""); + + createModulePom("m1", "test" + + "m1" + + "1"); + createModulePom("m2", "test" + + "m2" + + "1"); + + createProjectSubDirs("m1/src/main/java", + "m2/src/main/java", + "m3/src/main/java"); + + importProject(); + assertModules("project", "m1", "m2", "m3"); + + if (supportsLegacyKeepingFoldersFromPreviousImport()) { + assertSources("m1", "user-sources", "src/main/java"); + assertSources("m2", "user-sources", "src/main/java"); + assertSources("m3", "user-sources"); + } + else { + assertSources("m1", "src/main/java"); + assertSources("m2", "src/main/java"); + assertSources("m3", "user-sources"); + } + } + @Test public void testMarkModulesAsMavenized() { createModule("userModule"); diff --git a/plugins/maven/testFramework/src/com/intellij/maven/testFramework/MavenImportingTestCase.java b/plugins/maven/testFramework/src/com/intellij/maven/testFramework/MavenImportingTestCase.java index d9381f9138b8..c903f5fe9695 100644 --- a/plugins/maven/testFramework/src/com/intellij/maven/testFramework/MavenImportingTestCase.java +++ b/plugins/maven/testFramework/src/com/intellij/maven/testFramework/MavenImportingTestCase.java @@ -121,10 +121,6 @@ public abstract class MavenImportingTestCase extends MavenTestCase { ); } - public boolean supportsSeveralProjectsInSameFolders() { - return !MavenProjectImporter.isImportToWorkspaceModelEnabled(); // TODO should be supported! - } - public boolean supportModuleGroups() { return !MavenProjectImporter.isImportToWorkspaceModelEnabled() && !MavenProjectImporter.isImportToTreeStructureEnabled(myProject); @@ -139,7 +135,7 @@ public abstract class MavenImportingTestCase extends MavenTestCase { && !MavenProjectImporter.isImportToTreeStructureEnabled(myProject); } - public boolean supportsKeepingFoldersFromPreviousImport() { + public boolean supportsLegacyKeepingFoldersFromPreviousImport() { return !MavenProjectImporter.isImportToWorkspaceModelEnabled(); }