Search in sources :

Example 6 with ArtifactRepositoryMetadata

use of org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata in project maven by apache.

the class DefaultUpdateCheckManagerTest method testMetadata.

@Test
public void testMetadata() throws Exception {
    ArtifactRepository remoteRepository = remoteRepository();
    ArtifactRepository localRepository = localRepository();
    Artifact a = createRemoteArtifact("a", "0.0.1-SNAPSHOT");
    RepositoryMetadata metadata = new ArtifactRepositoryMetadata(a);
    File file = new File(localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata(metadata, localRepository));
    file.delete();
    File touchFile = updateCheckManager.getTouchfile(metadata, file);
    touchFile.delete();
    assertTrue(updateCheckManager.isUpdateRequired(metadata, remoteRepository, file));
    file.getParentFile().mkdirs();
    file.createNewFile();
    updateCheckManager.touch(metadata, remoteRepository, file);
    assertFalse(updateCheckManager.isUpdateRequired(metadata, remoteRepository, file));
    assertNotNull(updateCheckManager.readLastUpdated(touchFile, updateCheckManager.getMetadataKey(remoteRepository, file)));
}
Also used : ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) RepositoryMetadata(org.apache.maven.artifact.repository.metadata.RepositoryMetadata) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) Test(org.junit.jupiter.api.Test)

Example 7 with ArtifactRepositoryMetadata

use of org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata in project maven-plugins by apache.

the class DoapMojo method writeReleases.

/**
 * Write all DOAP releases.
 *
 * @param writer  not null
 * @param project the Maven project, not null
 * @throws MojoExecutionException if any
 * @see <a href="http://usefulinc.com/ns/doap#release">http://usefulinc.com/ns/doap#release</a>
 * @see <a href="http://usefulinc.com/ns/doap#Version">http://usefulinc.com/ns/doap#Version</a>
 */
private void writeReleases(XMLWriter writer, MavenProject project) throws MojoExecutionException {
    Artifact artifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging());
    RepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
    for (ArtifactRepository repo : remoteRepositories) {
        if (repo.isBlacklisted()) {
            continue;
        }
        if (repo.getSnapshots().isEnabled()) {
            continue;
        }
        if (repo.getReleases().isEnabled()) {
            try {
                repositoryMetadataManager.resolveAlways(metadata, localRepository, repo);
                break;
            } catch (RepositoryMetadataResolutionException e) {
                throw new MojoExecutionException(metadata.extendedToString() + " could not be retrieved from repositories due to an error: " + e.getMessage(), e);
            }
        }
    }
    if (metadata.getMetadata().getVersioning() == null) {
        messages.getWarnMessages().add("No versioning was found for " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ". Ignored DOAP <release/> tag.");
        return;
    }
    List<String> versions = metadata.getMetadata().getVersioning().getVersions();
    // Recent releases in first
    Collections.reverse(versions);
    boolean addComment = false;
    int i = 0;
    for (String version : versions) {
        if (!addComment) {
            DoapUtil.writeComment(writer, "Project releases.");
            addComment = true;
        }
        DoapUtil.writeStartElement(writer, doapOptions.getXmlnsPrefix(), "release");
        DoapUtil.writeStartElement(writer, doapOptions.getXmlnsPrefix(), "Version");
        DoapUtil.writeStartElement(writer, doapOptions.getXmlnsPrefix(), "name");
        if (version.equals(metadata.getMetadata().getVersioning().getRelease())) {
            writer.writeText("Latest stable release");
        } else {
            writer.writeText(project.getName() + " - " + version);
        }
        // name
        writer.endElement();
        DoapUtil.writeElement(writer, doapOptions.getXmlnsPrefix(), "revision", version);
        // list all file release from all remote repos
        for (ArtifactRepository repo : remoteRepositories) {
            Artifact artifactRelease = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getPackaging());
            if (artifactRelease == null) {
                continue;
            }
            String fileRelease = repo.getUrl() + "/" + repo.pathOf(artifactRelease);
            try {
                DoapUtil.fetchURL(settings, new URL(fileRelease));
            } catch (IOException e) {
                getLog().debug("IOException :" + e.getMessage());
                continue;
            }
            DoapUtil.writeElement(writer, doapOptions.getXmlnsPrefix(), "file-release", fileRelease);
            Date releaseDate = null;
            try {
                releaseDate = REPOSITORY_DATE_FORMAT.parse(metadata.getMetadata().getVersioning().getLastUpdated());
            } catch (ParseException e) {
                getLog().error("Unable to parse date '" + metadata.getMetadata().getVersioning().getLastUpdated() + "'");
                continue;
            }
            // See MDOAP-11
            if (i == 0) {
                DoapUtil.writeElement(writer, doapOptions.getXmlnsPrefix(), "created", DOAP_DATE_FORMAT.format(releaseDate));
            }
        }
        // Version
        writer.endElement();
        // release
        writer.endElement();
        i++;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) DoapArtifact(org.apache.maven.plugin.doap.options.DoapArtifact) Artifact(org.apache.maven.artifact.Artifact) URL(java.net.URL) Date(java.util.Date) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) RepositoryMetadata(org.apache.maven.artifact.repository.metadata.RepositoryMetadata) ParseException(java.text.ParseException) RepositoryMetadataResolutionException(org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata)

Example 8 with ArtifactRepositoryMetadata

use of org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata in project archiva by apache.

the class LegacyToDefaultConverter method validateMetadata.

private boolean validateMetadata(Artifact artifact) throws ArtifactConversionException {
    ArtifactRepository repository = artifact.getRepository();
    boolean result = true;
    RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(artifact);
    Path file = Paths.get(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata));
    if (Files.exists(file)) {
        Metadata metadata = readMetadata(file);
        result = validateMetadata(metadata, repositoryMetadata, artifact);
    }
    repositoryMetadata = new SnapshotArtifactRepositoryMetadata(artifact);
    file = Paths.get(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata));
    if (Files.exists(file)) {
        Metadata metadata = readMetadata(file);
        result = result && validateMetadata(metadata, repositoryMetadata, artifact);
    }
    return result;
}
Also used : Path(java.nio.file.Path) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) RepositoryMetadata(org.apache.maven.artifact.repository.metadata.RepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) RepositoryMetadata(org.apache.maven.artifact.repository.metadata.RepositoryMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata)

Example 9 with ArtifactRepositoryMetadata

use of org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata in project archiva by apache.

the class LegacyToDefaultConverter method convert.

@Override
public void convert(Artifact artifact, ArtifactRepository targetRepository) throws ArtifactConversionException {
    if (artifact.getRepository().getUrl().equals(targetRepository.getUrl())) {
        // $NON-NLS-1$
        throw new ArtifactConversionException(Messages.getString("exception.repositories.match"));
    }
    if (!validateMetadata(artifact)) {
        // $NON-NLS-1$
        addWarning(artifact, Messages.getString("unable.to.validate.metadata"));
        return;
    }
    FileTransaction transaction = new FileTransaction();
    if (!copyPom(artifact, targetRepository, transaction)) {
        // $NON-NLS-1$
        addWarning(artifact, Messages.getString("unable.to.copy.pom"));
        return;
    }
    if (!copyArtifact(artifact, targetRepository, transaction)) {
        // $NON-NLS-1$
        addWarning(artifact, Messages.getString("unable.to.copy.artifact"));
        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()), // $NON-NLS-1$
            e);
        }
    }
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) TransactionException(org.apache.archiva.transaction.TransactionException) Matcher(java.util.regex.Matcher) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) RepositoryMetadata(org.apache.maven.artifact.repository.metadata.RepositoryMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) FileTransaction(org.apache.archiva.transaction.FileTransaction) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata)

Example 10 with ArtifactRepositoryMetadata

use of org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata in project archiva by apache.

the class LegacyToDefaultConverterTest method testInvalidSourceArtifactMetadata.

@Test
public void testInvalidSourceArtifactMetadata() throws Exception {
    // test artifact is not converted when source metadata is invalid, and returns failure
    createModernSourceRepository();
    Artifact artifact = createArtifact("test", "incorrectArtifactMetadata", "1.0.0");
    Path file = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(artifact));
    Files.deleteIfExists(file);
    artifactConverter.convert(artifact, targetRepository);
    checkWarnings(artifactConverter, 2);
    assertHasWarningReason(artifactConverter, Messages.getString("failure.incorrect.artifactMetadata.versions"));
    assertFalse("Check artifact not created", Files.exists(file));
    ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
    Path metadataFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata(metadata));
    assertFalse("Check metadata not created", Files.exists(metadataFile));
}
Also used : Path(java.nio.file.Path) Artifact(org.apache.maven.artifact.Artifact) ArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) Test(org.junit.Test)

Aggregations

ArtifactRepositoryMetadata (org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata)28 SnapshotArtifactRepositoryMetadata (org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata)22 Artifact (org.apache.maven.artifact.Artifact)20 Path (java.nio.file.Path)18 Test (org.junit.Test)16 ArtifactMetadata (org.apache.maven.artifact.metadata.ArtifactMetadata)11 RepositoryMetadata (org.apache.maven.artifact.repository.metadata.RepositoryMetadata)8 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)4 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)3 RepositoryMetadataResolutionException (org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException)3 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)3 File (java.io.File)2 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)2 MetadataBridge (org.apache.maven.artifact.repository.metadata.MetadataBridge)2 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)2 ProjectArtifactMetadata (org.apache.maven.project.artifact.ProjectArtifactMetadata)2 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)2 SubArtifact (org.eclipse.aether.util.artifact.SubArtifact)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1