View Javadoc

1   package org.apache.maven.plugin.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.plugin.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   * @version $Id: ClassesPackager.java 1235053 2012-01-23 23:03:53Z dennisl $
41   */
42  public class ClassesPackager
43  {
44  
45      /**
46       * Creates a new instance.
47       */
48      public ClassesPackager()
49      {
50          super();
51      }
52  
53      /**
54       * Package the classes
55       *
56       * @param classesDirectory     the classes directory
57       * @param targetFile           the target file
58       * @param jarArchiver          the jar archiver to use
59       * @param session              the current session
60       * @param project              the related project
61       * @param archiveConfiguration the archive configuration to use
62       * @throws MojoExecutionException if an error occurred while creating the archive
63       */
64      public void packageClasses( File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenSession session,
65                                  MavenProject project, MavenArchiveConfiguration archiveConfiguration )
66          throws MojoExecutionException
67      {
68  
69          try
70          {
71              final MavenArchiver archiver = new MavenArchiver();
72              archiver.setArchiver( jarArchiver );
73              archiver.setOutputFile( targetFile );
74              archiver.getArchiver().addDirectory( classesDirectory );
75              archiver.createArchive( session, project, archiveConfiguration );
76          }
77          catch ( ArchiverException e )
78          {
79              throw new MojoExecutionException( "Could not create classes archive", e );
80          }
81          catch ( ManifestException e )
82          {
83              throw new MojoExecutionException( "Could not create classes archive", e );
84          }
85          catch ( IOException e )
86          {
87              throw new MojoExecutionException( "Could not create classes archive", e );
88          }
89          catch ( DependencyResolutionRequiredException e )
90          {
91              throw new MojoExecutionException( "Could not create classes archive", e );
92          }
93      }
94  
95      /**
96       * Returns the classes directory from the specified webapp directory.
97       *
98       * @param webappDirectory the webapp directory
99       * @return the classes directory of the specified webapp directory
100      */
101     public File getClassesDirectory( File webappDirectory )
102     {
103         return new File( webappDirectory, AbstractWarPackagingTask.CLASSES_PATH );
104     }
105 }