NewArtifactConfiguration (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( (ThrowableComputable<VirtualFile, IOException>)() -> VfsUtil.createDirectoryIfMissing(directoryForManifest)); } catch (IOException e) { LOG.info(e); Messages.showErrorDialog(project, JavaCompilerBundle.message("cannot.create.directory.0.1", 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 HashSet<>(); 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()); } }