Search in sources :

Example 1 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 2 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 3 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)

Example 4 with ArtifactRepositoryMetadata

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

the class LegacyToDefaultConverterTest method testForcedUnmodifiedArtifact.

@Test
public void testForcedUnmodifiedArtifact() throws Exception {
    // test unmodified artifact is still converted when set to force
    artifactConverter = applicationContext.getBean("artifactConverter#force-repository-converter", ArtifactConverter.class);
    Artifact artifact = createArtifact("test", "unmodified-artifact", "1.0.0");
    Artifact pomArtifact = createPomArtifact(artifact);
    Path sourceFile = Paths.get(sourceRepository.getBasedir(), sourceRepository.pathOf(artifact));
    Path sourcePomFile = Paths.get(sourceRepository.getBasedir(), sourceRepository.pathOf(pomArtifact));
    Path targetFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(artifact));
    Path targetPomFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(pomArtifact));
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    long origTime = dateFormat.parse("2006-03-03").getTime();
    Files.setLastModifiedTime(targetFile, FileTime.from(origTime, TimeUnit.MILLISECONDS));
    Files.setLastModifiedTime(targetPomFile, FileTime.from(origTime, TimeUnit.MILLISECONDS));
    Files.setLastModifiedTime(sourceFile, FileTime.from(dateFormat.parse("2006-01-01").getTime(), TimeUnit.MILLISECONDS));
    Files.setLastModifiedTime(sourcePomFile, FileTime.from(dateFormat.parse("2006-02-02").getTime(), TimeUnit.MILLISECONDS));
    artifactConverter.convert(artifact, targetRepository);
    checkSuccess(artifactConverter);
    compareFiles(sourceFile, targetFile);
    compareFiles(sourcePomFile, targetPomFile);
    assertFalse("Check modified", origTime == Files.getLastModifiedTime(targetFile).toMillis());
    assertFalse("Check modified", origTime == Files.getLastModifiedTime(targetPomFile).toMillis());
    ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
    Path metadataFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata(metadata));
    assertTrue("Check metadata created", Files.exists(metadataFile));
}
Also used : Path(java.nio.file.Path) SimpleDateFormat(java.text.SimpleDateFormat) 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)

Example 5 with ArtifactRepositoryMetadata

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

the class LegacyToDefaultConverterTest method testV3PomWarningsOnConvert.

@Test
public void testV3PomWarningsOnConvert() throws Exception {
    // test that the pom is converted but that warnings are reported
    Artifact artifact = createArtifact("test", "v3-warnings-artifact", "1.0.0");
    ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata(artifact);
    Path artifactMetadataFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata(artifactMetadata));
    Files.deleteIfExists(artifactMetadataFile);
    ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata(artifact);
    Path versionMetadataFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata(versionMetadata));
    Files.deleteIfExists(versionMetadataFile);
    artifactConverter.convert(artifact, targetRepository);
    checkWarnings(artifactConverter, 2);
    Path artifactFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(artifact));
    assertTrue("Check artifact created", Files.exists(artifactFile));
    assertTrue("Check artifact matches", FileUtils.contentEquals(artifactFile.toFile(), artifact.getFile()));
    artifact = createPomArtifact(artifact);
    Path pomFile = Paths.get(targetRepository.getBasedir(), targetRepository.pathOf(artifact));
    Path expectedPomFile = getTestFile("src/test/expected-files/converted-v3-warnings.pom");
    assertTrue("Check POM created", Files.exists(pomFile));
    compareFiles(expectedPomFile, pomFile);
// TODO: check 2 warnings (extend and versions) matched on i18n key
}
Also used : Path(java.nio.file.Path) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) 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)20 SnapshotArtifactRepositoryMetadata (org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata)19 Path (java.nio.file.Path)18 Artifact (org.apache.maven.artifact.Artifact)18 Test (org.junit.Test)16 ArtifactMetadata (org.apache.maven.artifact.metadata.ArtifactMetadata)9 RepositoryMetadata (org.apache.maven.artifact.repository.metadata.RepositoryMetadata)3 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 FileTransaction (org.apache.archiva.transaction.FileTransaction)1 TransactionException (org.apache.archiva.transaction.TransactionException)1 RepositoryMetadataResolutionException (org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException)1 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)1 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)1