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();
}
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();
}
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();
}
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();
}
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());
}
}
Aggregations