Search in sources :

Example 1 with Versioning

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

the class MavenUtil method generateMavenMetadata.

/**
     * Generate the maven-metadata-local.xml for the given Maven <code>Artifact</code>.
     *
     * @param artifact the Maven <code>Artifact</code>.
     * @param target   the target maven-metadata-local.xml file to generate.
     * @throws IOException if the maven-metadata-local.xml can't be generated.
     */
public static void generateMavenMetadata(Artifact artifact, File target) throws IOException {
    target.getParentFile().mkdirs();
    Metadata metadata = new Metadata();
    metadata.setGroupId(artifact.getGroupId());
    metadata.setArtifactId(artifact.getArtifactId());
    metadata.setVersion(artifact.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(artifact.getClassifier());
    snapshotVersion.setVersion(artifact.getVersion());
    snapshotVersion.setExtension(artifact.getType());
    snapshotVersion.setUpdated(versioning.getLastUpdated());
    versioning.addSnapshotVersion(snapshotVersion);
    metadata.setVersioning(versioning);
    MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
    Writer writer = new FileWriter(target);
    metadataWriter.write(writer, metadata);
}
Also used : 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) FileWriter(java.io.FileWriter) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Date(java.util.Date) FileWriter(java.io.FileWriter) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) Writer(java.io.Writer)

Example 2 with Versioning

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

the class RepositoryUtils method getDependencyUrlFromRepository.

/**
     * @param artifact not null
     * @param repo not null
     * @return the artifact url in the given repo for the given artifact. If it is a snapshot artifact, the version
     * will be the timestamp and the build number from the metadata. Could return null if the repo is blacklisted.
     */
public String getDependencyUrlFromRepository(Artifact artifact, ArtifactRepository repo) {
    if (repo.isBlacklisted()) {
        return null;
    }
    Artifact copyArtifact = ArtifactUtils.copyArtifact(artifact);
    // Try to get the last artifact repo name depending the snapshot version
    if ((artifact.isSnapshot() && repo.getSnapshots().isEnabled())) {
        if (artifact.getBaseVersion().equals(artifact.getVersion())) {
            // Try to resolve it if not already done
            if (artifact.getMetadataList() == null || artifact.getMetadataList().isEmpty()) {
                try {
                    resolve(artifact);
                } catch (ArtifactResolutionException e) {
                    log.error("Artifact: " + artifact.getId() + " could not be resolved.");
                } catch (ArtifactNotFoundException e) {
                    log.error("Artifact: " + artifact.getId() + " was not found.");
                }
            }
            for (ArtifactMetadata m : artifact.getMetadataList()) {
                if (m instanceof SnapshotArtifactRepositoryMetadata) {
                    SnapshotArtifactRepositoryMetadata snapshotMetadata = (SnapshotArtifactRepositoryMetadata) m;
                    Metadata metadata = snapshotMetadata.getMetadata();
                    Versioning versioning = metadata.getVersioning();
                    if (versioning == null || versioning.getSnapshot() == null || versioning.getSnapshot().isLocalCopy() || versioning.getSnapshot().getTimestamp() == null) {
                        continue;
                    }
                    // create the version according SnapshotTransformation
                    String version = StringUtils.replace(copyArtifact.getVersion(), Artifact.SNAPSHOT_VERSION, versioning.getSnapshot().getTimestamp()) + "-" + versioning.getSnapshot().getBuildNumber();
                    copyArtifact.setVersion(version);
                }
            }
        }
    }
    return repo.getUrl() + "/" + repo.pathOf(copyArtifact);
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactMetadata(org.apache.maven.artifact.metadata.ArtifactMetadata) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with Versioning

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

the class KojiMavenMetadataProvider method getMetadata.

@Override
@IndyMetrics(measure = @Measure(timers = @MetricNamed(name = IndyMetricsKojiNames.METHOD_MAVENMETADATA_GETMETADATA + IndyMetricsNames.TIMER), meters = @MetricNamed(name = IndyMetricsKojiNames.METHOD_MAVENMETADATA_GETMETADATA + IndyMetricsNames.METER)))
public Metadata getMetadata(StoreKey targetKey, String path) throws IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    if (group != targetKey.getType()) {
        logger.debug("Not a group. Cannot supplement with metadata from Koji builds");
        return null;
    }
    if (!kojiConfig.isEnabled()) {
        logger.debug("Koji add-on is disabled.");
        return null;
    }
    if (!kojiConfig.isEnabledFor(targetKey.getName())) {
        logger.debug("Koji integration is not enabled for group: {}", targetKey);
        return null;
    }
    File mdFile = new File(path);
    File artifactDir = mdFile.getParentFile();
    File groupDir = artifactDir == null ? null : artifactDir.getParentFile();
    if (artifactDir == null || groupDir == null) {
        logger.debug("Invalid groupId / artifactId directory structure: '{}' / '{}'", groupDir, artifactDir);
        return null;
    }
    String groupId = groupDir.getPath().replace(File.separatorChar, '.');
    String artifactId = artifactDir.getName();
    ProjectRef ga = null;
    try {
        ga = new SimpleProjectRef(groupId, artifactId);
    } catch (InvalidRefException e) {
        logger.debug("Not a valid Maven GA: {}:{}. Skipping Koji metadata retrieval.", groupId, artifactId);
    }
    if (ga == null) {
        logger.debug("Could not render a valid Maven GA for path: '{}'", path);
        return null;
    }
    ReentrantLock lock;
    synchronized (versionMetadataLocks) {
        lock = versionMetadataLocks.get(ga);
        if (lock == null) {
            lock = new ReentrantLock();
            versionMetadataLocks.put(ga, lock);
        }
    }
    boolean locked = false;
    try {
        locked = lock.tryLock(kojiConfig.getLockTimeoutSeconds(), TimeUnit.SECONDS);
        if (!locked) {
            throw new IndyWorkflowException("Failed to acquire Koji GA version metadata lock on: %s in %d seconds.", ga, kojiConfig.getLockTimeoutSeconds());
        }
        Metadata metadata = versionMetadata.get(ga);
        ProjectRef ref = ga;
        if (metadata == null) {
            try {
                metadata = kojiClient.withKojiSession((session) -> {
                    // short-term caches to help improve performance a bit by avoiding xml-rpc calls.
                    Map<Integer, KojiBuildArchiveCollection> seenBuildArchives = new HashMap<>();
                    Set<Integer> seenBuilds = new HashSet<>();
                    List<KojiArchiveInfo> archives = kojiClient.listArchivesMatching(ref, session);
                    Set<SingleVersion> versions = new HashSet<>();
                    for (KojiArchiveInfo archive : archives) {
                        if (!archive.getFilename().endsWith(".pom")) {
                            logger.debug("Skipping non-POM: {}", archive.getFilename());
                            continue;
                        }
                        SingleVersion singleVersion = VersionUtils.createSingleVersion(archive.getVersion());
                        if (versions.contains(singleVersion)) {
                            logger.debug("Skipping already collected version: {}", archive.getVersion());
                            continue;
                        }
                        KojiBuildInfo build;
                        if (seenBuilds.contains(archive.getBuildId())) {
                            logger.debug("Skipping already seen build: {}", archive.getBuildId());
                            continue;
                        } else {
                            build = kojiClient.getBuildInfo(archive.getBuildId(), session);
                            seenBuilds.add(archive.getBuildId());
                        }
                        if (build == null) {
                            logger.debug("Cannot retrieve build info: {}. Skipping: {}", archive.getBuildId(), archive.getFilename());
                            continue;
                        }
                        if (build.getBuildState() != KojiBuildState.COMPLETE) {
                            logger.debug("Build: {} is not completed. The state is {}. Skipping.", build.getNvr(), build.getBuildState());
                            continue;
                        }
                        if (build.getTaskId() == null) {
                            logger.debug("Build: {} is not a real build. It looks like a binary import. Skipping.", build.getNvr());
                            // This is not a real build, it's a binary import.
                            continue;
                        }
                        boolean buildAllowed = false;
                        if (!kojiConfig.isTagPatternsEnabled()) {
                            buildAllowed = true;
                        } else {
                            logger.debug("Checking for builds/tags of: {}", archive);
                            List<KojiTagInfo> tags = kojiClient.listTags(build.getId(), session);
                            for (KojiTagInfo tag : tags) {
                                if (kojiConfig.isTagAllowed(tag.getName())) {
                                    logger.debug("Koji tag: {} is allowed for proxying.", tag.getName());
                                    buildAllowed = true;
                                    break;
                                } else {
                                    logger.debug("Koji tag: {} is not allowed for proxying.", tag.getName());
                                }
                            }
                        }
                        logger.debug("Checking if build passed tag whitelist check and doesn't collide with something in authority store (if configured)...");
                        if (buildAllowed && buildAuthority.isAuthorized(path, new EventMetadata(), ref, build, session, seenBuildArchives)) {
                            try {
                                logger.debug("Adding version: {} for: {}", archive.getVersion(), path);
                                versions.add(singleVersion);
                            } catch (InvalidVersionSpecificationException e) {
                                logger.warn(String.format("Encountered invalid version: %s for archive: %s. Reason: %s", archive.getVersion(), archive.getArchiveId(), e.getMessage()), e);
                            }
                        }
                    }
                    if (versions.isEmpty()) {
                        logger.debug("No versions found in Koji builds for metadata: {}", path);
                        return null;
                    }
                    List<SingleVersion> sortedVersions = new ArrayList<>(versions);
                    Collections.sort(sortedVersions);
                    Metadata md = new Metadata();
                    md.setGroupId(ref.getGroupId());
                    md.setArtifactId(ref.getArtifactId());
                    Versioning versioning = new Versioning();
                    versioning.setRelease(sortedVersions.get(versions.size() - 1).renderStandard());
                    versioning.setLatest(sortedVersions.get(versions.size() - 1).renderStandard());
                    versioning.setVersions(sortedVersions.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
                    Date lastUpdated = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
                    versioning.setLastUpdated(new SimpleDateFormat(LAST_UPDATED_FORMAT).format(lastUpdated));
                    md.setVersioning(versioning);
                    return md;
                });
            } catch (KojiClientException e) {
                throw new IndyWorkflowException("Failed to retrieve version metadata for: %s from Koji. Reason: %s", e, ga, e.getMessage());
            }
            Metadata md = metadata;
            if (metadata != null) {
                // FIXME: Need a way to listen for cache expiration and re-request this?
                versionMetadata.execute((cache) -> cache.getAdvancedCache().put(ref, md, kojiConfig.getMetadataTimeoutSeconds(), TimeUnit.SECONDS));
            }
        }
        return metadata;
    } catch (InterruptedException e) {
        logger.warn("Interrupted waiting for Koji GA version metadata lock on target: {}", ga);
    } finally {
        if (locked) {
            lock.unlock();
        }
    }
    logger.debug("Returning null metadata result for unknown reason (path: '{}')", path);
    return null;
}
Also used : SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) Date(java.util.Date) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) LoggerFactory(org.slf4j.LoggerFactory) MavenMetadataProvider(org.commonjava.indy.pkg.maven.content.group.MavenMetadataProvider) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) KojiTagInfo(com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) VersionUtils(org.commonjava.maven.atlas.ident.util.VersionUtils) Calendar(java.util.Calendar) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) MetricNamed(org.commonjava.indy.measure.annotation.MetricNamed) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) Map(java.util.Map) KojiArchiveInfo(com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo) KojiClientException(com.redhat.red.build.koji.KojiClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) WeakHashMap(java.util.WeakHashMap) StoreType.group(org.commonjava.indy.model.core.StoreType.group) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) TimeZone(java.util.TimeZone) KojiClient(com.redhat.red.build.koji.KojiClient) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics) Set(java.util.Set) Collectors(java.util.stream.Collectors) CacheHandle(org.commonjava.indy.subsys.infinispan.CacheHandle) InvalidRefException(org.commonjava.maven.atlas.ident.ref.InvalidRefException) File(java.io.File) IndyMetricsKojiNames(org.commonjava.indy.koji.metrics.IndyMetricsKojiNames) TimeUnit(java.util.concurrent.TimeUnit) KojiBuildState(com.redhat.red.build.koji.model.xmlrpc.KojiBuildState) InvalidVersionSpecificationException(org.commonjava.maven.atlas.ident.version.InvalidVersionSpecificationException) List(java.util.List) KojiMavenVersionMetadataCache(org.commonjava.indy.koji.inject.KojiMavenVersionMetadataCache) Measure(org.commonjava.indy.measure.annotation.Measure) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo) IndyMetricsNames(org.commonjava.indy.IndyMetricsNames) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KojiBuildArchiveCollection(com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection) Collections(java.util.Collections) IndyKojiConfig(org.commonjava.indy.koji.conf.IndyKojiConfig) HashSet(java.util.HashSet) Set(java.util.Set) InvalidRefException(org.commonjava.maven.atlas.ident.ref.InvalidRefException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Logger(org.slf4j.Logger) KojiTagInfo(com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo) KojiArchiveInfo(com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) List(java.util.List) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Date(java.util.Date) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) InvalidVersionSpecificationException(org.commonjava.maven.atlas.ident.version.InvalidVersionSpecificationException) KojiClientException(com.redhat.red.build.koji.KojiClientException) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) SimpleDateFormat(java.text.SimpleDateFormat) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics)

Example 4 with Versioning

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

the class MavenMetadataMergerTest method mergeTwoFilesWithInterleavedVersions.

@Test
public void mergeTwoFilesWithInterleavedVersions() throws Exception {
    String path = "org/foo/bar/maven-metadata.xml";
    HostedRepository h1 = new HostedRepository("test-hosted-1");
    HostedRepository h2 = new HostedRepository("test-hosted-2");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "simple-skip.xml");
    Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
    initTestData(t2, VERSION_META + "simple-2.xml");
    Group g = new Group("test-group", h1.getKey(), h2.getKey());
    List<Transfer> sources = Arrays.asList(t1, t2);
    byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
    Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
    assertThat(merged.getGroupId(), equalTo("org.foo"));
    assertThat(merged.getArtifactId(), equalTo("bar"));
    Versioning versioning = merged.getVersioning();
    assertThat(versioning, notNullValue());
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(3));
    assertThat(versioning.getRelease(), equalTo("1.2"));
    assertThat(versioning.getLatest(), equalTo("1.2"));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("1.0"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.1"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.2"));
}
Also used : Group(org.commonjava.indy.model.core.Group) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ByteArrayInputStream(java.io.ByteArrayInputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 5 with Versioning

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

the class MavenMetadataMergerTest method mergeTwoSimpleVersionMetadataFiles.

@Test
public void mergeTwoSimpleVersionMetadataFiles() throws Exception {
    String path = "org/foo/bar/maven-metadata.xml";
    HostedRepository h1 = new HostedRepository("test-hosted-1");
    HostedRepository h2 = new HostedRepository("test-hosted-2");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "simple-1.xml");
    Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
    initTestData(t2, VERSION_META + "simple-2.xml");
    Group g = new Group("test-group", h1.getKey(), h2.getKey());
    List<Transfer> sources = Arrays.asList(t1, t2);
    byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
    Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
    assertThat(merged.getGroupId(), equalTo("org.foo"));
    assertThat(merged.getArtifactId(), equalTo("bar"));
    Versioning versioning = merged.getVersioning();
    assertThat(versioning, notNullValue());
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(2));
    assertThat(versioning.getRelease(), equalTo("1.1"));
    assertThat(versioning.getLatest(), equalTo("1.1"));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("1.0"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.1"));
}
Also used : Group(org.commonjava.indy.model.core.Group) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ByteArrayInputStream(java.io.ByteArrayInputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Aggregations

Metadata (org.apache.maven.artifact.repository.metadata.Metadata)18 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)18 Test (org.junit.Test)11 MetadataXpp3Writer (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)8 MetadataXpp3Reader (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader)7 StoreKey (org.commonjava.indy.model.core.StoreKey)7 Transfer (org.commonjava.maven.galley.model.Transfer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Group (org.commonjava.indy.model.core.Group)6 HostedRepository (org.commonjava.indy.model.core.HostedRepository)6 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)6 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)6 StringWriter (java.io.StringWriter)5 TransferMetadata (org.commonjava.maven.galley.io.checksum.TransferMetadata)4 File (java.io.File)3 Date (java.util.Date)3 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)3 SnapshotVersion (org.apache.maven.artifact.repository.metadata.SnapshotVersion)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 KojiClient (com.redhat.red.build.koji.KojiClient)1