View Javadoc
1   package org.apache.maven.plugins.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.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.war.Overlay;
24  import org.apache.maven.plugins.war.util.PathSet;
25  import org.codehaus.plexus.util.FileUtils;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Handles an overlay.
32   *
33   * @author Stephane Nicoll
34   * @version $Id$
35   */
36  public class OverlayPackagingTask
37      extends AbstractWarPackagingTask
38  {
39      private final Overlay overlay;
40  
41      /**
42       * @param overlay {@link #overlay}
43       * @param currentProjectOverlay current overlay.
44       */
45      public OverlayPackagingTask( Overlay overlay, Overlay currentProjectOverlay )
46      {
47          if ( overlay == null )
48          {
49              throw new NullPointerException( "overlay could not be null." );
50          }
51          if ( overlay.equals( currentProjectOverlay ) )
52          {
53              throw new IllegalStateException( "Could not handle the current project with this task." );
54          }
55          this.overlay = overlay;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public void performPackaging( WarPackagingContext context )
62          throws MojoExecutionException
63      {
64          context.getLog().debug( "OverlayPackagingTask performPackaging overlay.getTargetPath() "
65                                      + overlay.getTargetPath() );
66          if ( overlay.shouldSkip() )
67          {
68              context.getLog().info( "Skipping overlay [" + overlay + "]" );
69          }
70          else
71          {
72              try
73              {
74                  context.getLog().info( "Processing overlay [" + overlay + "]" );
75  
76                  // Step1: Extract if necessary
77                  final File tmpDir = unpackOverlay( context, overlay );
78  
79                  // Step2: setup
80                  final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() );
81  
82                  // Copy
83                  if ( null == overlay.getTargetPath() )
84                  {
85                      copyFiles( overlay.getId(), context, tmpDir, includes, overlay.isFiltered() );
86                  }
87                  else
88                  {
89                      // overlay.getTargetPath() must ended with /
90                      // if not we add it
91                      String targetPath = overlay.getTargetPath();
92                      if ( !targetPath.endsWith( "/" ) )
93                      {
94                          targetPath = targetPath + "/";
95                      }
96                      copyFiles( overlay.getId(), context, tmpDir, includes, targetPath, overlay.isFiltered() );
97                  }
98              }
99              catch ( IOException e )
100             {
101                 throw new MojoExecutionException( "Failed to copy file for overlay [" + overlay + "]", e );
102             }
103         }
104     }
105 
106     /**
107      * Unpacks the specified overlay.
108      * 
109      * Makes sure to skip the unpack process if the overlay has already been unpacked.
110      *
111      * @param context the packaging context
112      * @param overlay the overlay
113      * @return the directory containing the unpacked overlay
114      * @throws MojoExecutionException if an error occurred while unpacking the overlay
115      */
116     protected File unpackOverlay( WarPackagingContext context, Overlay overlay )
117         throws MojoExecutionException
118     {
119         final File tmpDir = getOverlayTempDirectory( context, overlay );
120 
121         // TODO: not sure it's good, we should reuse the markers of the dependency plugin
122         if ( FileUtils.sizeOfDirectory( tmpDir ) == 0
123             || overlay.getArtifact().getFile().lastModified() > tmpDir.lastModified() )
124         {
125             doUnpack( context, overlay.getArtifact().getFile(), tmpDir );
126         }
127         else
128         {
129             context.getLog().debug( "Overlay [" + overlay + "] was already unpacked" );
130         }
131         return tmpDir;
132     }
133 
134     /**
135      * Returns the directory to use to unpack the specified overlay.
136      *
137      * @param context the packaging context
138      * @param overlay the overlay
139      * @return the temp directory for the overlay
140      */
141     protected File getOverlayTempDirectory( WarPackagingContext context, Overlay overlay )
142     {
143         final File groupIdDir = new File( context.getOverlaysWorkDirectory(), overlay.getGroupId() );
144         if ( !groupIdDir.exists() )
145         {
146             groupIdDir.mkdir();
147         }
148         String directoryName = overlay.getArtifactId();
149         if ( overlay.getClassifier() != null )
150         {
151             directoryName = directoryName + "-" + overlay.getClassifier();
152         }
153         final File result = new File( groupIdDir, directoryName );
154         if ( !result.exists() )
155         {
156             result.mkdirs();
157         }
158         return result;
159     }
160 }