Search in sources :

Example 1 with DefaultMetadata

use of org.eclipse.aether.metadata.DefaultMetadata in project bnd by bndtools.

the class RemotePostProcessor method postProcessSnapshot.

private ArtifactResult postProcessSnapshot(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
    for (RemoteRepository repository : request.getRepositories()) {
        if (!repository.getPolicy(true).isEnabled()) {
            // Skip the repo if it isn't enabled for snapshots
            continue;
        }
        // Remove the workspace from the session so that we don't use it
        DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
        newSession.setWorkspaceReader(null);
        // Find the snapshot metadata for the module
        MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "maven-metadata.xml", SNAPSHOT));
        for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
            if (metadataResult.isResolved()) {
                String version;
                try {
                    Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
                    Versioning versioning = read.getVersioning();
                    if (versioning == null || versioning.getSnapshotVersions() == null || versioning.getSnapshotVersions().isEmpty()) {
                        continue;
                    } else {
                        version = versioning.getSnapshotVersions().get(0).getVersion();
                    }
                } catch (Exception e) {
                    throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
                }
                Artifact fullVersionArtifact = new org.eclipse.aether.artifact.DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getExtension(), version);
                try {
                    ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(fullVersionArtifact).addRepository(repository));
                    if (result.isResolved()) {
                        File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteArtifact(fullVersionArtifact, repository, artifact.toString()));
                        if (!toUse.exists()) {
                            logger.warn("The resolved artifact {} does not exist at {}", fullVersionArtifact, toUse);
                            continue;
                        } else {
                            logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
                        }
                        result.getArtifact().setFile(toUse);
                        return result;
                    }
                } catch (ArtifactResolutionException e) {
                    logger.debug("Unable to locate the artifact {}", fullVersionArtifact, e);
                }
            }
        }
    }
    logger.debug("Unable to resolve a remote repository containing {}", artifact);
    return null;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Artifact(org.eclipse.aether.artifact.Artifact) MetadataResult(org.eclipse.aether.resolution.MetadataResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) File(java.io.File)

Example 2 with DefaultMetadata

use of org.eclipse.aether.metadata.DefaultMetadata in project fabric8 by jboss-fuse.

the class MavenProxyServletSupport method convertPathToMetadata.

/**
 * <p>Converts the path of the request to {@link Metadata}.</p>
 * <p>The problem is that we may have <code>g/a/v/maven-metadata.xml</code> or <code>g/a/maven-metadata.xml</code>,
 * but <code>g</code> may be <code>org/something/something</code> or just <code>commons-logging</code>.</p>
 *
 * @param path The request path, following the format: {@code <groupId>/<artifactId>/<version>/<artifactId>-<version>-[<classifier>].extension}
 * @return
 * @throws InvalidMavenArtifactRequest
 */
protected Metadata convertPathToMetadata(String path) throws InvalidMavenArtifactRequest {
    DefaultMetadata metadata = null;
    if (path == null) {
        throw new InvalidMavenArtifactRequest("Cannot match request path to maven url, request path is empty.");
    }
    Matcher pathMatcher = ARTIFACT_GA_METADATA_URL_REGEX.matcher(path);
    if (pathMatcher.matches()) {
        // from g/a/maven-metadata.xml* to g1/g2/.../a/v/maven-metadata.xml*
        String version = pathMatcher.group(3);
        if (version != null && version.length() > 0 && Character.isDigit(version.charAt(0))) {
            // we have version that starts with digit - assume GAV metadata
            Matcher pathGavMatcher = ARTIFACT_METADATA_URL_REGEX.matcher(path);
            if (pathGavMatcher.matches()) {
                pathMatcher = pathGavMatcher;
                version = pathMatcher.group(3);
            }
        } else {
            version = "";
        }
        String groupId = pathMatcher.group(1).replaceAll("/", ".");
        String artifactId = pathMatcher.group(2);
        String type = pathMatcher.group(8);
        if (type == null) {
            type = "maven-metadata.xml";
        } else {
            type = "maven-metadata.xml." + type;
        }
        metadata = new DefaultMetadata(groupId, artifactId, version, type, Metadata.Nature.RELEASE_OR_SNAPSHOT);
    } else {
        pathMatcher = ARTIFACT_GA_METADATA_URL_REGEX.matcher(path);
        if (pathMatcher.matches()) {
            String groupId = pathMatcher.group(1).replaceAll("/", ".");
            String artifactId = pathMatcher.group(2);
            String version = pathMatcher.group(3);
            if (version != null && version.length() > 0 && Character.isDigit(version.charAt(0))) {
            // we have version that starts with digit - assume GAV metadata
            } else {
                Matcher pathGaMatcher = ARTIFACT_GA_METADATA_URL_REGEX.matcher(path);
                if (pathGaMatcher.matches()) {
                    // let's assume it's GA metadata (no version)
                    version = "";
                    groupId = pathGaMatcher.group(1).replaceAll("/", ".");
                    artifactId = pathGaMatcher.group(2);
                }
            }
            String type = pathMatcher.group(8);
            if (type == null) {
                type = "maven-metadata.xml";
            } else {
                type = "maven-metadata.xml." + type;
            }
            metadata = new DefaultMetadata(groupId, artifactId, version, type, Metadata.Nature.RELEASE_OR_SNAPSHOT);
        }
    }
    return metadata;
}
Also used : Matcher(java.util.regex.Matcher) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata)

Example 3 with DefaultMetadata

use of org.eclipse.aether.metadata.DefaultMetadata in project bnd by bndtools.

the class RemotePostProcessor method postProcessRelease.

private ArtifactResult postProcessRelease(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
    for (RemoteRepository repository : request.getRepositories()) {
        if (!repository.getPolicy(false).isEnabled()) {
            // Skip the repo if it isn't enabled for releases
            continue;
        }
        // Remove the workspace from the session so that we don't use it
        DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
        newSession.setWorkspaceReader(null);
        // Find the snapshot metadata for the module
        MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), null, "maven-metadata.xml", RELEASE));
        for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
            if (metadataResult.isResolved()) {
                try {
                    Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
                    Versioning versioning = read.getVersioning();
                    if (versioning == null || versioning.getVersions() == null || versioning.getVersions().isEmpty()) {
                        continue;
                    } else if (versioning.getVersions().contains(artifact.getVersion())) {
                        ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(artifact).addRepository(repository));
                        if (result.isResolved()) {
                            File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForLocalArtifact(artifact));
                            if (!toUse.exists()) {
                                logger.warn("The resolved artifact {} does not exist at {}", artifact, toUse);
                                continue;
                            } else {
                                logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
                            }
                            result.getArtifact().setFile(toUse);
                            return result;
                        }
                    }
                } catch (Exception e) {
                    throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
                }
            }
        }
    }
    logger.debug("Unable to resolve a remote repository containing {}", artifact);
    return null;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MetadataResult(org.eclipse.aether.resolution.MetadataResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Aggregations

DefaultMetadata (org.eclipse.aether.metadata.DefaultMetadata)3 File (java.io.File)2 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)2 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)2 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)2 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)2 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)2 MetadataRequest (org.eclipse.aether.resolution.MetadataRequest)2 MetadataResult (org.eclipse.aether.resolution.MetadataResult)2 Matcher (java.util.regex.Matcher)1 Artifact (org.eclipse.aether.artifact.Artifact)1