From a27c3119feea61a3b607d637296c439ca38fdde6 Mon Sep 17 00:00:00 2001 From: "Anton.Makeev" Date: Tue, 17 Nov 2009 15:47:25 +0300 Subject: [PATCH] Maven: do not populate system-scope dependency root entries on every import (IDEADEV-41396) --- .../importing/MavenRootModelAdapter.java | 7 +- .../idea/maven/project/MavenArtifact.java | 5 +- .../idea/maven/MavenImportingTestCase.java | 2 + .../importing/DependenciesImportingTest.java | 106 +++++++++++++++++- 4 files changed, 113 insertions(+), 7 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenRootModelAdapter.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenRootModelAdapter.java index 1686869dc9ca..c29ee7211d31 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenRootModelAdapter.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/importing/MavenRootModelAdapter.java @@ -240,13 +240,14 @@ public class MavenRootModelAdapter { OrderRootType type, MavenArtifact artifact, String classifier) { + String newUrl = artifact.getUrlForClassifier(classifier); for (String url : libraryModel.getUrls(type)) { - if (isRepositoryUrl(artifact, url, classifier)) { + if (newUrl.equals(url)) return; + if (MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope()) || isRepositoryUrl(artifact, url, classifier)) { libraryModel.removeRoot(url, type); } } - - libraryModel.addRoot(artifact.getUrlForClassifier(classifier), type); + libraryModel.addRoot(newUrl, type); } private boolean isRepositoryUrl(MavenArtifact artifact, String url, String classifier) { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenArtifact.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenArtifact.java index 046c325bf2c6..0c047302dd7c 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenArtifact.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenArtifact.java @@ -24,6 +24,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.handler.ArtifactHandler; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.maven.embedder.CustomArtifact; import static org.jetbrains.idea.maven.project.MavenId.append; @@ -134,10 +135,10 @@ public class MavenArtifact implements Serializable { } public boolean isResolved() { - return myResolved && myFile != null && myFile.exists() && !myStubbed; + return myResolved && myFile.exists() && !myStubbed; } - @Nullable + @NotNull public File getFile() { return myFile; } diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/MavenImportingTestCase.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/MavenImportingTestCase.java index 4177a87dbbdd..f663b620e208 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/MavenImportingTestCase.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/MavenImportingTestCase.java @@ -187,6 +187,8 @@ public abstract class MavenImportingTestCase extends MavenTestCase { private void assertModuleLibDepPath(LibraryOrderEntry lib, OrderRootType type, List paths) { if (paths == null) return; assertUnorderedElementsAreEqual(lib.getUrls(type), ArrayUtil.toStringArray(paths)); + // also check the library because it may contain slight different set of urls (e.g. with duplicates) + assertUnorderedElementsAreEqual(lib.getLibrary().getUrls(type), ArrayUtil.toStringArray(paths)); } protected void assertModuleLibDepScope(String moduleName, String depName, DependencyScope scope) { diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/DependenciesImportingTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/DependenciesImportingTest.java index 6a484519697d..5a7a2db0dd9c 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/DependenciesImportingTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/importing/DependenciesImportingTest.java @@ -1221,9 +1221,8 @@ public class DependenciesImportingTest extends MavenImportingTestCase { ModifiableRootModel rootModel = ModuleRootManager.getInstance(getModule("project")).getModifiableModel(); LibraryTable.ModifiableModel tableModel = rootModel.getModuleLibraryTable().getModifiableModel(); - Library lib = tableModel.createLibrary("junit:junit:4.0"); + tableModel.createLibrary("junit:junit:4.0"); tableModel.commit(); - //rootModel.addLibraryEntry(lib); rootModel.commit(); assertModuleLibDeps("project", "junit:junit:4.0"); @@ -1405,6 +1404,109 @@ public class DependenciesImportingTest extends MavenImportingTestCase { "jar://" + getRepositoryPath() + "/org/testng/testng/5.8/testng-5.8-jdk15-javadoc.jar!/"); } + public void testDoNotPopulateSameRootEntriesOnEveryImport() throws Exception { + importProject("test" + + "project" + + "1" + + + "" + + " " + + " junit" + + " junit" + + " 4.0" + + " " + + ""); + + assertModuleLibDep("project", "Maven: junit:junit:4.0", + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0.jar!/"), + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar!/"), + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/")); + + scheduleResolveAll(); + resolveDependenciesAndImport(); + scheduleResolveAll(); + resolveDependenciesAndImport(); + + assertModuleLibDep("project", "Maven: junit:junit:4.0", + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0.jar!/"), + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar!/"), + Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/")); + } + + public void testDoNotPopulateSameRootEntriesOnEveryImportForSystemLibraries() throws Exception { + importProject("test" + + "project" + + "1" + + + "" + + " " + + " xxx" + + " yyy" + + " 1" + + " system" + + " c:/foo/bar.jar" + + " " + + ""); + + assertModuleLibDep("project", "Maven: xxx:yyy:1", + Arrays.asList("jar://c:/foo/bar.jar!/"), + Arrays.asList("jar://c:/foo/bar-sources.jar!/"), + Arrays.asList("jar://c:/foo/bar-javadoc.jar!/")); + + scheduleResolveAll(); + resolveDependenciesAndImport(); + scheduleResolveAll(); + resolveDependenciesAndImport(); + + assertModuleLibDep("project", "Maven: xxx:yyy:1", + Arrays.asList("jar://c:/foo/bar.jar!/"), + Arrays.asList("jar://c:/foo/bar-sources.jar!/"), + Arrays.asList("jar://c:/foo/bar-javadoc.jar!/")); + } + + public void testRemovingPreviousSystemPathForForSystemLibraries() throws Exception { + importProject("test" + + "project" + + "1" + + + "" + + " " + + " xxx" + + " yyy" + + " 1" + + " system" + + " c:/foo/bar.jar" + + " " + + ""); + + assertModuleLibDep("project", "Maven: xxx:yyy:1", + Arrays.asList("jar://c:/foo/bar.jar!/"), + Arrays.asList("jar://c:/foo/bar-sources.jar!/"), + Arrays.asList("jar://c:/foo/bar-javadoc.jar!/")); + + createProjectPom("test" + + "project" + + "1" + + + "" + + " " + + " xxx" + + " yyy" + + " 1" + + " system" + + " c:/foo/xxx.jar" + + " " + + ""); + + scheduleResolveAll(); + resolveDependenciesAndImport(); + + assertModuleLibDep("project", "Maven: xxx:yyy:1", + Arrays.asList("jar://c:/foo/xxx.jar!/"), + Arrays.asList("jar://c:/foo/xxx-sources.jar!/"), + Arrays.asList("jar://c:/foo/xxx-javadoc.jar!/")); + } + public void testRemovingUnusedLibraries() throws Exception { createProjectPom("test" + "project" +