Search in sources :

Example 1 with ClassesPackager

use of org.apache.maven.plugins.war.util.ClassesPackager 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 2 with ClassesPackager

use of org.apache.maven.plugins.war.util.ClassesPackager in project maven-plugins by apache.

the class ClassesPackagingTask method generateJarArchive.

/**
     * @param context The warPackingContext.
     * @throws MojoExecutionException In casae of an error.
     */
protected void generateJarArchive(WarPackagingContext context) throws MojoExecutionException {
    MavenProject project = context.getProject();
    ArtifactFactory factory = context.getArtifactFactory();
    Artifact artifact = factory.createBuildArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), "jar");
    String archiveName;
    try {
        archiveName = getArtifactFinalName(context, artifact);
    } catch (InterpolationException e) {
        throw new MojoExecutionException("Could not get the final name of the artifact [" + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + "]", e);
    }
    final String targetFilename = LIB_PATH + archiveName;
    if (context.getWebappStructure().registerFile(currentProjectOverlay.getId(), targetFilename)) {
        final File libDirectory = new File(context.getWebappDirectory(), LIB_PATH);
        final File jarFile = new File(libDirectory, archiveName);
        final ClassesPackager packager = new ClassesPackager();
        packager.packageClasses(context.getClassesDirectory(), jarFile, context.getJarArchiver(), context.getSession(), project, context.getArchive());
    } else {
        context.getLog().warn("Could not generate archive classes file [" + targetFilename + "] has already been copied.");
    }
}
Also used : ClassesPackager(org.apache.maven.plugins.war.util.ClassesPackager) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

File (java.io.File)2 Artifact (org.apache.maven.artifact.Artifact)2 ClassesPackager (org.apache.maven.plugins.war.util.ClassesPackager)2 MavenArchiver (org.apache.maven.archiver.MavenArchiver)1 ArtifactFactory (org.apache.maven.artifact.factory.ArtifactFactory)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenProject (org.apache.maven.project.MavenProject)1 InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)1