Search in sources :

Example 16 with Versioning

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

the class KojiMavenMetadataProviderTest method allowVersionMetadataToExpire.

@Test
public void allowVersionMetadataToExpire() throws Exception {
    initKojiClient("simple-metadata-generate", false);
    StoreKey sk = new StoreKey(MAVEN_PKG_KEY, group, "public");
    String path = "commons-io/commons-io/maven-metadata.xml";
    Metadata metadata = provider.getMetadata(sk, path);
    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"));
    String originalLastUpdated = versioning.getLastUpdated();
    Thread.sleep(4000);
    // reset to just after getAPIVersion, since this is only called when the client initializes.
    counter.set(1);
    metadata = provider.getMetadata(sk, path);
    assertThat(metadata, notNullValue());
    sw = new StringWriter();
    new MetadataXpp3Writer().write(sw, metadata);
    System.out.println(sw.toString());
    assertThat(metadata.getVersioning(), notNullValue());
    System.out.printf("\n\nOriginal lastUpdated: '%s'\nNew lastUpdated: '%s'\n\n", originalLastUpdated, metadata.getVersioning().getLastUpdated());
    assertThat(metadata.getVersioning().getLastUpdated(), CoreMatchers.not(CoreMatchers.equalTo(originalLastUpdated)));
}
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 17 with Versioning

use of org.apache.maven.artifact.repository.metadata.Versioning 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 = File.createTempFile("maven-metadata-local.xml", ".tmp");
                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)

Example 18 with Versioning

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

the class RemotePostProcessor method postProcessRelease.

private ArtifactResult postProcessRelease(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
    for (RemoteRepository repository : request.getRepositories()) {
        if (!repository.getPolicy(false).isEnabled()) {
            // Skip the repo if it isn't enabled for releases
            continue;
        }
        // Remove the workspace from the session so that we don't use it
        DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
        newSession.setWorkspaceReader(null);
        // Find the snapshot metadata for the module
        MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), null, "maven-metadata.xml", RELEASE));
        for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
            if (metadataResult.isResolved()) {
                try {
                    Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
                    Versioning versioning = read.getVersioning();
                    if (versioning == null || versioning.getVersions() == null || versioning.getVersions().isEmpty()) {
                        continue;
                    } else if (versioning.getVersions().contains(artifact.getVersion())) {
                        ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(artifact).addRepository(repository));
                        if (result.isResolved()) {
                            File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForLocalArtifact(artifact));
                            if (!toUse.exists()) {
                                logger.warn("The resolved artifact {} does not exist at {}", artifact, toUse);
                                continue;
                            } else {
                                logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
                            }
                            result.getArtifact().setFile(toUse);
                            return result;
                        }
                    }
                } catch (Exception e) {
                    throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
                }
            }
        }
    }
    logger.debug("Unable to resolve a remote repository containing {}", artifact);
    return null;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MetadataResult(org.eclipse.aether.resolution.MetadataResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

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