public void addLibraryDependency( MavenArtifact artifact, DependencyScope scope, MavenModifiableModelsProvider provider, MavenProject project) { String libraryName = artifact.getLibraryName(); Library library = provider.getLibraryByName(libraryName); if (library == null) { library = provider.createLibrary(libraryName); } Library.ModifiableModel libraryModel = provider.getLibraryModel(library); updateUrl(libraryModel, OrderRootType.CLASSES, artifact, null, null, true); if (!MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope())) { updateUrl( libraryModel, OrderRootType.SOURCES, artifact, MavenExtraArtifactType.SOURCES, project, false); updateUrl( libraryModel, JavadocOrderRootType.getInstance(), artifact, MavenExtraArtifactType.DOCS, project, false); } LibraryOrderEntry e = myRootModel.addLibraryEntry(library); e.setScope(scope); }
@Nullable public static OrderEntry findLibraryEntry(@NotNull Module m, @NotNull MavenArtifact artifact) { String name = artifact.getLibraryName(); for (OrderEntry each : ModuleRootManager.getInstance(m).getOrderEntries()) { if (each instanceof LibraryOrderEntry && name.equals(((LibraryOrderEntry) each).getLibraryName())) { return each; } } return null; }
private void addAttachArtifactDependency( @NotNull Element buildHelperCfg, @NotNull DependencyScope scope, @NotNull MavenProject mavenProject, @NotNull MavenArtifact artifact) { Library.ModifiableModel libraryModel = null; for (Element artifactsElement : (List<Element>) buildHelperCfg.getChildren("artifacts")) { for (Element artifactElement : (List<Element>) artifactsElement.getChildren("artifact")) { String typeString = artifactElement.getChildTextTrim("type"); if (typeString != null && !typeString.equals("jar")) continue; OrderRootType rootType = OrderRootType.CLASSES; String classifier = artifactElement.getChildTextTrim("classifier"); if ("sources".equals(classifier)) { rootType = OrderRootType.SOURCES; } else if ("javadoc".equals(classifier)) { rootType = JavadocOrderRootType.getInstance(); } String filePath = artifactElement.getChildTextTrim("file"); if (StringUtil.isEmpty(filePath)) continue; VirtualFile file = VfsUtil.findRelativeFile(filePath, mavenProject.getDirectoryFile()); if (file == null) continue; file = JarFileSystem.getInstance().getJarRootForLocalFile(file); if (file == null) continue; if (libraryModel == null) { String libraryName = artifact.getLibraryName(); assert libraryName.startsWith(MavenArtifact.MAVEN_LIB_PREFIX); libraryName = MavenArtifact.MAVEN_LIB_PREFIX + "ATTACHED-JAR: " + libraryName.substring(MavenArtifact.MAVEN_LIB_PREFIX.length()); Library library = myModifiableModelsProvider.getLibraryByName(libraryName); if (library == null) { library = myModifiableModelsProvider.createLibrary(libraryName); } libraryModel = myModifiableModelsProvider.getLibraryModel(library); LibraryOrderEntry entry = myRootModelAdapter.getRootModel().addLibraryEntry(library); entry.setScope(scope); } libraryModel.addRoot(file, rootType); } } }
@Nullable public static MavenArtifact findArtifact( @NotNull MavenProject project, @Nullable Library library) { if (library == null) return null; String name = library.getName(); if (!MavenArtifact.isMavenLibrary(name)) return null; for (MavenArtifact each : project.getDependencies()) { if (each.getLibraryName().equals(name)) return each; } return null; }
public Library findLibrary(@NotNull final MavenArtifact artifact) { final String name = artifact.getLibraryName(); final Ref<Library> result = Ref.create(null); myRootModel .orderEntries() .forEachLibrary( new Processor<Library>() { @Override public boolean process(Library library) { if (name.equals(library.getName())) { result.set(library); } return true; } }); return result.get(); }
@Deprecated // Use artifact.getLibraryName(); public static String makeLibraryName(@NotNull MavenArtifact artifact) { return artifact.getLibraryName(); }