Search in sources :

Example 91 with Artifact

use of org.sonatype.aether.artifact.Artifact in project sonatype-aether by sonatype.

the class DefaultDependencyCollector method process.

private void process(final Args args, List<Dependency> dependencies, List<RemoteRepository> repositories, DependencySelector depSelector, DependencyManager depManager, DependencyTraverser depTraverser) throws DependencyCollectionException {
    nextDependency: for (Dependency dependency : dependencies) {
        boolean disableVersionManagement = false;
        List<Artifact> relocations = Collections.emptyList();
        thisDependency: while (true) {
            if (!depSelector.selectDependency(dependency)) {
                continue nextDependency;
            }
            DependencyManagement depMngt = depManager.manageDependency(dependency);
            String premanagedVersion = null;
            String premanagedScope = null;
            if (depMngt != null) {
                if (depMngt.getVersion() != null && !disableVersionManagement) {
                    Artifact artifact = dependency.getArtifact();
                    premanagedVersion = artifact.getVersion();
                    dependency = dependency.setArtifact(artifact.setVersion(depMngt.getVersion()));
                }
                if (depMngt.getProperties() != null) {
                    Artifact artifact = dependency.getArtifact();
                    dependency = dependency.setArtifact(artifact.setProperties(depMngt.getProperties()));
                }
                if (depMngt.getScope() != null) {
                    premanagedScope = dependency.getScope();
                    dependency = dependency.setScope(depMngt.getScope());
                }
                if (depMngt.getExclusions() != null) {
                    dependency = dependency.setExclusions(depMngt.getExclusions());
                }
            }
            disableVersionManagement = false;
            boolean noDescriptor = isLackingDescriptor(dependency.getArtifact());
            boolean traverse = !noDescriptor && depTraverser.traverseDependency(dependency);
            VersionRangeResult rangeResult;
            try {
                VersionRangeRequest rangeRequest = new VersionRangeRequest();
                rangeRequest.setArtifact(dependency.getArtifact());
                rangeRequest.setRepositories(repositories);
                rangeRequest.setRequestContext(args.result.getRequest().getRequestContext());
                rangeRequest.setTrace(args.trace);
                Object key = args.pool.toKey(rangeRequest);
                rangeResult = args.pool.getConstraint(key, rangeRequest);
                if (rangeResult == null) {
                    rangeResult = versionRangeResolver.resolveVersionRange(args.session, rangeRequest);
                    args.pool.putConstraint(key, rangeResult);
                }
                if (rangeResult.getVersions().isEmpty()) {
                    throw new VersionRangeResolutionException(rangeResult, "No versions available for " + dependency.getArtifact() + " within specified range");
                }
            } catch (VersionRangeResolutionException e) {
                addException(args.result, e);
                continue nextDependency;
            }
            List<Version> versions = rangeResult.getVersions();
            for (Version version : versions) {
                Artifact originalArtifact = dependency.getArtifact().setVersion(version.toString());
                Dependency d = dependency.setArtifact(originalArtifact);
                ArtifactDescriptorResult descriptorResult;
                {
                    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
                    descriptorRequest.setArtifact(d.getArtifact());
                    descriptorRequest.setRepositories(repositories);
                    descriptorRequest.setRequestContext(args.result.getRequest().getRequestContext());
                    descriptorRequest.setTrace(args.trace);
                    if (noDescriptor) {
                        descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
                    } else {
                        Object key = args.pool.toKey(descriptorRequest);
                        descriptorResult = args.pool.getDescriptor(key, descriptorRequest);
                        if (descriptorResult == null) {
                            try {
                                descriptorResult = descriptorReader.readArtifactDescriptor(args.session, descriptorRequest);
                                args.pool.putDescriptor(key, descriptorResult);
                            } catch (ArtifactDescriptorException e) {
                                addException(args.result, e);
                                args.pool.putDescriptor(key, e);
                                continue;
                            }
                        } else if (descriptorResult == DataPool.NO_DESCRIPTOR) {
                            continue;
                        }
                    }
                }
                d = d.setArtifact(descriptorResult.getArtifact());
                GraphNode node = args.edges.top().getTarget();
                GraphEdge cycleEdge = args.edges.find(d.getArtifact());
                if (cycleEdge != null) {
                    GraphEdge edge = new GraphEdge(cycleEdge.getTarget());
                    edge.setDependency(d);
                    edge.setScope(d.getScope());
                    edge.setPremanagedScope(premanagedScope);
                    edge.setPremanagedVersion(premanagedVersion);
                    edge.setRelocations(relocations);
                    edge.setVersionConstraint(rangeResult.getVersionConstraint());
                    edge.setVersion(version);
                    edge.setRequestContext(args.result.getRequest().getRequestContext());
                    node.getOutgoingEdges().add(edge);
                    continue;
                }
                if (!descriptorResult.getRelocations().isEmpty()) {
                    relocations = descriptorResult.getRelocations();
                    disableVersionManagement = originalArtifact.getGroupId().equals(d.getArtifact().getGroupId()) && originalArtifact.getArtifactId().equals(d.getArtifact().getArtifactId());
                    dependency = d;
                    continue thisDependency;
                }
                d = args.pool.intern(d.setArtifact(args.pool.intern(d.getArtifact())));
                DependencySelector childSelector = null;
                DependencyManager childManager = null;
                DependencyTraverser childTraverser = null;
                List<RemoteRepository> childRepos = null;
                Object key = null;
                boolean recurse = traverse && !descriptorResult.getDependencies().isEmpty();
                if (recurse) {
                    DefaultDependencyCollectionContext context = args.collectionContext;
                    context.set(d, descriptorResult.getManagedDependencies());
                    childSelector = depSelector.deriveChildSelector(context);
                    childManager = depManager.deriveChildManager(context);
                    childTraverser = depTraverser.deriveChildTraverser(context);
                    childRepos = remoteRepositoryManager.aggregateRepositories(args.session, repositories, descriptorResult.getRepositories(), true);
                    key = args.pool.toKey(d.getArtifact(), childRepos, childSelector, childManager, childTraverser);
                } else {
                    key = args.pool.toKey(d.getArtifact(), repositories);
                }
                List<RemoteRepository> repos;
                ArtifactRepository repo = rangeResult.getRepository(version);
                if (repo instanceof RemoteRepository) {
                    repos = Collections.singletonList((RemoteRepository) repo);
                } else if (repo == null) {
                    repos = repositories;
                } else {
                    repos = Collections.emptyList();
                }
                GraphNode child = args.pool.getNode(key);
                if (child == null) {
                    child = new GraphNode();
                    child.setAliases(descriptorResult.getAliases());
                    child.setRepositories(repos);
                    args.pool.putNode(key, child);
                } else {
                    recurse = false;
                    if (repos.size() < child.getRepositories().size()) {
                        child.setRepositories(repos);
                    }
                }
                GraphEdge edge = new GraphEdge(child);
                edge.setDependency(d);
                edge.setScope(d.getScope());
                edge.setPremanagedScope(premanagedScope);
                edge.setPremanagedVersion(premanagedVersion);
                edge.setRelocations(relocations);
                edge.setVersionConstraint(rangeResult.getVersionConstraint());
                edge.setVersion(version);
                edge.setRequestContext(args.result.getRequest().getRequestContext());
                node.getOutgoingEdges().add(edge);
                if (recurse) {
                    args.edges.push(edge);
                    process(args, descriptorResult.getDependencies(), childRepos, childSelector, childManager, childTraverser);
                    args.edges.pop();
                }
            }
            break;
        }
    }
}
Also used : VersionRangeResult(org.sonatype.aether.resolution.VersionRangeResult) DependencyManager(org.sonatype.aether.collection.DependencyManager) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) ArtifactRepository(org.sonatype.aether.repository.ArtifactRepository) Dependency(org.sonatype.aether.graph.Dependency) DependencyTraverser(org.sonatype.aether.collection.DependencyTraverser) Artifact(org.sonatype.aether.artifact.Artifact) Version(org.sonatype.aether.version.Version) DependencySelector(org.sonatype.aether.collection.DependencySelector) ArrayList(java.util.ArrayList) List(java.util.List) VersionRangeRequest(org.sonatype.aether.resolution.VersionRangeRequest) ArtifactDescriptorResult(org.sonatype.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.sonatype.aether.resolution.ArtifactDescriptorException) DependencyManagement(org.sonatype.aether.collection.DependencyManagement) ArtifactDescriptorRequest(org.sonatype.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.sonatype.aether.resolution.VersionRangeResolutionException)

Example 92 with Artifact

use of org.sonatype.aether.artifact.Artifact in project sonatype-aether by sonatype.

the class DefaultInstaller method install.

private InstallResult install(SyncContext syncContext, RepositorySystemSession session, InstallRequest request) throws InstallationException {
    InstallResult result = new InstallResult(request);
    RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
    List<MetadataGenerator> generators = getMetadataGenerators(session, request);
    List<Artifact> artifacts = new ArrayList<Artifact>(request.getArtifacts());
    IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<Metadata, Object>();
    List<Metadata> metadatas = Utils.prepareMetadata(generators, artifacts);
    syncContext.acquire(artifacts, Utils.combine(request.getMetadata(), metadatas));
    for (Metadata metadata : metadatas) {
        install(session, trace, metadata);
        processedMetadata.put(metadata, null);
        result.addMetadata(metadata);
    }
    for (int i = 0; i < artifacts.size(); i++) {
        Artifact artifact = artifacts.get(i);
        for (MetadataGenerator generator : generators) {
            artifact = generator.transformArtifact(artifact);
        }
        artifacts.set(i, artifact);
        install(session, trace, artifact);
        result.addArtifact(artifact);
    }
    metadatas = Utils.finishMetadata(generators, artifacts);
    syncContext.acquire(null, metadatas);
    for (Metadata metadata : metadatas) {
        install(session, trace, metadata);
        processedMetadata.put(metadata, null);
        result.addMetadata(metadata);
    }
    for (Metadata metadata : request.getMetadata()) {
        if (!processedMetadata.containsKey(metadata)) {
            install(session, trace, metadata);
            result.addMetadata(metadata);
        }
    }
    return result;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) Metadata(org.sonatype.aether.metadata.Metadata) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) InstallResult(org.sonatype.aether.installation.InstallResult) Artifact(org.sonatype.aether.artifact.Artifact) MetadataGenerator(org.sonatype.aether.impl.MetadataGenerator)

Example 93 with Artifact

use of org.sonatype.aether.artifact.Artifact in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManager method checkArtifact.

public void checkArtifact(RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check) {
    if (check.getLocalLastUpdated() != 0 && !isUpdatedRequired(session, check.getLocalLastUpdated(), check.getPolicy())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", locally installed artifact up-to-date.");
        }
        check.setRequired(false);
        return;
    }
    Artifact artifact = check.getItem();
    RemoteRepository repository = check.getRepository();
    File artifactFile = check.getFile();
    if (artifactFile == null) {
        throw new IllegalArgumentException(String.format("The artifact '%s' has no file attached", artifact));
    }
    boolean fileExists = check.isFileValid() && artifactFile.exists();
    File touchFile = getTouchFile(artifact, artifactFile);
    Properties props = read(touchFile);
    String updateKey = getUpdateKey(artifactFile, repository);
    String dataKey = getDataKey(artifact, artifactFile, repository);
    String error = getError(props, dataKey);
    long lastUpdated;
    if (fileExists) {
        lastUpdated = artifactFile.lastModified();
    } else if (error == null) {
        // this is the first attempt ever
        lastUpdated = 0;
    } else if (error.length() <= 0) {
        // artifact did not exist
        lastUpdated = getLastUpdated(props, dataKey);
    } else {
        // artifact could not be transferred
        String transferKey = getTransferKey(artifact, artifactFile, repository);
        lastUpdated = getLastUpdated(props, transferKey);
    }
    if (isAlreadyUpdated(session.getData(), updateKey)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", already updated during this session.");
        }
        check.setRequired(false);
        if (error != null) {
            check.setException(newException(error, artifact, repository));
        }
    } else if (lastUpdated == 0) {
        check.setRequired(true);
    } else if (isUpdatedRequired(session, lastUpdated, check.getPolicy())) {
        check.setRequired(true);
    } else if (fileExists) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", locally cached artifact up-to-date.");
        }
        check.setRequired(false);
    } else {
        if (error == null || error.length() <= 0) {
            if (session.isNotFoundCachingEnabled()) {
                check.setRequired(false);
                check.setException(newException(error, artifact, repository));
            } else {
                check.setRequired(true);
            }
        } else {
            if (session.isTransferErrorCachingEnabled()) {
                check.setRequired(false);
                check.setException(newException(error, artifact, repository));
            } else {
                check.setRequired(true);
            }
        }
    }
}
Also used : RemoteRepository(org.sonatype.aether.repository.RemoteRepository) Properties(java.util.Properties) File(java.io.File) Artifact(org.sonatype.aether.artifact.Artifact)

Example 94 with Artifact

use of org.sonatype.aether.artifact.Artifact in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testLocalArtifactAvailable.

@Test
public void testLocalArtifactAvailable() throws ArtifactResolutionException {
    session.setLocalRepositoryManager(new LocalRepositoryManager() {

        public LocalRepository getRepository() {
            return null;
        }

        public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
            return null;
        }

        public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
            return null;
        }

        public String getPathForLocalMetadata(Metadata metadata) {
            return null;
        }

        public String getPathForLocalArtifact(Artifact artifact) {
            return null;
        }

        public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
            LocalArtifactResult result = new LocalArtifactResult(request);
            result.setAvailable(true);
            try {
                result.setFile(TestFileUtils.createTempFile(""));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

        public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
        }

        public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
            LocalMetadataResult result = new LocalMetadataResult(request);
            try {
                result.setFile(TestFileUtils.createTempFile(""));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

        public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
        }
    });
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    request.addRepository(new RemoteRepository("id", "default", "file:///"));
    ArtifactResult result = resolver.resolveArtifact(session, request);
    assertTrue(result.getExceptions().isEmpty());
    Artifact resolved = result.getArtifact();
    assertNotNull(resolved.getFile());
    resolved = resolved.setFile(null);
    assertEquals(artifact, resolved);
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) LocalArtifactRegistration(org.sonatype.aether.repository.LocalArtifactRegistration) LocalMetadataRequest(org.sonatype.aether.repository.LocalMetadataRequest) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) LocalMetadataRegistration(org.sonatype.aether.repository.LocalMetadataRegistration) LocalRepository(org.sonatype.aether.repository.LocalRepository) Metadata(org.sonatype.aether.metadata.Metadata) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) IOException(java.io.IOException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) TestLocalRepositoryManager(org.sonatype.aether.test.impl.TestLocalRepositoryManager) LocalMetadataResult(org.sonatype.aether.repository.LocalMetadataResult) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) Test(org.junit.Test)

Example 95 with Artifact

use of org.sonatype.aether.artifact.Artifact in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testArtifactNotFoundCache.

@Test
public void testArtifactNotFoundCache() throws Exception {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            for (ArtifactDownload download : artifactDownloads) {
                download.getFile().delete();
                ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
                download.setException(exception);
            }
        }
    };
    remoteRepositoryManager.setConnector(connector);
    resolver.setUpdateCheckManager(new DefaultUpdateCheckManager());
    session.setNotFoundCachingEnabled(true);
    session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
    RemoteRepository remoteRepo = new RemoteRepository("id", "default", "file:///");
    Artifact artifact1 = artifact;
    Artifact artifact2 = artifact.setVersion("ver2");
    ArtifactRequest request1 = new ArtifactRequest(artifact1, Arrays.asList(remoteRepo), "");
    ArtifactRequest request2 = new ArtifactRequest(artifact2, Arrays.asList(remoteRepo), "");
    connector.setExpectGet(new Artifact[] { artifact1, artifact2 });
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
    }
    TestFileUtils.write("artifact", new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact2)));
    lrm.setArtifactAvailability(artifact2, false);
    DefaultUpdateCheckManagerTest.resetSessionData(session);
    connector.resetActual();
    connector.setExpectGet(new Artifact[0]);
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        for (ArtifactResult result : e.getResults()) {
            Throwable t = result.getExceptions().get(0);
            assertEquals(t.toString(), true, t instanceof ArtifactNotFoundException);
            assertEquals(t.toString(), true, t.getMessage().contains("cached"));
        }
    }
}
Also used : RemoteRepository(org.sonatype.aether.repository.RemoteRepository) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) File(java.io.File) Test(org.junit.Test)

Aggregations

Artifact (org.sonatype.aether.artifact.Artifact)116 Test (org.junit.Test)62 File (java.io.File)34 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)33 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)28 DefaultArtifact (org.sonatype.aether.util.artifact.DefaultArtifact)28 Dependency (org.sonatype.aether.graph.Dependency)21 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)17 ArtifactDownload (org.sonatype.aether.spi.connector.ArtifactDownload)16 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)15 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)15 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)15 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)13 SubArtifact (org.sonatype.aether.util.artifact.SubArtifact)13 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)12 ArrayList (java.util.ArrayList)11 RepositorySystem (org.sonatype.aether.RepositorySystem)10 Metadata (org.sonatype.aether.metadata.Metadata)10 CollectRequest (org.sonatype.aether.collection.CollectRequest)9 IOException (java.io.IOException)8