Search in sources :

Example 11 with MetadataXpp3Writer

use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer in project indy by Commonjava.

the class KojiMavenMetadataProviderTest method retrieveVersionMetadataWithVerification.

@Test
public void retrieveVersionMetadataWithVerification() throws Exception {
    initKojiClient("simple-metadata-verify", true);
    Metadata metadata = provider.getMetadata(new StoreKey(MAVEN_PKG_KEY, group, "public"), "commons-io/commons-io/maven-metadata.xml");
    assertThat(metadata, notNullValue());
    StringWriter sw = new StringWriter();
    new MetadataXpp3Writer().write(sw, metadata);
    System.out.println(sw.toString());
    Versioning versioning = metadata.getVersioning();
    assertThat(versioning, notNullValue());
    assertThat(versioning.getLatest(), equalTo("2.4.0.redhat-1"));
    assertThat(versioning.getRelease(), equalTo("2.4.0.redhat-1"));
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(2));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("2.1-redhat-1"));
    idx++;
    assertThat(versions.get(idx), equalTo("2.4.0.redhat-1"));
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) StringWriter(java.io.StringWriter) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) TransferMetadata(org.commonjava.maven.galley.io.checksum.TransferMetadata) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Example 12 with MetadataXpp3Writer

use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer in project indy by Commonjava.

the class KojiMavenMetadataProviderTest method retrieveVersionMetadataWithTagWhitelist.

@Test
public void retrieveVersionMetadataWithTagWhitelist() throws Exception {
    kojiConfig.setTagPatternsEnabled(true);
    kojiConfig.setTagPatterns(Collections.singletonList("jb-.+"));
    initKojiClient("whitelisted-tags-metadata-generate", false);
    Metadata metadata = provider.getMetadata(new StoreKey(MAVEN_PKG_KEY, group, "public"), "commons-io/commons-io/maven-metadata.xml");
    assertThat(metadata, notNullValue());
    StringWriter sw = new StringWriter();
    new MetadataXpp3Writer().write(sw, metadata);
    System.out.println(sw.toString());
    Versioning versioning = metadata.getVersioning();
    assertThat(versioning, notNullValue());
    assertThat(versioning.getLatest(), equalTo("2.4.0.redhat-1"));
    assertThat(versioning.getRelease(), equalTo("2.4.0.redhat-1"));
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(2));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("2.1-redhat-1"));
    idx++;
    assertThat(versions.get(idx), equalTo("2.4.0.redhat-1"));
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) StringWriter(java.io.StringWriter) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) TransferMetadata(org.commonjava.maven.galley.io.checksum.TransferMetadata) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Example 13 with MetadataXpp3Writer

use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer in project gradle by gradle.

the class AbstractMavenPublisher method writeMetadataToTmpFile.

private File writeMetadataToTmpFile(Metadata metadata, String fileName) {
    File metadataFile = new File(temporaryDirFactory.create(), fileName);
    xmlTransformer.transform(metadataFile, POM_FILE_ENCODING, writer -> {
        try {
            new MetadataXpp3Writer().write(writer, metadata);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
    return metadataFile;
}
Also used : MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) File(java.io.File)

Example 14 with MetadataXpp3Writer

use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer in project indy by Commonjava.

the class MavenMetadataGenerator method generateGroupFileContent.

/**
 * Generate maven-metadata.xml and checksum files.
 * @param group
 * @param members Concrete stores in group
 * @param path
 * @param eventMetadata
 * @return
 * @throws IndyWorkflowException
 */
@Override
@Measure
public Transfer generateGroupFileContent(final Group group, final List<ArtifactStore> members, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final Transfer rawTarget = fileManager.getTransfer(group, path);
    // First we check the metadata and all of its siblings metadata files
    if (canProcess(path) && exists(rawTarget)) {
        // Means there is no metadata change if this transfer exists, so directly return it.
        logger.trace("Raw metadata file exists for group {} of path {}, no need to regenerate.", group.getKey(), path);
        eventMetadata.set(GROUP_METADATA_EXISTS, true);
        return rawTarget;
    }
    // Then changed back to the metadata itself whatever the path is
    String toMergePath = path;
    if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
        toMergePath = normalize(normalize(parentPath(toMergePath)), MavenMetadataMerger.METADATA_NAME);
    }
    final Transfer target = fileManager.getTransfer(group, toMergePath);
    if (exists(target)) {
        // Means there is no metadata change if this transfer exists, so directly return it.
        logger.trace("Merged metadata file exists for group {} of path {}, no need to regenerate.", group.getKey(), toMergePath);
        eventMetadata.set(GROUP_METADATA_EXISTS, true);
        return target;
    }
    AtomicReference<IndyWorkflowException> wfEx = new AtomicReference<>();
    final String mergePath = toMergePath;
    boolean mergingDone = mergerLocks.ifUnlocked(computeKey(group, toMergePath), p -> {
        try {
            logger.debug("Start metadata generation for metadata file {} in group {}", path, group);
            List<StoreKey> contributing = new ArrayList<>();
            final Metadata md = generateGroupMetadata(group, members, contributing, path);
            if (md != null) {
                final Versioning versioning = md.getVersioning();
                logger.trace("Regenerated Metadata for group {} of path {}: latest version: {}, versions: {}", group.getKey(), mergePath, versioning != null ? versioning.getLatest() : null, versioning != null ? versioning.getVersions() : null);
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    logger.trace("Regenerate lost metadata, group: {}, path: {}", group.getKey(), path);
                    new MetadataXpp3Writer().write(baos, md);
                    final byte[] merged = baos.toByteArray();
                    try (final OutputStream fos = target.openOutputStream(TransferOperation.GENERATE, true, eventMetadata)) {
                        fos.write(merged);
                    } catch (final IOException e) {
                        throw new IndyWorkflowException("Failed to write merged metadata to: {}.\nError: {}", e, target, e.getMessage());
                    }
                    String mergeInfo = writeGroupMergeInfo(md, group, contributing, mergePath);
                    eventMetadata.set(GROUP_METADATA_GENERATED, true);
                    MetadataInfo info = new MetadataInfo(md);
                    info.setMetadataMergeInfo(mergeInfo);
                    putToMetadataCache(group.getKey(), mergePath, info);
                } catch (final IOException e) {
                    logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
                }
            }
        } catch (IndyWorkflowException e) {
            wfEx.set(e);
            return false;
        }
        return true;
    }, (p, mergerLock) -> {
        logger.info("The metadata generation is still in process by another thread for the metadata file for this path {} in group {}, so block current thread to wait for result", path, group);
        return mergerLocks.waitForLock(THREAD_WAITING_TIME_SECONDS, mergerLock);
    });
    IndyWorkflowException ex = wfEx.get();
    if (ex != null) {
        throw ex;
    }
    if (exists(target)) {
        // if this is a checksum file, we need to return the original path (if it is metadata, original is target)
        Transfer original = fileManager.getTransfer(group, path);
        if (exists(original)) {
            if (toMergePath != path) {
                logger.debug("This is a checksum file, return the original path {}", path);
            }
            return original;
        }
    }
    if (mergingDone) {
        logger.info("Merging finished but the merging file not created correctly. See merging related log for details. Merging group: {}, path: {}", group, path);
    } else {
        logger.error("Merging not finished but thread waiting timeout, caused current thread will get a null merging result. Try to enlarge the waiting timeout. Merging group: {}, path: {}", group, path);
    }
    return null;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 15 with MetadataXpp3Writer

use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer in project karaf by apache.

the class KarMojo method createArchive.

/**
 * Generates the configuration archive.
 *
 * @param bundles
 */
@SuppressWarnings("deprecation")
private File createArchive(List<Artifact> bundles, File featuresFile, String groupId, String artifactId, String version) throws MojoExecutionException {
    ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
    File archiveFile = getArchiveFile(outputDirectory, finalName, classifier);
    MavenArchiver archiver = new MavenArchiver();
    MavenArchiveConfiguration configuration = new MavenArchiveConfiguration();
    configuration.addManifestEntries(archive.getManifestEntries());
    archiver.setArchiver(jarArchiver);
    archiver.setOutputFile(archiveFile);
    try {
        // TODO should .kar be a bundle?
        // archive.addManifestEntry(Constants.BUNDLE_NAME, project.getName());
        // archive.addManifestEntry(Constants.BUNDLE_VENDOR, project.getOrganization().getName());
        // ArtifactVersion version = project.getArtifact().getSelectedVersion();
        // String versionString = "" + version.getMajorVersion() + "." + version.getMinorVersion() + "." + version.getIncrementalVersion();
        // if (version.getQualifier() != null) {
        // versionString += "." + version.getQualifier();
        // }
        // archive.addManifestEntry(Constants.BUNDLE_VERSION, versionString);
        // archive.addManifestEntry(Constants.BUNDLE_MANIFESTVERSION, "2");
        // archive.addManifestEntry(Constants.BUNDLE_DESCRIPTION, project.getDescription());
        // // NB, no constant for this one
        // archive.addManifestEntry("Bundle-License", ((License) project.getLicenses().get(0)).getUrl());
        // archive.addManifestEntry(Constants.BUNDLE_DOCURL, project.getUrl());
        // //TODO this might need some help
        // archive.addManifestEntry(Constants.BUNDLE_SYMBOLICNAME, project.getArtifactId());
        // include the feature.xml
        Artifact featureArtifact = factory.createArtifactWithClassifier(groupId, artifactId, version, "xml", KarArtifactInstaller.FEATURE_CLASSIFIER);
        jarArchiver.addFile(featuresFile, repositoryPath + layout.pathOf(featureArtifact));
        if (featureArtifact.isSnapshot()) {
            // the artifact is a snapshot, create the maven-metadata-local.xml
            getLog().debug("Feature artifact is a SNAPSHOT, handling the maven-metadata-local.xml");
            File metadataTarget = new File(featuresFile.getParentFile(), "maven-metadata-local.xml");
            getLog().debug("Looking for " + metadataTarget.getAbsolutePath());
            if (!metadataTarget.exists()) {
                // the maven-metadata-local.xml doesn't exist, create it
                getLog().debug(metadataTarget.getAbsolutePath() + " doesn't exist, create it");
                Metadata metadata = new Metadata();
                metadata.setGroupId(featureArtifact.getGroupId());
                metadata.setArtifactId(featureArtifact.getArtifactId());
                metadata.setVersion(featureArtifact.getVersion());
                metadata.setModelVersion("1.1.0");
                Versioning versioning = new Versioning();
                versioning.setLastUpdatedTimestamp(new Date(System.currentTimeMillis()));
                Snapshot snapshot = new Snapshot();
                snapshot.setLocalCopy(true);
                versioning.setSnapshot(snapshot);
                SnapshotVersion snapshotVersion = new SnapshotVersion();
                snapshotVersion.setClassifier(featureArtifact.getClassifier());
                snapshotVersion.setVersion(featureArtifact.getVersion());
                snapshotVersion.setExtension(featureArtifact.getType());
                snapshotVersion.setUpdated(versioning.getLastUpdated());
                versioning.addSnapshotVersion(snapshotVersion);
                metadata.setVersioning(versioning);
                MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
                try (Writer writer = new FileWriter(metadataTarget)) {
                    metadataWriter.write(writer, metadata);
                } catch (Exception e) {
                    getLog().warn("Could not create maven-metadata-local.xml", e);
                    getLog().warn("It means that this SNAPSHOT could be overwritten by an older one present on remote repositories");
                }
            }
            getLog().debug("Adding file " + metadataTarget.getAbsolutePath() + " in the jar path " + repositoryPath + layout.pathOf(featureArtifact).substring(0, layout.pathOf(featureArtifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
            jarArchiver.addFile(metadataTarget, repositoryPath + layout.pathOf(featureArtifact).substring(0, layout.pathOf(featureArtifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
        }
        for (Artifact artifact : bundles) {
            artifactResolver.resolve(artifact, remoteRepos, localRepo);
            // TODO this may not be reasonable, but... resolved snapshot artifacts have timestamped versions
            // which do not work in startup.properties.
            artifact.setVersion(artifact.getBaseVersion());
            if (artifact.isSnapshot()) {
                // the artifact is a snapshot, create the maven-metadata-local.xml
                final File metadataTmp = Files.createTempFile("maven-metadata-local.xml", ".tmp").toFile();
                try {
                    MavenUtil.generateMavenMetadata(artifact, metadataTmp);
                } catch (Exception e) {
                    getLog().warn("Could not create maven-metadata-local.xml", e);
                    getLog().warn("It means that this SNAPSHOT could be overwritten by an older one present on remote repositories");
                }
                jarArchiver.addFile(metadataTmp, repositoryPath + layout.pathOf(artifact).substring(0, layout.pathOf(artifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
                try {
                    metadataTmp.delete();
                } catch (final Exception ex) {
                    getLog().warn("Cannot delete temporary created file.", ex);
                }
            }
            String targetFileName = repositoryPath + layout.pathOf(artifact);
            jarArchiver.addFile(artifact.getFile(), targetFileName);
        }
        if (resourcesDir.isDirectory()) {
            archiver.getArchiver().addDirectory(resourcesDir);
        }
        archiver.createArchive(project, archive);
        return archiveFile;
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to create archive", e);
    }
}
Also used : ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) MavenArchiver(org.apache.maven.archiver.MavenArchiver) Artifact(org.apache.maven.artifact.Artifact) Date(java.util.Date) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)

Aggregations

MetadataXpp3Writer (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)15 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)13 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)11 StoreKey (org.commonjava.indy.model.core.StoreKey)7 StringWriter (java.io.StringWriter)6 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)6 SnapshotVersion (org.apache.maven.artifact.repository.metadata.SnapshotVersion)5 TransferMetadata (org.commonjava.maven.galley.io.checksum.TransferMetadata)5 Test (org.junit.Test)5 File (java.io.File)4 IOException (java.io.IOException)4 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileWriter (java.io.FileWriter)3 Date (java.util.Date)2 MetadataXpp3Reader (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader)2 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1