Search in sources :

Example 11 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class MvcModuleStructureUtil method modifyDefaultLibrary.

public static Library.ModifiableModel modifyDefaultLibrary(ModifiableRootModel model, String libName) {
    LibraryTable libTable = model.getModuleLibraryTable();
    for (Library library : libTable.getLibraries()) {
        if (library.getName() != null && library.getName().startsWith(libName)) {
            return library.getModifiableModel();
        }
    }
    Library library = LibraryUtil.createLibrary(libTable, libName + " (" + model.getModule().getName() + ')');
    for (OrderEntry entry : model.getOrderEntries()) {
        if (!(entry instanceof LibraryOrderEntry))
            continue;
        LibraryOrderEntry libraryEntry = (LibraryOrderEntry) entry;
        if (libraryEntry.isModuleLevel() && libraryEntry.getLibrary() == library) {
            libraryEntry.setExported(true);
        }
    }
    return library.getModifiableModel();
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library)

Example 12 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class DependenciesImportingTest method testDoNotFailOnAbsentAppLibrary.

public void testDoNotFailOnAbsentAppLibrary() throws Exception {
    importProject("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>");
    ApplicationManager.getApplication().runWriteAction(() -> {
        LibraryTable appTable = LibraryTablesRegistrar.getInstance().getLibraryTable();
        Library lib = appTable.createLibrary("foo");
        ModuleRootModificationUtil.addDependency(getModule("project"), lib);
        appTable.removeLibrary(lib);
    });
    // should not fail;
    importProject();
}
Also used : ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library)

Example 13 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class FacetLibraryConfigurator method fillLibrary.

private static void fillLibrary(Project project, Library lib, List<String> paths) {
    Library.ModifiableModel modifiableModel = lib.getModifiableModel();
    for (String root : lib.getUrls(OrderRootType.CLASSES)) {
        modifiableModel.removeRoot(root, OrderRootType.CLASSES);
    }
    Set<VirtualFile> roots = new HashSet<>();
    ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
    Collections.addAll(roots, rootManager.getContentRoots());
    Collections.addAll(roots, rootManager.getContentSourceRoots());
    if (paths != null) {
        for (String dir : paths) {
            VirtualFile pathEntry = LocalFileSystem.getInstance().findFileByPath(dir);
            if (pathEntry != null && !pathEntry.isDirectory() && pathEntry.getFileType() instanceof ArchiveFileType) {
                pathEntry = JarFileSystem.getInstance().getJarRootForLocalFile(pathEntry);
            }
            // buildout includes source root of project in paths; don't add it as library home
            if (pathEntry != null && roots.contains(pathEntry)) {
                continue;
            }
            if (pathEntry != null) {
                modifiableModel.addRoot(pathEntry, OrderRootType.CLASSES);
            } else {
                modifiableModel.addRoot("file://" + dir, OrderRootType.CLASSES);
            }
        }
    }
    modifiableModel.commit();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) Library(com.intellij.openapi.roots.libraries.Library) HashSet(java.util.HashSet)

Example 14 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class PackagingElementsTestCase method addProjectLibrary.

static Library addProjectLibrary(final Project project, @Nullable final Module module, final String name, final DependencyScope scope, final VirtualFile[] jars) {
    return new WriteAction<Library>() {

        @Override
        protected void run(@NotNull final Result<Library> result) {
            final Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(project).createLibrary(name);
            final Library.ModifiableModel libraryModel = library.getModifiableModel();
            for (VirtualFile jar : jars) {
                libraryModel.addRoot(jar, OrderRootType.CLASSES);
            }
            libraryModel.commit();
            if (module != null) {
                ModuleRootModificationUtil.addDependency(module, library, scope, false);
            }
            result.setResult(library);
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) Library(com.intellij.openapi.roots.libraries.Library) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 15 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class JarFromModulesTemplate method doCreateArtifact.

@Nullable
public NewArtifactConfiguration doCreateArtifact(final Module[] modules, final String mainClassName, final String directoryForManifest, final boolean extractLibrariesToJar, final boolean includeTests) {
    VirtualFile manifestFile = null;
    final Project project = myContext.getProject();
    if (mainClassName != null && !mainClassName.isEmpty() || !extractLibrariesToJar) {
        final VirtualFile directory;
        try {
            directory = ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<VirtualFile, IOException>() {

                @Override
                public VirtualFile compute() throws IOException {
                    return VfsUtil.createDirectoryIfMissing(directoryForManifest);
                }
            });
        } catch (IOException e) {
            LOG.info(e);
            Messages.showErrorDialog(project, "Cannot create directory '" + directoryForManifest + "': " + e.getMessage(), CommonBundle.getErrorTitle());
            return null;
        }
        if (directory == null)
            return null;
        manifestFile = ManifestFileUtil.createManifestFile(directory, project);
        if (manifestFile == null) {
            return null;
        }
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, null, true);
    }
    String name = modules.length == 1 ? modules[0].getName() : project.getName();
    final PackagingElementFactory factory = PackagingElementFactory.getInstance();
    final CompositePackagingElement<?> archive = factory.createArchive(ArtifactUtil.suggestArtifactFileName(name) + ".jar");
    OrderEnumerator orderEnumerator = ProjectRootManager.getInstance(project).orderEntries(Arrays.asList(modules));
    final Set<Library> libraries = new THashSet<>();
    if (!includeTests) {
        orderEnumerator = orderEnumerator.productionOnly();
    }
    final ModulesProvider modulesProvider = myContext.getModulesProvider();
    final OrderEnumerator enumerator = orderEnumerator.using(modulesProvider).withoutSdk().runtimeOnly().recursively();
    enumerator.forEachLibrary(new CommonProcessors.CollectProcessor<>(libraries));
    enumerator.forEachModule(module -> {
        if (ProductionModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createModuleOutput(module));
        }
        if (includeTests && TestModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createTestModuleOutput(module));
        }
        return true;
    });
    final JarArtifactType jarArtifactType = JarArtifactType.getInstance();
    if (manifestFile != null && !manifestFile.equals(ManifestFileUtil.findManifestFile(archive, myContext, jarArtifactType))) {
        archive.addFirstChild(factory.createFileCopyWithParentDirectories(manifestFile.getPath(), ManifestFileUtil.MANIFEST_DIR_NAME));
    }
    final String artifactName = name + ":jar";
    if (extractLibrariesToJar) {
        addExtractedLibrariesToJar(archive, factory, libraries);
        return new NewArtifactConfiguration(archive, artifactName, jarArtifactType);
    } else {
        final ArtifactRootElement<?> root = factory.createArtifactRootElement();
        List<String> classpath = new ArrayList<>();
        root.addOrFindChild(archive);
        addLibraries(libraries, root, archive, classpath);
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, classpath, true);
        return new NewArtifactConfiguration(root, artifactName, PlainArtifactType.getInstance());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Library(com.intellij.openapi.roots.libraries.Library) CommonProcessors(com.intellij.util.CommonProcessors) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)266 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)65 Module (com.intellij.openapi.module.Module)43 NotNull (org.jetbrains.annotations.NotNull)32 File (java.io.File)31 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)28 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)27 Project (com.intellij.openapi.project.Project)25 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)25 Nullable (org.jetbrains.annotations.Nullable)25 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)19 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)12 THashSet (gnu.trove.THashSet)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 Result (com.intellij.openapi.application.Result)7 Logger (com.intellij.openapi.diagnostic.Logger)7