MavenArtifactResolvingHelper implements ArtifactResolvingHelper { @Override public Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes) throws Exception { if (specs.isEmpty()) { return Collections.emptySet(); } List<DependencyNode> nodes; if (transitive) { final CollectRequest request = new CollectRequest(); request.setRepositories(this.remoteRepositories); if (this.dependencyManagement != null) { List<Dependency> managedDependencies = this.dependencyManagement.getDependencies() .stream() .map(mavenDep -> { DefaultArtifact artifact = new DefaultArtifact( mavenDep.getGroupId(), mavenDep.getArtifactId(), mavenDep.getClassifier(), typeToExtension(mavenDep.getType()), mavenDep.getVersion(), new DefaultArtifactType(mavenDep.getType()) ); return new Dependency(artifact, mavenDep.getScope()); }) .collect(Collectors.toList()); request.setManagedDependencies(managedDependencies); } specs.forEach(spec -> request .addDependency(new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), typeToExtension(spec.type()), spec.version(), new DefaultArtifactType(spec.type())), "compile"))); RepositorySystemSession tempSession = new RepositorySystemSessionWrapper(this.session, defaultExcludes ); CollectResult result = this.system.collectDependencies(tempSession, request); PreorderNodeListGenerator gen = new PreorderNodeListGenerator(); result.getRoot().accept(gen); nodes = gen.getNodes(); } else { nodes = new ArrayList<>(); for (ArtifactSpec spec : specs) { Dependency dependency = new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), typeToExtension(spec.type()), spec.version(), new DefaultArtifactType(spec.type())), "compile"); DefaultDependencyNode node = new DefaultDependencyNode(dependency); nodes.add(node); } } List<DependencyNode> extraDependencies = ExtraArtifactsHandler.getExtraDependencies(nodes); nodes.addAll(extraDependencies); resolveDependenciesInParallel(nodes); return nodes.stream() .filter(node -> !"system".equals(node.getDependency().getScope())) .map(node -> { final Artifact artifact = node.getArtifact(); return new ArtifactSpec(node.getDependency().getScope(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getProperty(ArtifactProperties.TYPE, null), artifact.getClassifier(), null); }) .map(this::resolve) .filter(Objects::nonNull) .collect(Collectors.toSet()); } MavenArtifactResolvingHelper(ArtifactResolver resolver,
                                        RepositorySystem system,
                                        RepositorySystemSession session,
                                        DependencyManagement dependencyManagement); void remoteRepository(ArtifactRepository repo); void remoteRepository(RemoteRepository repo); List<RemoteRepository> getRemoteRepositories(); @Override ArtifactSpec resolve(ArtifactSpec spec); @Override Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes); static final ArtifactRepositoryPolicy ENABLED_POLICY; static final ArtifactRepositoryPolicy DISABLED_POLICY; }
@Test public void testArtifactExtensions() throws Exception { Mockito.when(localRepositoryManager.find(Mockito.any(), Mockito.any(LocalArtifactRequest.class))) .thenReturn(new LocalArtifactResult(new LocalArtifactRequest()) .setAvailable(true).setFile(new File("test.jar"))); MavenArtifactResolvingHelper artifactResolverHelper = new MavenArtifactResolvingHelper(resolver, system, sessionMock, null); List<ArtifactSpec> artifacts = Arrays.asList(createSpec("ejb"), createSpec("pom"), createSpec("javadoc")); Set<ArtifactSpec> result = artifactResolverHelper.resolveAll(artifacts, false, false); Assert.assertEquals(3, result.size()); ArgumentCaptor<LocalArtifactRequest> captor = ArgumentCaptor.forClass(LocalArtifactRequest.class); Mockito.verify(localRepositoryManager, Mockito.times(3)).find(Mockito.any(), captor.capture()); Assert.assertEquals("jar", captor.getAllValues().get(0).getArtifact().getExtension()); Assert.assertEquals("pom", captor.getAllValues().get(1).getArtifact().getExtension()); Assert.assertEquals("jar", captor.getAllValues().get(2).getArtifact().getExtension()); }
@Test public void testManagedDependenciesExtensions() throws Exception { Mockito.when(localRepositoryManager.find(Mockito.any(), Mockito.any(LocalArtifactRequest.class))) .thenReturn(new LocalArtifactResult(new LocalArtifactRequest()) .setAvailable(true).setFile(new File("test.jar"))); CollectResult collectResult = new CollectResult(new CollectRequest()); collectResult.setRoot(new DefaultDependencyNode((Artifact) null)); Mockito.when(system.collectDependencies(Mockito.any(), Mockito.any(CollectRequest.class))) .thenReturn(collectResult); DependencyManagement dependencyManagement = new DependencyManagement(); dependencyManagement.addDependency(createDependency("ejb-client")); dependencyManagement.addDependency(createDependency("javadoc")); dependencyManagement.addDependency(createDependency("pom")); MavenArtifactResolvingHelper artifactResolverHelper = new MavenArtifactResolvingHelper(resolver, system, sessionMock, dependencyManagement); List<ArtifactSpec> artifacts = Arrays.asList(createSpec("ejb"), createSpec("pom"), createSpec("javadoc")); artifactResolverHelper.resolveAll(artifacts, true, false); ArgumentCaptor<CollectRequest> captor = ArgumentCaptor.forClass(CollectRequest.class); Mockito.verify(system).collectDependencies(Mockito.any(), captor.capture()); Assert.assertEquals("jar", captor.getValue().getManagedDependencies().get(0).getArtifact().getExtension()); Assert.assertEquals("jar", captor.getValue().getManagedDependencies().get(1).getArtifact().getExtension()); Assert.assertEquals("pom", captor.getValue().getManagedDependencies().get(2).getArtifact().getExtension()); Assert.assertEquals("jar", captor.getValue().getDependencies().get(0).getArtifact().getExtension()); Assert.assertEquals("pom", captor.getValue().getDependencies().get(1).getArtifact().getExtension()); Assert.assertEquals("jar", captor.getValue().getDependencies().get(2).getArtifact().getExtension()); }