View Javadoc

1   package org.apache.maven.plugin.war.packaging;
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.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.war.Overlay;
26  import org.apache.maven.plugin.war.util.ClassesPackager;
27  import org.apache.maven.plugin.war.util.PathSet;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.interpolation.InterpolationException;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  /**
35   * Handles the classes directory that needs to be packaged in the web application.
36   * <p/>
37   * Based on the {@link WarPackagingContext#archiveClasses()} flag the resources
38   * either copied into to <tt>WEB-INF/classes</tt> directory or archived in a jar
39   * within the <tt>WEB-INF/lib</tt> directory.
40   *
41   * @author Stephane Nicoll
42   *
43   * @version $Id: ClassesPackagingTask.html 867902 2013-06-30 13:58:43Z olamy $
44   */
45  public class ClassesPackagingTask
46      extends AbstractWarPackagingTask
47  {
48      private final Overlay currentProjectOverlay;
49  
50      public ClassesPackagingTask( Overlay currentProjectOverlay )
51      {
52          this.currentProjectOverlay = currentProjectOverlay;
53      }
54  
55      public void performPackaging( WarPackagingContext context )
56          throws MojoExecutionException
57      {
58          final File webappClassesDirectory = new File( context.getWebappDirectory(), CLASSES_PATH );
59          if ( !webappClassesDirectory.exists() )
60          {
61              webappClassesDirectory.mkdirs();
62          }
63  
64          if ( context.getClassesDirectory().exists() && !context.getClassesDirectory().equals( webappClassesDirectory ) )
65          {
66              if ( context.archiveClasses() )
67              {
68                  generateJarArchive( context );
69              }
70              else
71              {
72                  final PathSet sources = getFilesToIncludes( context.getClassesDirectory(), null, null );
73                  try
74                  {
75                      copyFiles( currentProjectOverlay.getId(), context, context.getClassesDirectory(),
76                                 sources, CLASSES_PATH, false );
77                  }
78                  catch ( IOException e )
79                  {
80                      throw new MojoExecutionException(
81                          "Could not copy webapp classes [" + context.getClassesDirectory().getAbsolutePath() + "]", e );
82                  }
83              }
84          }
85      }
86  
87      protected void generateJarArchive( WarPackagingContext context )
88          throws MojoExecutionException
89      {
90          MavenProject project = context.getProject();
91          ArtifactFactory factory = context.getArtifactFactory();
92          Artifact artifact = factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(),
93                                                           project.getVersion(), "jar" );
94          String archiveName = null;
95          try
96          {
97              archiveName = getArtifactFinalName( context, artifact );
98          }
99          catch ( InterpolationException e )
100         {
101             throw new MojoExecutionException(
102                 "Could not get the final name of the artifact [" + artifact.getGroupId() + ":" + artifact.getArtifactId()
103                     + ":" + artifact.getVersion() + "]", e );
104         }
105         final String targetFilename = LIB_PATH + archiveName;
106 
107         if ( context.getWebappStructure().registerFile( currentProjectOverlay.getId(), targetFilename ) )
108         {
109             final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
110             final File jarFile = new File( libDirectory, archiveName );
111             final ClassesPackager packager = new ClassesPackager();
112             packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
113                                      context.getSession(), project, context.getArchive() );
114         }
115         else
116         {
117             context.getLog().warn(
118                 "Could not generate archive classes file [" + targetFilename + "] has already been copied." );
119         }
120     }
121 }