Search in sources :

Example 1 with MavenArchiveConfiguration

use of org.apache.maven.archiver.MavenArchiveConfiguration 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 2 with MavenArchiveConfiguration

use of org.apache.maven.archiver.MavenArchiveConfiguration in project maven-plugins by apache.

the class ManifestCreationFinalizerTest method testShouldAddManifestWhenArchiverIsJarArchiver.

public void testShouldAddManifestWhenArchiverIsJarArchiver() throws ArchiverException, IOException {
    MavenProject project = new MavenProject(new Model());
    MavenArchiveConfiguration config = new MavenArchiveConfiguration();
    File tempDir = fileManager.createTempDir();
    File manifestFile = fileManager.createFile(tempDir, "MANIFEST.MF", "Main-Class: Stuff\n");
    config.setManifestFile(manifestFile);
    JarArchiver archiver = new JarArchiver();
    archiver.setArchiveFinalizers(Collections.<ArchiveFinalizer>singletonList(new ManifestCreationFinalizer(null, project, config)));
    File file = fileManager.createTempFile();
    archiver.setDestFile(file);
    archiver.createArchive();
    URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
    StringWriter writer = new StringWriter();
    IOUtil.copy(reader, writer);
    assertTrue(writer.toString().contains("Main-Class: Stuff"));
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
    ((JarURLConnection) resource.openConnection()).getJarFile().close();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) Model(org.apache.maven.model.Model) BufferedReader(java.io.BufferedReader) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) URL(java.net.URL)

Example 3 with MavenArchiveConfiguration

use of org.apache.maven.archiver.MavenArchiveConfiguration in project maven-plugins by apache.

the class ManifestCreationFinalizerTest method testShouldAddManifestEntriesWhenArchiverIsJarArchiver.

public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver() throws ArchiverException, IOException {
    MavenProject project = new MavenProject(new Model());
    MavenArchiveConfiguration config = new MavenArchiveConfiguration();
    String testKey = "Test-Key";
    String testValue = "test-value";
    config.addManifestEntry(testKey, testValue);
    JarArchiver archiver = new JarArchiver();
    archiver.setArchiveFinalizers(Collections.<ArchiveFinalizer>singletonList(new ManifestCreationFinalizer(null, project, config)));
    File file = fileManager.createTempFile();
    archiver.setDestFile(file);
    archiver.createArchive();
    URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
    StringWriter writer = new StringWriter();
    IOUtil.copy(reader, writer);
    System.out.println("Test Manifest:\n\n" + writer);
    assertTrue(writer.toString().contains(testKey + ": " + testValue));
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
    ((JarURLConnection) resource.openConnection()).getJarFile().close();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) Model(org.apache.maven.model.Model) BufferedReader(java.io.BufferedReader) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) File(java.io.File) URL(java.net.URL)

Example 4 with MavenArchiveConfiguration

use of org.apache.maven.archiver.MavenArchiveConfiguration in project maven-plugins by apache.

the class ManifestCreationFinalizerTest method testShouldDoNothingWhenArchiverIsNotJarArchiver.

public void testShouldDoNothingWhenArchiverIsNotJarArchiver() throws ArchiverException {
    EasyMockSupport mm = new EasyMockSupport();
    MockAndControlForArchiver macArchiver = new MockAndControlForArchiver(mm);
    MavenProject project = new MavenProject(new Model());
    MavenArchiveConfiguration config = new MavenArchiveConfiguration();
    mm.replayAll();
    new ManifestCreationFinalizer(null, project, config).finalizeArchiveCreation(macArchiver.archiver);
    mm.verifyAll();
}
Also used : EasyMockSupport(org.easymock.classextension.EasyMockSupport) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration)

Aggregations

MavenArchiveConfiguration (org.apache.maven.archiver.MavenArchiveConfiguration)4 Model (org.apache.maven.model.Model)3 MavenProject (org.apache.maven.project.MavenProject)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)2 Date (java.util.Date)1 MavenArchiver (org.apache.maven.archiver.MavenArchiver)1 Artifact (org.apache.maven.artifact.Artifact)1 ArtifactRepositoryLayout (org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout)1 DefaultRepositoryLayout (org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout)1 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)1 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)1 SnapshotVersion (org.apache.maven.artifact.repository.metadata.SnapshotVersion)1 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)1 MetadataXpp3Writer (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1