Search in sources :

Example 11 with LocalRepositoryManager

use of org.sonatype.aether.repository.LocalRepositoryManager in project intellij-community by JetBrains.

the class Maven30ServerEmbedderImpl method resolveDependencies.

/**
   * copied from {@link DefaultProjectBuilder#resolveDependencies(MavenProject, RepositorySystemSession)}
   */
private DependencyResolutionResult resolveDependencies(MavenProject project, RepositorySystemSession session) {
    DependencyResolutionResult resolutionResult;
    try {
        ProjectDependenciesResolver dependencyResolver = getComponent(ProjectDependenciesResolver.class);
        DefaultDependencyResolutionRequest resolution = new DefaultDependencyResolutionRequest(project, session);
        resolutionResult = dependencyResolver.resolve(resolution);
    } catch (DependencyResolutionException e) {
        resolutionResult = e.getResult();
    }
    Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
    if (resolutionResult.getDependencyGraph() != null) {
        RepositoryUtils.toArtifacts(artifacts, resolutionResult.getDependencyGraph().getChildren(), Collections.singletonList(project.getArtifact().getId()), null);
        // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
        LocalRepositoryManager lrm = session.getLocalRepositoryManager();
        for (Artifact artifact : artifacts) {
            if (!artifact.isResolved()) {
                String path = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact));
                artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
            }
        }
    }
    project.setResolvedArtifacts(artifacts);
    project.setArtifacts(artifacts);
    return resolutionResult;
}
Also used : LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 12 with LocalRepositoryManager

use of org.sonatype.aether.repository.LocalRepositoryManager in project intellij-community by JetBrains.

the class CustomMaven30ArtifactResolver method resolveOld.

private void resolveOld(Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
    if (artifact == null) {
        return;
    }
    if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        File systemFile = artifact.getFile();
        if (systemFile == null) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached", artifact);
        }
        if (!systemFile.exists()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " not found in path: " + systemFile, artifact);
        }
        if (!systemFile.isFile()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " is not a file: " + systemFile, artifact);
        }
        artifact.setResolved(true);
        return;
    }
    if (!artifact.isResolved()) {
        ArtifactResult result;
        try {
            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
            artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));
            // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
            LocalRepositoryManager lrm = session.getLocalRepositoryManager();
            String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact());
            artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
            result = repoSystem.resolveArtifact(session, artifactRequest);
        } catch (org.sonatype.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.sonatype.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact, remoteRepositories, e) {
                };
            } else {
                throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
            }
        }
        artifact.selectVersion(result.getArtifact().getVersion());
        artifact.setFile(result.getArtifact().getFile());
        artifact.setResolved(true);
        if (artifact.isSnapshot()) {
            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
            if (matcher.matches()) {
                Snapshot snapshot = new Snapshot();
                snapshot.setTimestamp(matcher.group(2));
                try {
                    snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
                    artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) LegacyLocalRepositoryManager(org.apache.maven.artifact.repository.LegacyLocalRepositoryManager) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) File(java.io.File)

Aggregations

LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)12 File (java.io.File)8 IOException (java.io.IOException)5 Metadata (org.sonatype.aether.metadata.Metadata)5 LocalArtifactRegistration (org.sonatype.aether.repository.LocalArtifactRegistration)5 LocalMetadataRequest (org.sonatype.aether.repository.LocalMetadataRequest)5 LocalMetadataResult (org.sonatype.aether.repository.LocalMetadataResult)5 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)5 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)5 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)5 Artifact (org.sonatype.aether.artifact.Artifact)4 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)4 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)4 LocalMetadataRegistration (org.sonatype.aether.repository.LocalMetadataRegistration)4 LocalRepository (org.sonatype.aether.repository.LocalRepository)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)3 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)3 RepositoryPolicy (org.sonatype.aether.repository.RepositoryPolicy)3