Search in sources :

Example 1 with MavenProgressIndicator

use of org.jetbrains.idea.maven.utils.MavenProgressIndicator in project intellij-community by JetBrains.

the class MavenProjectImporter method scheduleRefreshResolvedArtifacts.

private void scheduleRefreshResolvedArtifacts(List<MavenProjectsProcessorTask> postTasks) {
    // We have to refresh all the resolved artifacts manually in order to
    // update all the VirtualFilePointers. It is not enough to call
    // VirtualFileManager.refresh() since the newly created files will be only
    // picked by FS when FileWatcher finishes its work. And in the case of import
    // it doesn't finish in time.
    // I couldn't manage to write a test for this since behaviour of VirtualFileManager
    // and FileWatcher differs from real-life execution.
    List<MavenArtifact> artifacts = new ArrayList<>();
    for (MavenProject each : myProjectsToImportWithChanges.keySet()) {
        artifacts.addAll(each.getDependencies());
    }
    final Set<File> files = new THashSet<>();
    for (MavenArtifact each : artifacts) {
        if (each.isResolved())
            files.add(each.getFile());
    }
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        doRefreshFiles(files);
    } else {
        postTasks.add(new MavenProjectsProcessorTask() {

            public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
                indicator.setText("Refreshing files...");
                doRefreshFiles(files);
            }
        });
    }
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) MavenArtifact(org.jetbrains.idea.maven.model.MavenArtifact) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 2 with MavenProgressIndicator

use of org.jetbrains.idea.maven.utils.MavenProgressIndicator in project android by JetBrains.

the class AndroidFacetImporterBase method importExternalAarDependency.

private static void importExternalAarDependency(@NotNull MavenArtifact artifact, @NotNull MavenProject mavenProject, @NotNull MavenProjectsTree mavenTree, @NotNull MavenRootModelAdapter rootModelAdapter, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull Project project, @NotNull List<MavenProjectsProcessorTask> postTasks) {
    final Library aarLibrary = rootModelAdapter.findLibrary(artifact);
    if (aarLibrary == null) {
        return;
    }
    final MavenId mavenId = artifact.getMavenId();
    Map<MavenId, String> importedAarArtifacts = project.getUserData(IMPORTED_AAR_ARTIFACTS);
    if (importedAarArtifacts == null) {
        importedAarArtifacts = new HashMap<MavenId, String>();
        project.putUserData(IMPORTED_AAR_ARTIFACTS, importedAarArtifacts);
        postTasks.add(new MavenProjectsProcessorTask() {

            @Override
            public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
                project.putUserData(IMPORTED_AAR_ARTIFACTS, null);
            }
        });
    }
    final List<MavenProject> allProjects = mavenTree.getProjects();
    String aarDirPath = importedAarArtifacts.get(mavenId);
    if (aarDirPath == null) {
        final String aarDirName = AndroidMavenUtil.getMavenIdStringForFileName(mavenId);
        aarDirPath = findExtractedAarDirectory(allProjects, aarDirName);
        if (aarDirPath == null) {
            final String genDirPath = AndroidMavenUtil.computePathForGenExternalApklibsDir(mavenId, mavenProject, allProjects);
            if (genDirPath == null) {
                return;
            }
            aarDirPath = genDirPath + "/" + aarDirName;
        }
        importedAarArtifacts.put(mavenId, aarDirPath);
        extractArtifact(artifact.getPath(), aarDirPath, project, mavenProject.getName());
    }
    final Library.ModifiableModel aarLibModel = modelsProvider.getModifiableLibraryModel(aarLibrary);
    final String classesJarPath = aarDirPath + "/" + SdkConstants.FN_CLASSES_JAR;
    final String classesJarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, classesJarPath) + JarFileSystem.JAR_SEPARATOR;
    final String resDirUrl = VfsUtilCore.pathToUrl(aarDirPath + "/" + SdkConstants.FD_RES);
    final Set<String> urlsToAdd = new HashSet<String>(Arrays.asList(classesJarUrl, resDirUrl));
    collectJarsInAarLibsFolder(aarDirPath, urlsToAdd);
    for (String url : aarLibModel.getUrls(OrderRootType.CLASSES)) {
        if (!urlsToAdd.remove(url)) {
            aarLibModel.removeRoot(url, OrderRootType.CLASSES);
        }
    }
    for (String url : urlsToAdd) {
        aarLibModel.addRoot(url, OrderRootType.CLASSES);
    }
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Project(com.intellij.openapi.project.Project) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) Library(com.intellij.openapi.roots.libraries.Library) HashSet(com.intellij.util.containers.HashSet)

Example 3 with MavenProgressIndicator

use of org.jetbrains.idea.maven.utils.MavenProgressIndicator in project intellij-community by JetBrains.

the class RepositoryAttachHandler method doResolveInner.

public static void doResolveInner(Project project, final MavenId mavenId, List<MavenExtraArtifactType> extraTypes, Collection<MavenRepositoryInfo> repositories, @Nullable final Processor<List<MavenArtifact>> resultProcessor, ProgressIndicator indicator) {
    boolean cancelled = false;
    final Collection<MavenArtifact> result = new LinkedHashSet<>();
    MavenEmbeddersManager manager = MavenProjectsManager.getInstance(project).getEmbeddersManager();
    MavenEmbedderWrapper embedder = manager.getEmbedder(MavenEmbeddersManager.FOR_DOWNLOAD, null, null);
    try {
        final MavenGeneralSettings mavenGeneralSettings = MavenProjectsManager.getInstance(project).getGeneralSettings();
        embedder.customizeForResolve(new SoutMavenConsole(mavenGeneralSettings.getOutputLevel(), mavenGeneralSettings.isPrintErrorStackTraces()), new MavenProgressIndicator(indicator));
        List<MavenRemoteRepository> remoteRepositories = convertRepositories(repositories);
        List<MavenArtifactInfo> artifacts = Collections.singletonList(new MavenArtifactInfo(mavenId, "jar", null));
        List<MavenArtifact> firstResult = embedder.resolveTransitively(artifacts, remoteRepositories);
        for (MavenArtifact artifact : firstResult) {
            if (!artifact.isResolved() || MavenConstants.SCOPE_TEST.equals(artifact.getScope())) {
                continue;
            }
            result.add(artifact);
        }
        // download docs & sources
        if (!extraTypes.isEmpty()) {
            Set<String> allowedClassifiers = JBIterable.from(extraTypes).transform(extraType -> extraType.getDefaultClassifier()).toSet();
            List<MavenArtifactInfo> resolve = JBIterable.from(extraTypes).transform(extraType -> new MavenArtifactInfo(mavenId, extraType.getDefaultExtension(), extraType.getDefaultClassifier())).toList();
            // skip sources/javadoc for dependencies
            for (MavenArtifact artifact : embedder.resolveTransitively(new ArrayList<>(resolve), remoteRepositories)) {
                if (!artifact.isResolved() || MavenConstants.SCOPE_TEST.equals(artifact.getScope()) || !allowedClassifiers.contains(artifact.getClassifier())) {
                    continue;
                }
                result.add(artifact);
            }
        }
    } catch (MavenProcessCanceledException e) {
        cancelled = true;
    } finally {
        manager.release(embedder);
        if (!cancelled && resultProcessor != null) {
            ApplicationManager.getApplication().invokeAndWait(() -> resultProcessor.process(new ArrayList<>(result)));
        }
    }
}
Also used : MavenRepositoryServicesManager(org.jetbrains.idea.maven.services.MavenRepositoryServicesManager) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) JBIterable(com.intellij.util.containers.JBIterable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashMap(gnu.trove.THashMap) MavenGeneralSettings(org.jetbrains.idea.maven.project.MavenGeneralSettings) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) PairProcessor(com.intellij.util.PairProcessor) MavenEmbeddersManager(org.jetbrains.idea.maven.project.MavenEmbeddersManager) Task(com.intellij.openapi.progress.Task) SmartList(com.intellij.util.SmartList) SoutMavenConsole(org.jetbrains.idea.maven.execution.SoutMavenConsole) ProjectBundle(org.jetbrains.idea.maven.project.ProjectBundle) RepositoryAttachDialog(org.jetbrains.idea.maven.utils.RepositoryAttachDialog) Messages(com.intellij.openapi.ui.Messages) FileUtil(com.intellij.openapi.util.io.FileUtil) Notifications(com.intellij.notification.Notifications) ProgressManager(com.intellij.openapi.progress.ProgressManager) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryEditor(com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor) NotificationType(com.intellij.notification.NotificationType) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) NewLibraryConfiguration(com.intellij.openapi.roots.libraries.NewLibraryConfiguration) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) MavenLog(org.jetbrains.idea.maven.utils.MavenLog) MavenExtraArtifactType(org.jetbrains.idea.maven.importing.MavenExtraArtifactType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JavadocOrderRootType(com.intellij.openapi.roots.JavadocOrderRootType) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) CommonBundle(com.intellij.CommonBundle) Project(com.intellij.openapi.project.Project) org.jetbrains.idea.maven.model(org.jetbrains.idea.maven.model) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) IOException(java.io.IOException) File(java.io.File) Pair(com.intellij.openapi.util.Pair) MavenDependenciesRemoteManager(org.jetbrains.idea.maven.utils.library.remote.MavenDependenciesRemoteManager) VfsUtil(com.intellij.openapi.vfs.VfsUtil) javax.swing(javax.swing) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) MavenEmbeddersManager(org.jetbrains.idea.maven.project.MavenEmbeddersManager) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) SoutMavenConsole(org.jetbrains.idea.maven.execution.SoutMavenConsole) MavenGeneralSettings(org.jetbrains.idea.maven.project.MavenGeneralSettings) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator)

Example 4 with MavenProgressIndicator

use of org.jetbrains.idea.maven.utils.MavenProgressIndicator in project android by JetBrains.

the class AndroidFacetImporterBase method doImportExternalApklibDependency.

private static String doImportExternalApklibDependency(Project project, IdeModifiableModelsProvider modelsProvider, MavenProjectsTree mavenTree, MavenProject mavenProject, Map<MavenProject, String> mavenProject2ModuleName, List<MavenProjectsProcessorTask> tasks, AndroidExternalApklibDependenciesManager.MavenDependencyInfo depInfo) {
    final MavenId depArtifactMavenId = new MavenId(depInfo.getGroupId(), depInfo.getArtifactId(), depInfo.getVersion());
    final ModifiableModuleModel moduleModel = modelsProvider.getModifiableModuleModel();
    final String apklibModuleName = AndroidMavenUtil.getModuleNameForExtApklibArtifact(depArtifactMavenId);
    Module apklibModule = moduleModel.findModuleByName(apklibModuleName);
    if ((apklibModule == null || apklibModule.getUserData(MODULE_IMPORTED) == null) && MavenConstants.SCOPE_COMPILE.equals(depInfo.getScope())) {
        apklibModule = importExternalApklibArtifact(project, apklibModule, modelsProvider, mavenProject, mavenTree, depArtifactMavenId, depInfo.getPath(), moduleModel, mavenProject2ModuleName);
        if (apklibModule != null) {
            apklibModule.putUserData(MODULE_IMPORTED, Boolean.TRUE);
            final Module finalGenModule = apklibModule;
            tasks.add(new MavenProjectsProcessorTask() {

                @Override
                public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
                    finalGenModule.putUserData(MODULE_IMPORTED, null);
                }
            });
            final MavenArtifactResolvedInfo resolvedDepArtifact = AndroidExternalApklibDependenciesManager.getInstance(project).getResolvedInfoForArtifact(depArtifactMavenId);
            if (resolvedDepArtifact != null) {
                for (AndroidExternalApklibDependenciesManager.MavenDependencyInfo depDepInfo : resolvedDepArtifact.getDependencies()) {
                    final MavenId depDepMavenId = new MavenId(depDepInfo.getGroupId(), depDepInfo.getArtifactId(), depDepInfo.getVersion());
                    if (AndroidMavenUtil.APKLIB_DEPENDENCY_AND_PACKAGING_TYPE.equals(depDepInfo.getType()) && mavenTree.findProject(depDepMavenId) == null) {
                        doImportExternalApklibDependency(project, modelsProvider, mavenTree, mavenProject, mavenProject2ModuleName, tasks, depDepInfo);
                    }
                }
            } else {
                AndroidUtils.reportImportErrorToEventLog("Cannot find resolved info for artifact " + depArtifactMavenId.getKey(), apklibModuleName, project);
            }
        }
    }
    return apklibModuleName;
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Project(com.intellij.openapi.project.Project) MavenId(org.jetbrains.idea.maven.model.MavenId) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) Module(com.intellij.openapi.module.Module)

Aggregations

Project (com.intellij.openapi.project.Project)4 MavenProcessCanceledException (org.jetbrains.idea.maven.utils.MavenProcessCanceledException)4 MavenProgressIndicator (org.jetbrains.idea.maven.utils.MavenProgressIndicator)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 CommonBundle (com.intellij.CommonBundle)1 Notification (com.intellij.notification.Notification)1 NotificationType (com.intellij.notification.NotificationType)1 Notifications (com.intellij.notification.Notifications)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 WriteAction (com.intellij.openapi.application.WriteAction)1 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)1 Module (com.intellij.openapi.module.Module)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Task (com.intellij.openapi.progress.Task)1 JavadocOrderRootType (com.intellij.openapi.roots.JavadocOrderRootType)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1 Library (com.intellij.openapi.roots.libraries.Library)1 NewLibraryConfiguration (com.intellij.openapi.roots.libraries.NewLibraryConfiguration)1