1 package org.alfresco.maven.plugin.amp.packaging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
70 final File tmpDir = unpackOverlay( context, overlay );
71
72
73 final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() );
74
75
76 if ( null == overlay.getTargetPath() )
77 {
78 copyFiles( overlay.getId(), context, tmpDir, includes );
79 }
80 else
81 {
82
83
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
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 }