View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.war.util;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.archiver.MavenArchiveConfiguration;
25  import org.apache.maven.archiver.MavenArchiver;
26  import org.apache.maven.artifact.DependencyResolutionRequiredException;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.war.packaging.AbstractWarPackagingTask;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.archiver.jar.JarArchiver;
33  import org.codehaus.plexus.archiver.jar.ManifestException;
34  
35  /**
36   * Packages the content of the classes directory.
37   *
38   * @author Stephane Nicoll
39   */
40  public class ClassesPackager {
41  
42      /**
43       * Package the classes
44       *
45       * @param classesDirectory the classes directory
46       * @param targetFile the target file
47       * @param jarArchiver the jar archiver to use
48       * @param session the current session
49       * @param project the related project
50       * @param archiveConfiguration the archive configuration to use
51       * @param outputTimestamp the output timestamp for reproducibility
52       * @throws MojoExecutionException if an error occurred while creating the archive
53       */
54      public void packageClasses(
55              File classesDirectory,
56              File targetFile,
57              JarArchiver jarArchiver,
58              MavenSession session,
59              MavenProject project,
60              MavenArchiveConfiguration archiveConfiguration,
61              String outputTimestamp)
62              throws MojoExecutionException {
63  
64          try {
65              final MavenArchiver archiver = new MavenArchiver();
66              archiver.setArchiver(jarArchiver);
67              archiver.setOutputFile(targetFile);
68              archiver.setCreatedBy("Maven WAR Plugin", "org.apache.maven.plugins", "maven-war-plugin");
69              archiver.configureReproducibleBuild(outputTimestamp);
70              archiver.getArchiver().addDirectory(classesDirectory);
71              archiver.createArchive(session, project, archiveConfiguration);
72          } catch (ArchiverException | ManifestException | IOException | DependencyResolutionRequiredException e) {
73              throw new MojoExecutionException("Could not create classes archive", e);
74          }
75      }
76  
77      /**
78       * Returns the classes directory from the specified webapp directory.
79       *
80       * @param webappDirectory the webapp directory
81       * @return the classes directory of the specified webapp directory
82       */
83      public File getClassesDirectory(File webappDirectory) {
84          return new File(webappDirectory, AbstractWarPackagingTask.CLASSES_PATH);
85      }
86  }