Search in sources :

Example 1 with MavenProcessCanceledException

use of org.jetbrains.idea.maven.utils.MavenProcessCanceledException 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 MavenProcessCanceledException

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

the class MavenIndicesStressTest method test1.

public void test1() throws Exception {
    MavenCustomRepositoryHelper helper;
    helper = new MavenCustomRepositoryHelper(myDir, "plugins", "local1", "local2");
    helper.copy("plugins", "local1");
    helper.copy("local2", "local1");
    //setRepositoryPath(fixture.getTestDataPath("local1"));
    final MavenIndexerWrapper indexer = MavenServerManager.getInstance().createIndexer();
    File indicesDir = new File(myDir, "indices");
    final MavenIndices indices = new MavenIndices(indexer, indicesDir, this);
    final MavenIndex index = indices.add("id", getRepositoryPath(), MavenIndex.Kind.LOCAL);
    final AtomicBoolean isFinished = new AtomicBoolean(false);
    Thread t1 = new Thread(() -> {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println("INDEXING #" + i);
                indices.updateOrRepair(index, true, getMavenGeneralSettings(), EMPTY_MAVEN_PROCESS);
            }
        } catch (MavenProcessCanceledException e) {
            throw new RuntimeException(e);
        } finally {
            isFinished.set(true);
        }
    }, "maven index 1");
    Thread t2 = new Thread(() -> {
        Random random = new Random();
        while (!isFinished.get()) {
            int i = random.nextInt(100);
            System.out.println("Adding artifact #" + i);
            //index.addArtifact(new MavenId("group" + i, "artifact" + i, "" + i));
            fail();
        }
    }, "maven index 2");
    t1.start();
    t2.start();
    do {
        t1.join(100);
        t2.join(100);
    } while (!isFinished.get());
    t1.join(100);
    t2.join(100);
    indices.close();
    t1.join();
    t2.join();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MavenCustomRepositoryHelper(org.jetbrains.idea.maven.MavenCustomRepositoryHelper) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Random(java.util.Random) MavenIndexerWrapper(org.jetbrains.idea.maven.server.MavenIndexerWrapper) File(java.io.File)

Example 3 with MavenProcessCanceledException

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

the class MavenProjectReader method doReadProjectModel.

private RawModelReadResult doReadProjectModel(VirtualFile file, boolean headerOnly) {
    MavenModel result = null;
    Collection<MavenProjectProblem> problems = MavenProjectProblem.createProblemsList();
    Set<String> alwaysOnProfiles = new THashSet<>();
    String fileExtension = file.getExtension();
    if (!"pom".equalsIgnoreCase(fileExtension) && !"xml".equalsIgnoreCase(fileExtension)) {
        File basedir = getBaseDir(file);
        MavenEmbeddersManager manager = MavenProjectsManager.getInstance(myProject).getEmbeddersManager();
        MavenEmbedderWrapper embedder = manager.getEmbedder(MavenEmbeddersManager.FOR_MODEL_READ, basedir.getPath(), basedir.getPath());
        try {
            result = embedder.readModel(VfsUtilCore.virtualToIoFile(file));
        } catch (MavenProcessCanceledException ignore) {
        } finally {
            manager.release(embedder);
        }
        if (result == null) {
            result = new MavenModel();
            result.setPackaging(MavenConstants.TYPE_JAR);
            return new RawModelReadResult(result, problems, alwaysOnProfiles);
        } else {
            return new RawModelReadResult(result, problems, alwaysOnProfiles);
        }
    }
    result = new MavenModel();
    Element xmlProject = readXml(file, problems, MavenProjectProblem.ProblemType.SYNTAX);
    if (xmlProject == null || !"project".equals(xmlProject.getName())) {
        result.setPackaging(MavenConstants.TYPE_JAR);
        return new RawModelReadResult(result, problems, alwaysOnProfiles);
    }
    MavenParent parent;
    if (MavenJDOMUtil.hasChildByPath(xmlProject, "parent")) {
        parent = new MavenParent(new MavenId(MavenJDOMUtil.findChildValueByPath(xmlProject, "parent.groupId", UNKNOWN), MavenJDOMUtil.findChildValueByPath(xmlProject, "parent.artifactId", UNKNOWN), MavenJDOMUtil.findChildValueByPath(xmlProject, "parent.version", UNKNOWN)), MavenJDOMUtil.findChildValueByPath(xmlProject, "parent.relativePath", "../pom.xml"));
        result.setParent(parent);
    } else {
        parent = new MavenParent(new MavenId(UNKNOWN, UNKNOWN, UNKNOWN), "../pom.xml");
    }
    result.setMavenId(new MavenId(MavenJDOMUtil.findChildValueByPath(xmlProject, "groupId", parent.getMavenId().getGroupId()), MavenJDOMUtil.findChildValueByPath(xmlProject, "artifactId", UNKNOWN), MavenJDOMUtil.findChildValueByPath(xmlProject, "version", parent.getMavenId().getVersion())));
    if (headerOnly)
        return new RawModelReadResult(result, problems, alwaysOnProfiles);
    result.setPackaging(MavenJDOMUtil.findChildValueByPath(xmlProject, "packaging", MavenConstants.TYPE_JAR));
    result.setName(MavenJDOMUtil.findChildValueByPath(xmlProject, "name"));
    readModelBody(result, result.getBuild(), xmlProject);
    result.setProfiles(collectProfiles(file, xmlProject, problems, alwaysOnProfiles));
    return new RawModelReadResult(result, problems, alwaysOnProfiles);
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Element(org.jdom.Element) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) THashSet(gnu.trove.THashSet) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 4 with MavenProcessCanceledException

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

the class MavenProjectReader method resolveProject.

public Collection<MavenProjectReaderResult> resolveProject(final MavenGeneralSettings generalSettings, MavenEmbedderWrapper embedder, Collection<VirtualFile> files, final MavenExplicitProfiles explicitProfiles, final MavenProjectReaderProjectLocator locator) throws MavenProcessCanceledException {
    try {
        Collection<MavenServerExecutionResult> executionResults = embedder.resolveProject(files, explicitProfiles.getEnabledProfiles(), explicitProfiles.getDisabledProfiles());
        Collection<MavenProjectReaderResult> readerResults = ContainerUtil.newArrayList();
        for (MavenServerExecutionResult result : executionResults) {
            MavenServerExecutionResult.ProjectData projectData = result.projectData;
            if (projectData == null) {
                if (files.size() == 1) {
                    final VirtualFile file = files.iterator().next();
                    MavenProjectReaderResult temp = readProject(generalSettings, file, explicitProfiles, locator);
                    temp.readingProblems.addAll(result.problems);
                    temp.unresolvedArtifactIds.addAll(result.unresolvedArtifacts);
                    readerResults.add(temp);
                }
            } else {
                readerResults.add(new MavenProjectReaderResult(projectData.mavenModel, projectData.mavenModelMap, new MavenExplicitProfiles(projectData.activatedProfiles, explicitProfiles.getDisabledProfiles()), projectData.nativeMavenProject, result.problems, result.unresolvedArtifacts));
            }
        }
        return readerResults;
    } catch (MavenProcessCanceledException e) {
        throw e;
    } catch (final Throwable e) {
        MavenLog.LOG.info(e);
        // print exception since we need to know if something wrong with our logic
        MavenLog.printInTests(e);
        return ContainerUtil.mapNotNull(files, file -> {
            MavenProjectReaderResult result = readProject(generalSettings, file, explicitProfiles, locator);
            String message = e.getMessage();
            if (message != null) {
                result.readingProblems.add(MavenProjectProblem.createStructureProblem(file.getPath(), message));
            } else {
                result.readingProblems.add(MavenProjectProblem.createSyntaxProblem(file.getPath(), MavenProjectProblem.ProblemType.SYNTAX));
            }
            return result;
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProfileApplicationResult(org.jetbrains.idea.maven.server.ProfileApplicationResult) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenLog(org.jetbrains.idea.maven.utils.MavenLog) THashSet(gnu.trove.THashSet) MavenServerManager(org.jetbrains.idea.maven.server.MavenServerManager) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) MavenUtil.getBaseDir(org.jetbrains.idea.maven.utils.MavenUtil.getBaseDir) StringUtil.isEmptyOrSpaces(com.intellij.openapi.util.text.StringUtil.isEmptyOrSpaces) org.jetbrains.idea.maven.model(org.jetbrains.idea.maven.model) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) MavenServerExecutionResult(org.jetbrains.idea.maven.server.MavenServerExecutionResult) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) MavenUtil(org.jetbrains.idea.maven.utils.MavenUtil) IOException(java.io.IOException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) MavenJDOMUtil(org.jetbrains.idea.maven.utils.MavenJDOMUtil) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) MavenServerExecutionResult(org.jetbrains.idea.maven.server.MavenServerExecutionResult)

Example 5 with MavenProcessCanceledException

use of org.jetbrains.idea.maven.utils.MavenProcessCanceledException 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)

Aggregations

MavenProcessCanceledException (org.jetbrains.idea.maven.utils.MavenProcessCanceledException)10 File (java.io.File)6 Project (com.intellij.openapi.project.Project)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 MavenEmbedderWrapper (org.jetbrains.idea.maven.server.MavenEmbedderWrapper)5 MavenProgressIndicator (org.jetbrains.idea.maven.utils.MavenProgressIndicator)4 THashMap (gnu.trove.THashMap)3 THashSet (gnu.trove.THashSet)3 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 Pair (com.intellij.openapi.util.Pair)2 VfsUtilCore (com.intellij.openapi.vfs.VfsUtilCore)2 Element (org.jdom.Element)2 MavenEmbeddersManager (org.jetbrains.idea.maven.project.MavenEmbeddersManager)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