Search in sources :

Example 51 with Metadata

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

the class MavenLocalPublisher method createSnapshotMetadata.

@Override
protected Metadata createSnapshotMetadata(MavenNormalizedPublication publication, String groupId, String artifactId, String version, ExternalResourceRepository repository, ExternalResourceName metadataResource) {
    Metadata metadata = new Metadata();
    metadata.setModelVersion("1.1.0");
    metadata.setGroupId(groupId);
    metadata.setArtifactId(artifactId);
    metadata.setVersion(version);
    Snapshot snapshot = new Snapshot();
    snapshot.setLocalCopy(true);
    Versioning versioning = new Versioning();
    versioning.updateTimestamp();
    versioning.setSnapshot(snapshot);
    for (MavenArtifact artifact : publication.getAllArtifacts()) {
        SnapshotVersion sv = new SnapshotVersion();
        sv.setClassifier(artifact.getClassifier());
        sv.setExtension(artifact.getExtension());
        sv.setVersion(version);
        sv.setUpdated(versioning.getLastUpdated());
        versioning.getSnapshotVersions().add(sv);
    }
    metadata.setVersioning(versioning);
    return metadata;
}
Also used : Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) MavenArtifact(org.gradle.api.publish.maven.MavenArtifact)

Example 52 with Metadata

use of org.apache.maven.artifact.repository.metadata.Metadata in project indy by Commonjava.

the class MavenMetadataGenerator method generateGroupMetadata.

/**
 * Generate group related files (e.g maven-metadata.xml) from three levels.
 * 1. cache, which means all the generation of the files will be cached. In terms of cache clearing, see #{@link MetadataMergeListener}
 * 2. read cached from member hosted repos and try to download from member remote repos
 * 3. generate by member hosted repos (list dir trying to find version directories)
 *
 * @param group
 * @param members concrete store in group
 * @param path
 */
private Metadata generateGroupMetadata(final Group group, final List<ArtifactStore> members, final List<StoreKey> contributingMembers, final String path) throws IndyWorkflowException {
    if (!canProcess(path)) {
        logger.error("The path is not a metadata file: {} ", path);
        return null;
    }
    String toMergePath = path;
    if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
        toMergePath = normalize(normalize(parentPath(toMergePath)), MavenMetadataMerger.METADATA_NAME);
    }
    Metadata meta = getMetaFromCache(group.getKey(), toMergePath);
    if (meta != null) {
        return meta;
    }
    Metadata master = new Metadata();
    master.setVersioning(new Versioning());
    MetadataIncrementalResult incrementalResult = new MetadataIncrementalResult(new HashSet<>(members), Collections.emptySet(), master);
    incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "cached", this::retrieveCached);
    contributingMembers.addAll(incrementalResult.merged);
    incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "downloaded", this::downloadMissing);
    contributingMembers.addAll(incrementalResult.merged);
    incrementalResult = mergeMissing(group, incrementalResult, toMergePath, "generated", this::generateMissing);
    contributingMembers.addAll(incrementalResult.merged);
    if (metadataProviders != null) {
        master = mergeProviderMetadata(group, incrementalResult.result, toMergePath);
    } else {
        master = incrementalResult.result;
    }
    if (!incrementalResult.missing.isEmpty()) {
        logger.warn("After download and generation attempts, metadata is still missing from the following stores: {}, size: {}", incrementalResult.missing, incrementalResult.missing.size());
    }
    Versioning versioning = master.getVersioning();
    List<String> versions = versioning.getVersions();
    logger.debug("Get versioning, versions: {}, release: {}, latest: {}", versions, versioning.getRelease(), versioning.getLatest());
    if (versions != null && !versions.isEmpty()) {
        merger.sortVersions(master);
        return master;
    }
    List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
    if (snapshotVersions != null && !snapshotVersions.isEmpty()) {
        if (logger.isTraceEnabled()) {
            snapshotVersions.forEach(snapshotVersion -> logger.trace("snapshotVersion: {}", snapshotVersion.getVersion()));
        }
        return master;
    }
    List<Plugin> plugins = master.getPlugins();
    if (plugins != null && !plugins.isEmpty()) {
        return master;
    }
    logger.info("The group metadata generation is not successful for path: {} in group: {}", path, group);
    return null;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Plugin(org.apache.maven.artifact.repository.metadata.Plugin)

Example 53 with Metadata

use of org.apache.maven.artifact.repository.metadata.Metadata 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 54 with Metadata

use of org.apache.maven.artifact.repository.metadata.Metadata in project indy by Commonjava.

the class MetadataMarshaller method readFrom.

@Override
public Metadata readFrom(ProtoStreamReader reader) throws IOException {
    Metadata md = new Metadata();
    md.setModelVersion(reader.readString("modelVersion"));
    md.setGroupId(reader.readString("groupId"));
    md.setArtifactId(reader.readString("artifactId"));
    md.setVersion(reader.readString("version"));
    md.setPlugins(reader.readCollection("plugins", new ArrayList<Plugin>(), Plugin.class));
    md.setVersioning(reader.readObject("versioning", Versioning.class));
    md.setModelEncoding(reader.readString("modelEncoding"));
    return md;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) ArrayList(java.util.ArrayList) Plugin(org.apache.maven.artifact.repository.metadata.Plugin)

Example 55 with Metadata

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

the class MavenResolver method resolveVersion.

String resolveVersion(Path artifactPath, MavenReference reference) throws IOException {
    Path artifactMavenMetadataPath = artifactPath.resolve("maven-metadata.xml");
    log.info("Reading file: " + artifactMavenMetadataPath);
    String mavenMetadataXml = IoUtils.readAll(Files.newInputStream(artifactMavenMetadataPath));
    Metadata mavenMetadata = MavenXml.readMetadata(mavenMetadataXml);
    String version = pickVersion(mavenMetadata.getVersioning());
    if (version == null) {
        throw new IllegalStateException("Could not find version");
    }
    return version;
}
Also used : Path(java.nio.file.Path) Metadata(org.apache.maven.artifact.repository.metadata.Metadata)

Aggregations

Metadata (org.apache.maven.artifact.repository.metadata.Metadata)55 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)31 Test (org.junit.Test)20 File (java.io.File)18 MetadataXpp3Reader (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader)16 MetadataXpp3Writer (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)16 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)15 Transfer (org.commonjava.maven.galley.model.Transfer)13 SnapshotVersion (org.apache.maven.artifact.repository.metadata.SnapshotVersion)12 StoreKey (org.commonjava.indy.model.core.StoreKey)12 IOException (java.io.IOException)9 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)9 Group (org.commonjava.indy.model.core.Group)9 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)9 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MavenResolver (io.fabric8.maven.MavenResolver)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6