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