Search in sources :

Example 6 with MavenArchiver

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

the class WarMojo method performPackaging.

/**
     * Generates the webapp according to the <tt>mode</tt> attribute.
     *
     * @param warFile the target WAR file
     * @throws IOException if an error occurred while copying files
     * @throws ArchiverException if the archive could not be created
     * @throws ManifestException if the manifest could not be created
     * @throws DependencyResolutionRequiredException if an error occurred while resolving the dependencies
     * @throws MojoExecutionException if the execution failed
     * @throws MojoFailureException if a fatal exception occurred
     */
private void performPackaging(File warFile) throws IOException, ManifestException, DependencyResolutionRequiredException, MojoExecutionException, MojoFailureException {
    getLog().info("Packaging webapp");
    buildExplodedWebapp(getWebappDirectory());
    MavenArchiver archiver = new MavenArchiver();
    archiver.setArchiver(warArchiver);
    archiver.setOutputFile(warFile);
    // CHECKSTYLE_OFF: LineLength
    getLog().debug("Excluding " + Arrays.asList(getPackagingExcludes()) + " from the generated webapp archive.");
    getLog().debug("Including " + Arrays.asList(getPackagingIncludes()) + " in the generated webapp archive.");
    // CHECKSTYLE_ON: LineLength
    warArchiver.addDirectory(getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes());
    final File webXmlFile = new File(getWebappDirectory(), "WEB-INF/web.xml");
    if (webXmlFile.exists()) {
        warArchiver.setWebxml(webXmlFile);
    }
    warArchiver.setRecompressAddedZips(isRecompressZippedFiles());
    warArchiver.setIncludeEmptyDirs(isIncludeEmptyDirectories());
    if (Boolean.FALSE.equals(failOnMissingWebXml) || (failOnMissingWebXml == null && isProjectUsingAtLeastServlet30())) {
        getLog().debug("Build won't fail if web.xml file is missing.");
        warArchiver.setExpectWebXml(false);
    }
    // create archive
    archiver.createArchive(getSession(), getProject(), getArchive());
    // create the classes to be attached if necessary
    if (isAttachClasses()) {
        if (isArchiveClasses() && getJarArchiver().getDestFile() != null) {
            // special handling in case of archived classes: MWAR-240
            File targetClassesFile = getTargetClassesFile();
            FileUtils.copyFile(getJarArchiver().getDestFile(), targetClassesFile);
            projectHelper.attachArtifact(getProject(), "jar", getClassesClassifier(), targetClassesFile);
        } else {
            ClassesPackager packager = new ClassesPackager();
            final File classesDirectory = packager.getClassesDirectory(getWebappDirectory());
            if (classesDirectory.exists()) {
                getLog().info("Packaging classes");
                packager.packageClasses(classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(), getProject(), getArchive());
                projectHelper.attachArtifact(getProject(), "jar", getClassesClassifier(), getTargetClassesFile());
            }
        }
    }
    if (this.classifier != null) {
        projectHelper.attachArtifact(getProject(), "war", this.classifier, warFile);
    } else {
        Artifact artifact = getProject().getArtifact();
        if (primaryArtifact) {
            artifact.setFile(warFile);
        } else if (artifact.getFile() == null || artifact.getFile().isDirectory()) {
            artifact.setFile(warFile);
        }
    }
}
Also used : ClassesPackager(org.apache.maven.plugins.war.util.ClassesPackager) MavenArchiver(org.apache.maven.archiver.MavenArchiver) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 7 with MavenArchiver

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

the class ClassesPackager method packageClasses.

/**
     * Package the classes
     *
     * @param classesDirectory the classes directory
     * @param targetFile the target file
     * @param jarArchiver the jar archiver to use
     * @param session the current session
     * @param project the related project
     * @param archiveConfiguration the archive configuration to use
     * @throws MojoExecutionException if an error occurred while creating the archive
     */
public void packageClasses(File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenSession session, MavenProject project, MavenArchiveConfiguration archiveConfiguration) throws MojoExecutionException {
    try {
        final MavenArchiver archiver = new MavenArchiver();
        archiver.setArchiver(jarArchiver);
        archiver.setOutputFile(targetFile);
        archiver.getArchiver().addDirectory(classesDirectory);
        archiver.createArchive(session, project, archiveConfiguration);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (ManifestException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) MavenArchiver(org.apache.maven.archiver.MavenArchiver) IOException(java.io.IOException) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException)

Example 8 with MavenArchiver

use of org.apache.maven.archiver.MavenArchiver in project jangaroo-tools by CoreMedia.

the class PackageMojo method execute.

public void execute() throws MojoExecutionException {
    File jarFile = new File(targetDir, finalName + "." + Types.JAVASCRIPT_EXTENSION);
    MavenArchiver mavenArchiver = new MavenArchiver();
    mavenArchiver.setArchiver(archiver);
    mavenArchiver.setOutputFile(jarFile);
    try {
        if (archive.getManifestFile() == null) {
            if (useDefaultManifestFile && defaultManifestFile.exists()) {
                getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
                archive.setManifestFile(defaultManifestFile);
            } else if (manifest != null) {
                archive.setManifestFile(manifest);
            } else {
                archive.setManifestFile(createDefaultManifest(project));
            }
        }
        if (outputDirectory.exists()) {
            mavenArchiver.getArchiver().addDirectory(outputDirectory);
            if (!getModuleJsFile().exists() && new File(outputDirectory, moduleClassesJsFile).exists()) {
                createDefaultModuleJsFile();
            }
        }
        String groupId = project.getGroupId();
        String artifactId = project.getArtifactId();
        mavenArchiver.getArchiver().addFile(project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
        mavenArchiver.createArchive(project, archive);
    } catch (Exception e) {
        // NOSONAR
        throw new MojoExecutionException("Failed to create the javascript archive", e);
    }
    project.getArtifact().setFile(jarFile);
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenArchiver(org.apache.maven.archiver.MavenArchiver) File(java.io.File) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException)

Example 9 with MavenArchiver

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

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

the class AbstractJarMojo method createArchive.

/**
     * Generates the JAR.
     * @return The instance of File for the created archive file.
     * @throws MojoExecutionException in case of an error.
     */
public File createArchive() throws MojoExecutionException {
    File jarFile = getJarFile(outputDirectory, finalName, getClassifier());
    MavenArchiver archiver = new MavenArchiver();
    archiver.setArchiver(jarArchiver);
    archiver.setOutputFile(jarFile);
    archive.setForced(forceCreation);
    try {
        File contentDirectory = getClassesDirectory();
        if (!contentDirectory.exists()) {
            getLog().warn("JAR will be empty - no content was marked for inclusion!");
        } else {
            archiver.getArchiver().addDirectory(contentDirectory, getIncludes(), getExcludes());
        }
        archiver.createArchive(session, project, archive);
        return jarFile;
    } catch (Exception e) {
        // TODO: improve error handling
        throw new MojoExecutionException("Error assembling JAR", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenArchiver(org.apache.maven.archiver.MavenArchiver) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

MavenArchiver (org.apache.maven.archiver.MavenArchiver)14 File (java.io.File)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 IOException (java.io.IOException)8 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)8 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)8 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)6 MavenFilteringException (org.apache.maven.shared.filtering.MavenFilteringException)4 Artifact (org.apache.maven.artifact.Artifact)3 Resource (org.apache.maven.model.Resource)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Date (java.util.Date)1 ZipException (java.util.zip.ZipException)1 MavenArchiveConfiguration (org.apache.maven.archiver.MavenArchiveConfiguration)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