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