View Javadoc

1   package org.alfresco.maven.plugin.amp.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.alfresco.maven.plugin.amp.Overlay;
24  import org.alfresco.maven.plugin.amp.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   */
35  public class OverlayPackagingTask
36      extends AbstractAmpPackagingTask
37  {
38      private final Overlay overlay;
39  
40  
41      public OverlayPackagingTask( Overlay overlay )
42      {
43          if ( overlay == null )
44          {
45              throw new NullPointerException( "overlay could not be null." );
46          }
47          if ( overlay.equals( Overlay.currentProjectInstance() ) )
48          {
49              throw new IllegalStateException( "Could not handle the current project with this task." );
50          }
51          this.overlay = overlay;
52      }
53  
54  
55      public void performPackaging( AmpPackagingContext context )
56          throws MojoExecutionException
57      {
58          System.out.print( "OverlayPackagingTask performPackaging overlay.getTargetPath() " + 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 );
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 );
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
103      * already been unpacked.
104      *
105      * @param context the packaging context
106      * @param overlay the overlay
107      * @return the directory containing the unpacked overlay
108      * @throws MojoExecutionException if an error occured while unpacking the overlay
109      */
110     protected File unpackOverlay( AmpPackagingContext context, Overlay overlay )
111         throws MojoExecutionException
112     {
113         final File tmpDir = getOverlayTempDirectory( context, overlay );
114 
115         // TODO: not sure it's good, we should reuse the markers of the dependency plugin
116         if ( FileUtils.sizeOfDirectory( tmpDir ) == 0 ||
117             overlay.getArtifact().getFile().lastModified() > tmpDir.lastModified() )
118         {
119             context.getLog().info( "Unpacking overlay[" + overlay + "]" );
120             doUnpack( context, overlay.getArtifact().getFile(), tmpDir );
121         }
122         else
123         {
124             context.getLog().debug( "Overlay[" + overlay + "] was already unpacked" );
125         }
126         return tmpDir;
127     }
128 
129     /***
130      * Returns the directory to use to unpack the specified overlay.
131      *
132      * @param context the packaging context
133      * @param overlay the overlay
134      * @return the temp directory for the overlay
135      */
136     protected File getOverlayTempDirectory( AmpPackagingContext context, Overlay overlay )
137     {
138         final File groupIdDir = new File( context.getOverlaysWorkDirectory(), overlay.getGroupId() );
139         if ( !groupIdDir.exists() )
140         {
141             groupIdDir.mkdir();
142         }
143         final File result = new File( groupIdDir, overlay.getArtifactId() );
144         if ( !result.exists() )
145         {
146             result.mkdirs();
147         }
148         return result;
149     }
150 }