Search in sources :

Example 96 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRuleTest method testArtifactTransferError.

@Test
public void testArtifactTransferError() throws RepositoryException, DependencyResolutionException {
    DependencyNode graph = createResolvedDependencyGraph("org.apache.maven:maven-core:jar:3.5.2");
    DependencyResolutionResult resolutionResult = mock(DependencyResolutionResult.class);
    when(resolutionResult.getDependencyGraph()).thenReturn(graph);
    DependencyResolutionException exception = createDummyResolutionException(new DefaultArtifact("aopalliance:aopalliance:1.0"), resolutionResult);
    when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception);
    try {
        rule.execute(mockRuleHelper);
        fail("The rule should throw EnforcerRuleException upon dependency resolution exception");
    } catch (EnforcerRuleException expected) {
        verify(mockLog).warn("aopalliance:aopalliance:jar:1.0 was not resolved. " + "Dependency path: a:b:jar:0.1 > " + "org.apache.maven:maven-core:jar:3.5.2 (compile) > " + "com.google.inject:guice:jar:no_aop:4.0 (compile) > " + "aopalliance:aopalliance:jar:1.0 (compile)");
    }
}
Also used : DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 97 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project m2e-core by eclipse-m2e.

the class MavenPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (WORKSPACE_RESULUTION_ENABLE.equals(property)) {
        boolean enableWorkspaceResolution = true;
        IAdaptable adaptable = (IAdaptable) receiver;
        IProject projectAdapter = adaptable.getAdapter(IProject.class);
        if (projectAdapter != null) {
            IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
            IMavenProjectFacade projectFacade = projectManager.create(projectAdapter, new NullProgressMonitor());
            if (projectFacade != null) {
                ResolverConfiguration configuration = projectFacade.getResolverConfiguration();
                return !configuration.shouldResolveWorkspaceProjects();
            }
        }
        return enableWorkspaceResolution;
    }
    if (HAS_ARTIFACT_KEY.equals(property)) {
        ArtifactKey ak = SelectionUtil.getType(receiver, ArtifactKey.class);
        return ak != null;
    }
    if (HAS_PROJECT_ARTIFACT_KEY.equals(property)) {
        ArtifactKey key = SelectionUtil.getType(receiver, ArtifactKey.class);
        if (key != null) {
            IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
            IMavenProjectFacade mavenProject = null;
            mavenProject = // 
            projectManager.getMavenProject(key.getGroupId(), key.getArtifactId(), key.getVersion());
            return mavenProject != null;
        }
    }
    if (IS_TRANSITIVE_DEPENDENCY_TREE_NODE.equals(property)) {
        if (receiver instanceof DependencyNode) {
            DependencyNode nd = (DependencyNode) receiver;
            return nd.getData().get("LEVEL") == null;
        }
    }
    if (IS_DIRECT_DEPENDENCY_TREE_NODE.equals(property)) {
        if (receiver instanceof DependencyNode) {
            DependencyNode nd = (DependencyNode) receiver;
            return "DIRECT".equals(nd.getData().get("LEVEL"));
        }
    }
    if (IS_BUILD_DIRECTORY.equals(property)) {
        if (receiver instanceof IFolder) {
            IFolder folder = (IFolder) receiver;
            IProject project = folder.getProject();
            if (project != null) {
                IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
                // Lookup project facade in the cache registry to minimize UI locking
                IMavenProjectFacade projectFacade = projectManager.create(project, new NullProgressMonitor());
                IPath outputLocation;
                if (projectFacade == null || projectFacade.getMavenProject() == null) {
                    // If the project facade has not been cached yet (ex. during workspace startup),
                    // fall back on the default value
                    outputLocation = new Path(DEFAULT_BUILD_DIR);
                } else {
                    String buildDir = projectFacade.getMavenProject().getBuild().getDirectory();
                    outputLocation = MavenProjectUtils.getProjectRelativePath(project, buildDir);
                }
                if (outputLocation != null) {
                    return folder.equals(project.getFolder(outputLocation));
                }
            }
        }
    }
    if (HAS_MAVEN_NATURE.equals(property)) {
        if (receiver instanceof IFileEditorInput) {
            var editor = (IFileEditorInput) receiver;
            return checkProjectNature(editor.getFile().getProject(), expectedValue);
        }
        if (receiver instanceof IResource) {
            IProject project = ((IResource) receiver).getProject();
            return checkProjectNature(project, expectedValue);
        }
    }
    return false;
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IAdaptable(org.eclipse.core.runtime.IAdaptable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) IPath(org.eclipse.core.runtime.IPath) IMavenProjectRegistry(org.eclipse.m2e.core.project.IMavenProjectRegistry) IProject(org.eclipse.core.resources.IProject) IFileEditorInput(org.eclipse.ui.IFileEditorInput) DependencyNode(org.eclipse.aether.graph.DependencyNode) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 98 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project m2e-core by eclipse-m2e.

the class DependencyTreePage method createHierarchySection.

private void createHierarchySection(Composite sashForm, FormToolkit formToolkit) {
    Composite hierarchyComposite = formToolkit.createComposite(sashForm, SWT.NONE);
    hierarchyComposite.setLayout(new GridLayout());
    Section hierarchySection = formToolkit.createSection(hierarchyComposite, ExpandableComposite.TITLE_BAR);
    hierarchySection.marginHeight = 1;
    GridData gd_hierarchySection = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd_hierarchySection.widthHint = 100;
    gd_hierarchySection.minimumWidth = 100;
    hierarchySection.setLayoutData(gd_hierarchySection);
    hierarchySection.setText(Messages.DependencyTreePage_section_hierarchy);
    formToolkit.paintBordersFor(hierarchySection);
    Tree tree = formToolkit.createTree(hierarchySection, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
    hierarchySection.setClient(tree);
    treeViewer = new TreeViewer(tree);
    treeViewer.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.TRUE);
    DependencyTreeLabelProvider treeLabelProvider = new DependencyTreeLabelProvider();
    treeViewer.setContentProvider(new DependencyTreeContentProvider());
    treeViewer.setLabelProvider(treeLabelProvider);
    treeViewer.addSelectionChangedListener(event -> {
        if (!isSettingSelection) {
            isSettingSelection = true;
            IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
            selectListElements(new DependencyNodeMatcher(selection));
            isSettingSelection = false;
        }
    });
    treeViewer.addOpenListener(event -> {
        IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
        for (Object o : selection) {
            if (o instanceof DependencyNode) {
                org.eclipse.aether.artifact.Artifact a = ((DependencyNode) o).getDependency().getArtifact();
                OpenPomAction.openEditor(a.getGroupId(), a.getArtifactId(), a.getVersion(), mavenProject, null);
            }
        }
    });
    createHierarchyToolbar(hierarchySection, treeLabelProvider, formToolkit);
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ExpandableComposite(org.eclipse.ui.forms.widgets.ExpandableComposite) TreeViewer(org.eclipse.jface.viewers.TreeViewer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Section(org.eclipse.ui.forms.widgets.Section) GridLayout(org.eclipse.swt.layout.GridLayout) DependencyNode(org.eclipse.aether.graph.DependencyNode) GridData(org.eclipse.swt.layout.GridData) Tree(org.eclipse.swt.widgets.Tree)

Example 99 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project m2e-core by eclipse-m2e.

the class ExcludeArtifactRefactoring method getDependencyExcludes.

/**
 * Map key is one of <dependency> element of specified (workspace) model. Map value is set of <excludes> element keys
 * to be added to the <dependency>.
 */
private Map<Dependency, Set<ArtifactKey>> getDependencyExcludes(ParentHierarchyEntry model, IProgressMonitor monitor) throws CoreException {
    IMavenProjectFacade facade = model.getFacade();
    MavenProject project = model.getProject();
    DependencyNode root = MavenPlugin.getMavenModelManager().readDependencyTree(facade, project, JavaScopes.TEST, monitor);
    Visitor visitor = new Visitor(model);
    root.accept(visitor);
    return visitor.getSourceMap();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) DependencyNode(org.eclipse.aether.graph.DependencyNode) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade)

Example 100 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project m2e-core by eclipse-m2e.

the class MavenModelManager method readDependencyTree.

DependencyNode readDependencyTree(RepositorySystemSession repositorySession, MavenProject mavenProject, String scope) throws CoreException {
    DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySession);
    // 
    // Taken from MavenRepositorySystemSession.newSession()
    // 
    ConflictResolver transformer = new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver());
    session.setDependencyGraphTransformer(transformer);
    session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, Boolean.toString(true));
    session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(maven.getProjectRealm(mavenProject));
        ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
        CollectRequest request = new CollectRequest();
        // $NON-NLS-1$
        request.setRequestContext("project");
        request.setRepositories(mavenProject.getRemoteProjectRepositories());
        for (org.apache.maven.model.Dependency dependency : mavenProject.getDependencies()) {
            request.addDependency(RepositoryUtils.toDependency(dependency, stereotypes));
        }
        DependencyManagement depMngt = mavenProject.getDependencyManagement();
        if (depMngt != null) {
            for (org.apache.maven.model.Dependency dependency : depMngt.getDependencies()) {
                request.addManagedDependency(RepositoryUtils.toDependency(dependency, stereotypes));
            }
        }
        DependencyNode node;
        try {
            node = MavenPluginActivator.getDefault().getRepositorySystem().collectDependencies(session, request).getRoot();
        } catch (DependencyCollectionException ex) {
            String msg = Messages.MavenModelManager_error_read;
            log.error(msg, ex);
            throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, msg, ex));
        }
        Collection<String> scopes = new HashSet<>();
        Collections.addAll(scopes, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST);
        if (Artifact.SCOPE_COMPILE.equals(scope)) {
            scopes.remove(Artifact.SCOPE_COMPILE);
            scopes.remove(Artifact.SCOPE_SYSTEM);
            scopes.remove(Artifact.SCOPE_PROVIDED);
        } else if (Artifact.SCOPE_RUNTIME.equals(scope)) {
            scopes.remove(Artifact.SCOPE_COMPILE);
            scopes.remove(Artifact.SCOPE_RUNTIME);
        } else if (Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals(scope)) {
            scopes.remove(Artifact.SCOPE_COMPILE);
            scopes.remove(Artifact.SCOPE_SYSTEM);
            scopes.remove(Artifact.SCOPE_PROVIDED);
            scopes.remove(Artifact.SCOPE_RUNTIME);
        } else {
            scopes.clear();
        }
        CloningDependencyVisitor cloner = new CloningDependencyVisitor();
        node.accept(new FilteringDependencyVisitor(cloner, new ScopeDependencyFilter(null, scopes)));
        node = cloner.getRootNode();
        return node;
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CloningDependencyVisitor(org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor) FilteringDependencyVisitor(org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) CollectRequest(org.eclipse.aether.collection.CollectRequest) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) CoreException(org.eclipse.core.runtime.CoreException) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) DependencyManagement(org.apache.maven.model.DependencyManagement) HashSet(java.util.HashSet)

Aggregations

DependencyNode (org.eclipse.aether.graph.DependencyNode)258 Test (org.junit.Test)107 Artifact (org.eclipse.aether.artifact.Artifact)63 Dependency (org.eclipse.aether.graph.Dependency)59 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)56 DefaultDependencyNode (org.eclipse.aether.graph.DefaultDependencyNode)53 CollectRequest (org.eclipse.aether.collection.CollectRequest)52 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)29 List (java.util.List)27 CollectResult (org.eclipse.aether.collection.CollectResult)24 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)22 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)22 PreorderNodeListGenerator (org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator)20 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)19 Path (java.nio.file.Path)18 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)18 File (java.io.File)17 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)17 Map (java.util.Map)16