Example usage for org.apache.maven.artifact.repository.metadata ArtifactRepositoryMetadata ArtifactRepositoryMetadata

List of usage examples for org.apache.maven.artifact.repository.metadata ArtifactRepositoryMetadata ArtifactRepositoryMetadata

Introduction

In this page you can find the example usage for org.apache.maven.artifact.repository.metadata ArtifactRepositoryMetadata ArtifactRepositoryMetadata.

Prototype

public ArtifactRepositoryMetadata(Artifact artifact) 

Source Link

Usage

From source file:at.yawk.mdep.GenerateMojo.java

@Nullable
@SneakyThrows({ MalformedURLException.class, NoSuchAlgorithmException.class })
@VisibleForTesting/* www.ja  va 2s.c o  m*/
Dependency findArtifactInRepository(Artifact artifact, ArtifactRepository repository)
        throws MojoExecutionException {

    String artifactPath = getArtifactPath(artifact, artifact.getVersion());
    if (artifact.isSnapshot()) {
        ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact) {
            // maven is weird - i have yet to find a better solution.

            @Override
            public boolean storedInArtifactVersionDirectory() {
                return true;
            }

            @Override
            public String getBaseVersion() {
                return artifact.getBaseVersion();
            }
        };

        // try to load maven-metadata.xml in case we need to use a different version for snapshots
        URL metaUrl = new URL(repository.getUrl() + '/' + repository.pathOfRemoteRepositoryMetadata(metadata));

        Metadata loadedMetadata;
        try (InputStream input = openStream(metaUrl)) {
            loadedMetadata = new MetadataXpp3Reader().read(input, true);
        } catch (IOException e) {
            // could not find metadata
            loadedMetadata = null;
        } catch (XmlPullParserException e) {
            throw new MojoExecutionException("Failed to parse metadata", e);
        }

        if (loadedMetadata != null) {
            Snapshot snapshot = loadedMetadata.getVersioning().getSnapshot();

            String versionWithoutSuffix = artifact.getVersion().substring(0,
                    artifact.getBaseVersion().lastIndexOf('-'));
            artifactPath = getArtifactPath(artifact,
                    versionWithoutSuffix + '-' + snapshot.getTimestamp() + '-' + snapshot.getBuildNumber());
        }
    }

    URL url = new URL(repository.getUrl() + '/' + artifactPath);
    try (InputStream input = openStream(url)) {
        getLog().info("Getting checksum for " + artifact);

        MessageDigest digest = MessageDigest.getInstance("SHA-512");
        byte[] buf = new byte[4096];
        int len;
        while ((len = input.read(buf)) >= 0) {
            digest.update(buf, 0, len);
        }

        Dependency dependency = new Dependency();
        dependency.setUrl(url);
        dependency.setSha512sum(digest.digest());
        return dependency;
    } catch (IOException ignored) {
        // not in this repo
        return null;
    }
}

From source file:com.aquent.mojo.delivery.RemoteVersionMojo.java

License:Open Source License

@SuppressWarnings("unchecked")
public void execute() {
    defineVersionProperty("version", 0);
    defineVersionProperty("majorVersion", 0);
    defineVersionProperty("minorVersion", 0);
    defineVersionProperty("incrementalVersion", 0);
    try {//from w  w  w  .j ava  2s.c o m
        Artifact artifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), "",
                "", "");
        RepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
        repositoryMetadataManager.resolveAlways(metadata, localRepository, getRemoteRepository());
        if (metadata.getMetadata() != null && metadata.getMetadata().getVersioning() != null) {
            List<String> allVersions = metadata.getMetadata().getVersioning().getVersions();
            ArtifactVersion foundVersion = null;
            for (String version : allVersions) {
                ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
                if (foundVersion == null || artifactVersion.compareTo(foundVersion) > 0) {
                    foundVersion = artifactVersion;
                }
            }
            if (foundVersion != null) {
                defineVersionProperty("version", foundVersion.toString());
                defineVersionProperty("majorVersion", foundVersion.getMajorVersion());
                defineVersionProperty("minorVersion", foundVersion.getMinorVersion());
                defineVersionProperty("incrementalVersion", foundVersion.getIncrementalVersion());
            }
        }
    } catch (RepositoryMetadataResolutionException ex) {
        getLog().warn(ex.toString());
    }
}

From source file:hudson.maven.artifact.transform.AbstractVersionTransformation.java

License:Apache License

protected String resolveVersion(Artifact artifact, ArtifactRepository localRepository,
        List<ArtifactRepository> remoteRepositories) throws RepositoryMetadataResolutionException {
    RepositoryMetadata metadata;/*from w  ww.  j a v a 2 s .c  o  m*/
    // Don't use snapshot metadata for LATEST (which isSnapshot returns true for)
    if (!artifact.isSnapshot() || Artifact.LATEST_VERSION.equals(artifact.getBaseVersion())) {
        metadata = new ArtifactRepositoryMetadata(artifact);
    } else {
        metadata = new SnapshotArtifactRepositoryMetadata(artifact);
    }

    repositoryMetadataManager.resolve(metadata, remoteRepositories, localRepository);

    artifact.addMetadata(metadata);

    Metadata repoMetadata = metadata.getMetadata();
    String version = null;
    if (repoMetadata != null && repoMetadata.getVersioning() != null) {
        version = constructVersion(repoMetadata.getVersioning(), artifact.getBaseVersion());
    }

    if (version == null) {
        // use the local copy, or if it doesn't exist - go to the remote repo for it
        version = artifact.getBaseVersion();
    }

    // TODO: also do this logging for other metadata?
    // TODO: figure out way to avoid duplicated message
    if (getLogger().isDebugEnabled()) {
        if (!version.equals(artifact.getBaseVersion())) {
            String message = artifact.getArtifactId() + ": resolved to version " + version;
            if (artifact.getRepository() != null) {
                message += " from repository " + artifact.getRepository().getId();
            } else {
                message += " from local repository";
            }
            getLogger().debug(message);
        } else {
            // Locally installed file is newer, don't use the resolved version
            getLogger().debug(artifact.getArtifactId() + ": using locally installed snapshot");
        }
    }
    return version;
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

@Override
public void convert(Artifact artifact, ArtifactRepository targetRepository) throws ArtifactConversionException {
    if (artifact.getRepository().getUrl().equals(targetRepository.getUrl())) {
        throw new ArtifactConversionException(Messages.getString("exception.repositories.match")); //$NON-NLS-1$
    }/*from w ww .  j a v  a 2  s  .  c o m*/

    if (!validateMetadata(artifact)) {
        addWarning(artifact, Messages.getString("unable.to.validate.metadata")); //$NON-NLS-1$
        return;
    }

    FileTransaction transaction = new FileTransaction();

    if (!copyPom(artifact, targetRepository, transaction)) {
        addWarning(artifact, Messages.getString("unable.to.copy.pom")); //$NON-NLS-1$
        return;
    }

    if (!copyArtifact(artifact, targetRepository, transaction)) {
        addWarning(artifact, Messages.getString("unable.to.copy.artifact")); //$NON-NLS-1$
        return;
    }

    Metadata metadata = createBaseMetadata(artifact);
    Versioning versioning = new Versioning();
    versioning.addVersion(artifact.getBaseVersion());
    metadata.setVersioning(versioning);
    updateMetadata(new ArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction);

    metadata = createBaseMetadata(artifact);
    metadata.setVersion(artifact.getBaseVersion());
    versioning = new Versioning();

    Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
    if (matcher.matches()) {
        Snapshot snapshot = new Snapshot();
        snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
        snapshot.setTimestamp(matcher.group(2));
        versioning.setSnapshot(snapshot);
    }

    // TODO: merge latest/release/snapshot from source instead
    metadata.setVersioning(versioning);
    updateMetadata(new SnapshotArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction);

    if (!dryrun) {
        try {
            transaction.commit();
        } catch (TransactionException e) {
            throw new ArtifactConversionException(Messages.getString("transaction.failure", e.getMessage()), e); //$NON-NLS-1$
        }
    }
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

private boolean validateMetadata(Artifact artifact) throws ArtifactConversionException {
    ArtifactRepository repository = artifact.getRepository();

    boolean result = true;

    RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(artifact);
    File file = new File(repository.getBasedir(),
            repository.pathOfRemoteRepositoryMetadata(repositoryMetadata));
    if (file.exists()) {
        Metadata metadata = readMetadata(file);
        result = validateMetadata(metadata, repositoryMetadata, artifact);
    }/*w  w w  .ja v  a  2 s . c  o  m*/

    repositoryMetadata = new SnapshotArtifactRepositoryMetadata(artifact);
    file = new File(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata));
    if (file.exists()) {
        Metadata metadata = readMetadata(file);
        result = result && validateMetadata(metadata, repositoryMetadata, artifact);
    }

    return result;
}

From source file:org.codehaus.mojo.mockrepo.ProxyRepository.java

License:Apache License

public Metadata getMetadata(String path) {
    if (StringUtils.isEmpty(path) || path.length() < 13) {
        return null;
    }//from   ww  w  . j av a2s  .  co  m
    String coordPath = StringUtils.chompLast(path, "/metadata.xml");

    int index = coordPath.lastIndexOf('/');

    String artifactId = index == -1 ? null : coordPath.substring(index + 1);
    String groupId = index == -1 ? null : coordPath.substring(0, index);
    String pluginGroupId = coordPath.replace('/', '.');

    Metadata metadata = new Metadata();

    // is this path a groupId:artifactId pair?
    if (!StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(groupId)) {
        final Artifact artifact = artifactFactory.createDependencyArtifact(groupId, artifactId, anyVersion,
                "pom", null, "compile");
        final ArtifactRepositoryMetadata artifactRepositoryMetadata = new ArtifactRepositoryMetadata(artifact);
        try {
            repositoryMetadataManager.resolve(artifactRepositoryMetadata, remoteRepositories, localRepository);

            final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata();
            if (artifactMetadata.getVersioning() != null) {
                metadata.setGroupId(groupId);
                metadata.setArtifactId(artifactId);
                metadata.merge(artifactMetadata);
            }
        } catch (RepositoryMetadataResolutionException e) {
            log.debug(e);
        }
    }

    // if this path a groupId on its own?
    final GroupRepositoryMetadata groupRepositoryMetadata = new GroupRepositoryMetadata(pluginGroupId);
    try {
        repositoryMetadataManager.resolve(groupRepositoryMetadata, remotePluginRepositories, localRepository);
        metadata.merge(groupRepositoryMetadata.getMetadata());
    } catch (RepositoryMetadataResolutionException e) {
        log.debug(e);
    }

    return metadata;
}

From source file:org.codehaus.mojo.mrm.maven.ProxyArtifactStore.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  w w  .j  a  v  a  2 s . c om*/
 */
public Metadata getMetadata(String path) throws IOException, MetadataNotFoundException {
    path = StringUtils.strip(path, "/");
    int index = path.lastIndexOf('/');
    int index2 = index == -1 ? -1 : path.lastIndexOf('/', index - 1);

    String version = index2 == -1 ? null : path.substring(index + 1);
    String artifactId = index2 == -1 ? null : path.substring(index2 + 1, index);
    String groupId = index2 == -1 ? null : path.substring(0, index2).replace('/', '.');

    Metadata metadata = new Metadata();

    boolean foundSomething = false;

    // is this path a groupId:artifactId pair?
    if (version != null && version.endsWith("-SNAPSHOT") && !StringUtils.isEmpty(artifactId)
            && !StringUtils.isEmpty(groupId)) {
        final org.apache.maven.artifact.Artifact artifact = artifactFactory.createDependencyArtifact(groupId,
                artifactId, VersionRange.createFromVersion(version), "pom", null, "compile");
        final SnapshotArtifactRepositoryMetadata artifactRepositoryMetadata = new SnapshotArtifactRepositoryMetadata(
                artifact);
        try {
            repositoryMetadataManager.resolve(artifactRepositoryMetadata, remoteRepositories, localRepository);

            final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata();
            if (artifactMetadata.getVersioning() != null
                    && artifactMetadata.getVersioning().getSnapshot() != null) {
                foundSomething = true;
                metadata.setGroupId(groupId);
                metadata.setArtifactId(artifactId);
                metadata.setVersion(version);
                metadata.merge(artifactMetadata);
            }
            try {
                if (artifactMetadata.getVersioning() != null
                        && !artifactMetadata.getVersioning().getSnapshotVersions().isEmpty()) {
                    // TODO up to and including Maven 3.0.3 we do not get a populated SnapshotVersions
                    for (SnapshotVersion v : artifactMetadata.getVersioning().getSnapshotVersions()) {
                        metadata.getVersioning().addSnapshotVersion(v);
                        if (v.getVersion().endsWith("-SNAPSHOT")) {
                            addResolved(new Artifact(groupId, artifactId, version, v.getClassifier(),
                                    v.getExtension()));
                        }
                    }
                }
            } catch (NoSuchMethodError e) {
                // ignore Maven 2.x doesn't give us the info
            }
        } catch (RepositoryMetadataResolutionException e) {
            log.debug(e);
        }
    }

    // is this path a groupId:artifactId pair?
    artifactId = index == -1 ? null : path.substring(index + 1);
    groupId = index == -1 ? null : path.substring(0, index).replace('/', '.');
    if (!StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(groupId)) {
        final org.apache.maven.artifact.Artifact artifact = artifactFactory.createDependencyArtifact(groupId,
                artifactId, anyVersion, "pom", null, "compile");
        final ArtifactRepositoryMetadata artifactRepositoryMetadata = new ArtifactRepositoryMetadata(artifact);
        try {
            repositoryMetadataManager.resolve(artifactRepositoryMetadata, remoteRepositories, localRepository);

            final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata();
            if (artifactMetadata.getVersioning() != null) {
                foundSomething = true;
                if (StringUtils.isEmpty(metadata.getGroupId())) {
                    metadata.setGroupId(groupId);
                    metadata.setArtifactId(artifactId);
                }
                metadata.merge(artifactMetadata);
                for (String v : artifactMetadata.getVersioning().getVersions()) {
                    addResolved(path + "/" + v);
                }
            }
        } catch (RepositoryMetadataResolutionException e) {
            log.debug(e);
        }
    }

    // if this path a groupId on its own?
    groupId = path.replace('/', '.');
    final GroupRepositoryMetadata groupRepositoryMetadata = new GroupRepositoryMetadata(groupId);
    try {
        repositoryMetadataManager.resolve(groupRepositoryMetadata, remotePluginRepositories, localRepository);
        foundSomething = true;
        metadata.merge(groupRepositoryMetadata.getMetadata());
        for (Plugin plugin : groupRepositoryMetadata.getMetadata().getPlugins()) {
            addResolved(path + "/" + plugin.getArtifactId());
        }
    } catch (RepositoryMetadataResolutionException e) {
        log.debug(e);
    }

    if (!foundSomething) {
        throw new MetadataNotFoundException(path);
    }
    addResolved(path);
    return metadata;
}

From source file:org.codehaus.mojo.pomtools.helpers.MetadataHelper.java

License:Apache License

public RepositoryMetadata getMetadata(Artifact artifact) throws ArtifactMetadataRetrievalException {
    RepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
    try {/*from   w ww  . j  a v  a 2s  .c  om*/
        repositoryMetadataManager.resolve(metadata, remoteRepositories, localRepository);

        return metadata;
    } catch (RepositoryMetadataResolutionException e) {
        throw new ArtifactMetadataRetrievalException("An error occured while resolving repository metadata", e);
    }
}

From source file:org.commonjava.emb.component.vfind.VersionFinder.java

License:Apache License

protected List<String> retrieveMatchingVersions(final String groupId, final String artifactId,
        final SchemeAwareVersionRange range, final Repository... remoteRepositories)
        throws VersionFinderException, EMBArtifactVersionException {
    final List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>(
            remoteRepositories == null ? 0 : remoteRepositories.length);

    if (remoteRepositories != null) {
        for (final Repository repository : remoteRepositories) {
            try {
                repos.add(repositorySystem.buildArtifactRepository(repository));
            } catch (final InvalidRepositoryException e) {
                throw new VersionFinderException("Failed to build ArtifactRepository from: %s\nReason: %s", e,
                        repository, e.getMessage());
            }//ww  w.j a va  2  s  .c o  m
        }
    }

    final Artifact artifact = repositorySystem.createArtifact(groupId, artifactId, range.toString(), "pom");

    DefaultMetadataResolutionRequest req;
    try {
        req = new DefaultMetadataResolutionRequest().setArtifact(artifact).setForceUpdate(true)
                .setRemoteRepositories(repos)
                .setLocalRepository(repositorySystem.createDefaultLocalRepository());
    } catch (final InvalidRepositoryException e) {
        throw new VersionFinderException("Failed to build LOCAL ArtifactRepository.\nReason: %s", e,
                e.getMessage());
    }

    final ArtifactRepositoryMetadata arm = new ArtifactRepositoryMetadata(artifact);

    try {
        metadataManager.resolve(arm, req);
    } catch (final RepositoryMetadataResolutionException e) {
        throw new VersionFinderException("Failed to resolve available versions for: %s\nReason: %s", e,
                artifact, e.getMessage());
    }

    if (arm.getMetadata() != null && arm.getMetadata().getVersioning() != null) {
        final List<String> versions = arm.getMetadata().getVersioning().getVersions();
        if (versions != null && !versions.isEmpty()) {
            final List<String> result = new ArrayList<String>(versions.size());
            for (final String version : versions) {
                final ArtifactVersion v = new SchemeAwareArtifactVersion(version, range.getVersionScheme());
                if (range.containsVersion(v)) {
                    result.add(version);
                }
            }

            return result;
        }
    }

    return null;
}

From source file:org.echocat.jomon.maven.boot.ArtifactFactory.java

License:Mozilla Public License

@Nonnull
protected String getLatestVersionOf(@Nonnull MavenProject project, @Nonnull Artifact artifact)
        throws ArtifactResolutionException {
    String latestVersion = null;/*from  w ww .  j  a  v a 2 s . c  om*/
    try {
        final ArtifactRepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(artifact);
        _repositoryMetadataManager.resolve(repositoryMetadata, project.getRemoteArtifactRepositories(),
                getLocalRepository());
        latestVersion = selectLatestMatchingVersion(repositoryMetadata, artifact);
    } catch (final Exception ignored) {
    }

    if (latestVersion == null || latestVersion.isEmpty()) {
        throw new ArtifactResolutionException(
                "Could not find a version in the remote repositories but also no latestVersion in the local repository. Stop resolving now.",
                artifact);
    }
    return latestVersion;
}