Search in sources :

Example 11 with MavenEmbedderWrapper

use of org.jetbrains.idea.maven.server.MavenEmbedderWrapper in project intellij-plugins by JetBrains.

the class Flexmojos3ImporterTest method testTransitiveDepsOnPartialProjectImport.

public void testTransitiveDepsOnPartialProjectImport() throws IOException, MavenProcessCanceledException {
    createProjectPom(mavenProjectDescription("aggregator", "pom") + "  <modules>\n" + "    <module>app</module>\n" + "    <module>libA</module>\n" + "    <module>libB</module>\n" + "  </modules>\n");
    final VirtualFile app = createModulePom("app", parent() + "  <artifactId>ttApp</artifactId>\n" + "  <packaging>jar</packaging>\n" + "\n" + dependencies("A", "jar"));
    createModulePom("libA", parent() + "  <artifactId>A</artifactId>\n" + "  <packaging>jar</packaging>\n" + "  \n" + dependencies("B", "jar"));
    createModulePom("libB", parent() + "  <artifactId>B</artifactId>\n" + "  <packaging>jar</packaging>");
    importProject();
    MavenProjectsTree.EmbedderTask task = new MavenProjectsTree.EmbedderTask() {

        @Override
        public void run(MavenEmbedderWrapper embedder) throws MavenProcessCanceledException {
            MavenWorkspaceMap workspaceMap = new MavenWorkspaceMap();
            for (MavenProject mavenProject : myProjectsTree.getProjects()) {
                if (MavenConstants.TYPE_JAR.equalsIgnoreCase(mavenProject.getPackaging())) {
                    workspaceMap.register(mavenProject.getMavenId(), new File(mavenProject.getFile().getPath()), new File(mavenProject.getMavenId().getArtifactId() + ".jar"));
                } else {
                    workspaceMap.register(mavenProject.getMavenId(), new File(mavenProject.getFile().getPath()));
                }
            }
            embedder.customizeForStrictResolve(workspaceMap, NULL_MAVEN_CONSOLE, EMPTY_MAVEN_PROCESS);
            MavenServerExecutionResult result = embedder.execute(app, Collections.emptyList(), Collections.emptyList(), Collections.singletonList("compile"));
            assertEmpty(result.problems);
            assertNotNull(result);
            // test find all transitive deps (MavenWorkspaceMap filler is responsible for)
            MavenModel mavenModel = result.projectData.mavenModel;
            assertTransitiveDeps(TEST_GROUP_ID, TEST_VERSION, mavenModel.getDependencies());
            // test correct artifact file (must be SWC/JAR (project product), but not POM) (CustomArtifactResolver is responsible for)
            MavenArtifact resolve = embedder.resolve(new MavenArtifactInfo(TEST_GROUP_ID, "B", TEST_VERSION, MavenConstants.TYPE_JAR, null), mavenModel.getRemoteRepositories());
            assertNotNull(resolve);
            assertEquals("B.jar", resolve.getFile().getPath());
        }
    };
    MavenProject appProject = myProjectsTree.findProject(new MavenId(TEST_GROUP_ID, "ttApp", TEST_VERSION));
    assertNotNull(appProject);
    myProjectsTree.executeWithEmbedder(appProject, myProjectsManager.getEmbeddersManager(), MavenEmbeddersManager.FOR_POST_PROCESSING, NULL_MAVEN_CONSOLE, EMPTY_MAVEN_PROCESS, task);
    List<MavenArtifact> appSubProjectDeps = appProject.getDependencies();
    assertTransitiveDeps(TEST_GROUP_ID, TEST_VERSION, appSubProjectDeps);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenProjectsTree(org.jetbrains.idea.maven.project.MavenProjectsTree) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenServerExecutionResult(org.jetbrains.idea.maven.server.MavenServerExecutionResult) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 12 with MavenEmbedderWrapper

use of org.jetbrains.idea.maven.server.MavenEmbedderWrapper in project intellij-community by JetBrains.

the class MavenEmbeddersManager method getEmbedder.

@NotNull
public synchronized MavenEmbedderWrapper getEmbedder(Key kind, String workingDirectory, String multiModuleProjectDirectory) {
    Trinity<Key, String, String> key = Trinity.create(kind, workingDirectory, multiModuleProjectDirectory);
    MavenEmbedderWrapper result = myPool.get(key);
    boolean alwaysOnline = kind == FOR_DOWNLOAD;
    if (result == null) {
        result = MavenServerManager.getInstance().createEmbedder(myProject, alwaysOnline, workingDirectory, multiModuleProjectDirectory);
        myPool.put(key, result);
    }
    if (myEmbeddersInUse.contains(result)) {
        MavenLog.LOG.warn("embedder " + key + " is already used");
        return MavenServerManager.getInstance().createEmbedder(myProject, alwaysOnline, workingDirectory, multiModuleProjectDirectory);
    }
    myEmbeddersInUse.add(result);
    return result;
}
Also used : MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) Key(com.intellij.openapi.util.Key) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with MavenEmbedderWrapper

use of org.jetbrains.idea.maven.server.MavenEmbedderWrapper 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 14 with MavenEmbedderWrapper

use of org.jetbrains.idea.maven.server.MavenEmbedderWrapper in project intellij-community by JetBrains.

the class MavenVersionsRemoteManager method execute.

@NotNull
@Override
public List<String> execute(@NotNull RepositoryLibraryDescription repositoryLibraryDescription, ProgressIndicator indicator) {
    MavenEmbeddersManager manager = MavenProjectsManager.getInstance(myProject).getEmbeddersManager();
    MavenEmbedderWrapper embedder = manager.getEmbedder(MavenEmbeddersManager.FOR_GET_VERSIONS, null, null);
    embedder.customizeForGetVersions();
    try {
        List<MavenRemoteRepository> remoteRepositories = convertRepositories(repositoryLibraryDescription.getRemoteRepositories());
        List<String> versions = embedder.retrieveVersions(repositoryLibraryDescription.getGroupId(), repositoryLibraryDescription.getArtifactId(), remoteRepositories);
        Collections.sort(versions, (o1, o2) -> {
            MavenVersionComparable v1 = new MavenVersionComparable(o1);
            MavenVersionComparable v2 = new MavenVersionComparable(o2);
            return v2.compareTo(v1);
        });
        return versions;
    } catch (MavenProcessCanceledException e) {
        return Collections.emptyList();
    } finally {
        manager.release(embedder);
    }
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenRemoteRepository(org.jetbrains.idea.maven.model.MavenRemoteRepository) MavenEmbeddersManager(org.jetbrains.idea.maven.project.MavenEmbeddersManager) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with MavenEmbedderWrapper

use of org.jetbrains.idea.maven.server.MavenEmbedderWrapper in project intellij-community by JetBrains.

the class MavenEmbeddersManagerTest method testCachingOnlyOne.

public void testCachingOnlyOne() throws Exception {
    MavenEmbedderWrapper one1 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one2 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    assertNotSame(one1, one2);
    myManager.release(one1);
    myManager.release(one2);
    MavenEmbedderWrapper one11 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one22 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    assertSame(one1, one11);
    assertNotSame(one2, one22);
}
Also used : MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper)

Aggregations

MavenEmbedderWrapper (org.jetbrains.idea.maven.server.MavenEmbedderWrapper)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)5 MavenProcessCanceledException (org.jetbrains.idea.maven.utils.MavenProcessCanceledException)5 File (java.io.File)4 THashSet (gnu.trove.THashSet)3 IOException (java.io.IOException)3 Project (com.intellij.openapi.project.Project)2 Key (com.intellij.openapi.util.Key)2 Pair (com.intellij.openapi.util.Pair)2 VfsUtilCore (com.intellij.openapi.vfs.VfsUtilCore)2 MultiMap (com.intellij.util.containers.MultiMap)2 THashMap (gnu.trove.THashMap)2 java.util (java.util)2 Element (org.jdom.Element)2 MavenEmbeddersManager (org.jetbrains.idea.maven.project.MavenEmbeddersManager)2 MavenServerExecutionResult (org.jetbrains.idea.maven.server.MavenServerExecutionResult)2 CommonBundle (com.intellij.CommonBundle)1 Notification (com.intellij.notification.Notification)1 NotificationType (com.intellij.notification.NotificationType)1