if (library == null) {
library = provider.createLibrary(libraryName);
}
-
Library.ModifiableModel libraryModel = provider.getLibraryModel(library);
- setUrl(libraryModel, OrderRootType.CLASSES, artifact, null);
- setUrl(libraryModel, OrderRootType.SOURCES, artifact, MavenConstants.CLASSIFIER_SOURCES);
- setUrl(libraryModel, JavadocOrderRootType.getInstance(), artifact, MavenConstants.CLASSIFIER_JAVADOC);
+ updateUrl(libraryModel, OrderRootType.CLASSES, artifact, null, true);
+ if (!MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope())) {
+ updateUrl(libraryModel, OrderRootType.SOURCES, artifact, MavenConstants.CLASSIFIER_SOURCES, false);
+ updateUrl(libraryModel, JavadocOrderRootType.getInstance(), artifact, MavenConstants.CLASSIFIER_JAVADOC, false);
+ }
LibraryOrderEntry e = myRootModel.addLibraryEntry(library);
e.setExported(isExportable);
e.setScope(scope);
-
- removeOldLibraryDependency(artifact);
}
- private void setUrl(Library.ModifiableModel libraryModel,
- OrderRootType type,
- MavenArtifact artifact,
- String classifier) {
+ private void updateUrl(Library.ModifiableModel library, OrderRootType type, MavenArtifact artifact, String classifier, boolean clearAll) {
String newUrl = artifact.getUrlForExtraArtifact(classifier);
- for (String url : libraryModel.getUrls(type)) {
+ for (String url : library.getUrls(type)) {
if (newUrl.equals(url)) return;
- if (MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope()) || isRepositoryUrl(artifact, url, classifier)) {
- libraryModel.removeRoot(url, type);
+ if (clearAll || isRepositoryUrl(artifact, url, classifier)) {
+ library.removeRoot(url, type);
}
}
- libraryModel.addRoot(newUrl, type);
+ library.addRoot(newUrl, type);
}
private boolean isRepositoryUrl(MavenArtifact artifact, String url, String classifier) {
if (dotPos == -1) return true;
String pathToJar = classes.substring(0, dotPos);
- String[] sources = library.getUrls(OrderRootType.SOURCES);
- if (sources.length != 1 || !FileUtil.startsWith(sources[0], pathToJar)) return true;
-
- String[] javadoc = library.getUrls(JavadocOrderRootType.getInstance());
- if (javadoc.length != 1 || !FileUtil.startsWith(javadoc[0], pathToJar)) return true;
+ if (hasUserPaths(OrderRootType.SOURCES, library, pathToJar)) return true;
+ if (hasUserPaths(JavadocOrderRootType.getInstance(), library, pathToJar)) return true;
return false;
}
- private void removeOldLibraryDependency(MavenArtifact artifact) {
- Library lib = findLibrary(artifact, false);
- if (lib == null) return;
- LibraryOrderEntry entry = myRootModel.findLibraryOrderEntry(lib);
- if (entry == null) return;
-
- myRootModel.removeOrderEntry(entry);
- }
-
- public Library findLibrary(MavenArtifact artifact) {
- return findLibrary(artifact, true);
+ private static boolean hasUserPaths(OrderRootType rootType, Library library, String pathToJar) {
+ String[] sources = library.getUrls(rootType);
+ for (String each : sources) {
+ if (!FileUtil.startsWith(each, pathToJar)) return true;
+ }
+ return false;
}
- private Library findLibrary(final MavenArtifact artifact, final boolean newType) {
+ public Library findLibrary(final MavenArtifact artifact) {
return myRootModel.processOrder(new RootPolicy<Library>() {
@Override
public Library visitLibraryOrderEntry(LibraryOrderEntry e, Library result) {
- String name = newType ? makeLibraryName(artifact) : artifact.getDisplayStringForLibraryName();
+ String name = makeLibraryName(artifact);
return name.equals(e.getLibraryName()) ? e.getLibrary() : result;
}
}, null);
import java.io.File;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
public class DependenciesImportingTest extends MavenImportingTestCase {
assertModules("project");
assertModuleLibDep("project", "Maven: junit:junit:4.0",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0.jar!/",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar!/",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/");
+ Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0.jar!/"),
+ Collections.<String>emptyList(), Collections.<String>emptyList());
}
public void testTestJarDependencies() throws Exception {
"</dependencies>");
assertModules("project");
assertModuleLibDep("project", "Maven: junit:junit:bar:4.0",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-bar.jar!/",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar!/",
- "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/");
+ "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-bar.jar!/",
+ "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar!/",
+ "jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/");
}
public void testSystemDependencyWithoutPath() throws Exception {
assertModuleLibDeps("project", "My Library", "Maven: junit:junit:4.0");
}
- public void testRemoveOldTypeLibraryDependencies() throws Exception {
- importProject("<groupId>test</groupId>" +
- "<artifactId>project</artifactId>" +
- "<version>1</version>");
-
- ModifiableRootModel rootModel = ModuleRootManager.getInstance(getModule("project")).getModifiableModel();
- LibraryTable.ModifiableModel tableModel = rootModel.getModuleLibraryTable().getModifiableModel();
- tableModel.createLibrary("junit:junit:4.0");
- tableModel.commit();
- rootModel.commit();
-
- assertModuleLibDeps("project", "junit:junit:4.0");
-
- createProjectPom("<groupId>test</groupId>" +
- "<artifactId>project</artifactId>" +
- "<version>1</version>" +
-
- "<dependencies>" +
- " <dependency>" +
- " <groupId>junit</groupId>" +
- " <artifactId>junit</artifactId>" +
- " <version>4.0</version>" +
- " </dependency>" +
- "</dependencies>");
- importProject();
-
- assertModuleLibDeps("project", "Maven: junit:junit:4.0");
- }
-
public void testDoNotResetUserModuleDependencies() throws Exception {
VirtualFile m1 = createModulePom("m1",
"<groupId>test</groupId>" +
Arrays.asList("jar://" + getRepositoryPath() + "/junit/junit/4.0/junit-4.0-javadoc.jar!/", "file://foo.javadoc"));
}
+ public void testDoNotResetCustomRootForSystemLibraries() throws Exception {
+ importProject("<groupId>test</groupId>" +
+ "<artifactId>project</artifactId>" +
+ "<version>1</version>" +
+
+ "<dependencies>" +
+ " <dependency>" +
+ " <groupId>xxx</groupId>" +
+ " <artifactId>yyy</artifactId>" +
+ " <version>1</version>" +
+ " <scope>system</scope>" +
+ " <systemPath>c:/foo/foo.jar</systemPath>" +
+ " </dependency>" +
+ "</dependencies>");
+
+ assertProjectLibraries("Maven: xxx:yyy:1");
+ assertModuleLibDeps("project", "Maven: xxx:yyy:1");
+
+ // add sources and javadoc near the jar, just like in the local repository
+ addLibraryRoot("Maven: xxx:yyy:1", OrderRootType.SOURCES, "jar://c:/foo/foo-sources.jar!/");
+ addLibraryRoot("Maven: xxx:yyy:1", JavadocOrderRootType.getInstance(), "jar://c:/foo/foo-javadoc.jar!/");
+
+ assertModuleLibDep("project", "Maven: xxx:yyy:1",
+ Arrays.asList("jar://c:/foo/foo.jar!/"),
+ Arrays.asList("jar://c:/foo/foo-sources.jar!/"),
+ Arrays.asList("jar://c:/foo/foo-javadoc.jar!/"));
+
+ createProjectPom("<groupId>test</groupId>" +
+ "<artifactId>project</artifactId>" +
+ "<version>1</version>" +
+
+ "<dependencies>" +
+ " <dependency>" +
+ " <groupId>xxx</groupId>" +
+ " <artifactId>yyy</artifactId>" +
+ " <version>1</version>" +
+ " <scope>system</scope>" +
+ " <systemPath>c:/bar/bar.jar</systemPath>" +
+ " </dependency>" +
+ "</dependencies>");
+ scheduleResolveAll();
+ resolveDependenciesAndImport();
+
+ assertModuleLibDep("project", "Maven: xxx:yyy:1",
+ Arrays.asList("jar://c:/bar/bar.jar!/"),
+ Arrays.asList("jar://c:/foo/foo-sources.jar!/"),
+ Arrays.asList("jar://c:/foo/foo-javadoc.jar!/"));
+ }
+
public void testUpdateRootEntriesWithActualPath() throws Exception {
importProject("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
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!/"));
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList());
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!/"));
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList());
}
public void testRemovingPreviousSystemPathForForSystemLibraries() throws Exception {
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!/"));
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList());
createProjectPom("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
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!/"));
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList());
}
public void testRemovingUnusedLibraries() throws Exception {