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.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.war.packaging.AbstractWarPackagingTask;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  import org.codehaus.plexus.archiver.jar.JarArchiver;
30  import org.codehaus.plexus.archiver.jar.ManifestException;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  /**
36   * Packages the content of the classes directory.
37   *
38   * @author Stephane Nicoll
39   * @version $Id: ClassesPackager.java 682908 2008-08-05 19:57:49Z hboutemy $
40   */
41  public class ClassesPackager
42  {
43  
44      /**
45       * Creates a new instance.
46       */
47      public ClassesPackager()
48      {
49          super();
50      }
51  
52      /**
53       * Package the classes
54       *
55       * @param classesDirectory     the classes directory
56       * @param targetFile           the target file
57       * @param jarArchiver          the jar archiver to use
58       * @param project              the related project
59       * @param archiveConfiguration the archive configuration to use
60       * @throws MojoExecutionException if an error occurred while creating the archive
61       */
62      public void packageClasses( File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenProject project,
63                                  MavenArchiveConfiguration archiveConfiguration )
64          throws MojoExecutionException
65      {
66  
67          try
68          {
69              final MavenArchiver archiver = new MavenArchiver();
70              archiver.setArchiver( jarArchiver );
71              archiver.setOutputFile( targetFile );
72              archiver.getArchiver().addDirectory( classesDirectory );
73              archiver.createArchive( project, archiveConfiguration );
74          }
75          catch ( ArchiverException e )
76          {
77              throw new MojoExecutionException( "Could not create classes archive", e );
78          }
79          catch ( ManifestException e )
80          {
81              throw new MojoExecutionException( "Could not create classes archive", e );
82          }
83          catch ( IOException e )
84          {
85              throw new MojoExecutionException( "Could not create classes archive", e );
86          }
87          catch ( DependencyResolutionRequiredException e )
88          {
89              throw new MojoExecutionException( "Could not create classes archive", e );
90          }
91      }
92  
93      /**
94       * Returns the classes directory from the specified webapp directory.
95       *
96       * @param webappDirectory the webapp directory
97       * @return the classes directory of the specified webapp directory
98       */
99      public File getClassesDirectory( File webappDirectory )
100     {
101         return new File( webappDirectory, AbstractWarPackagingTask.CLASSES_PATH );
102     }
103 }