1 package org.apache.maven.plugin.war.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.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
32
33
34
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
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, overlay.isFiltered() );
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, 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
101
102
103
104
105
106
107
108
109 protected File unpackOverlay( WarPackagingContext context, Overlay overlay )
110 throws MojoExecutionException
111 {
112 final File tmpDir = getOverlayTempDirectory( context, overlay );
113
114
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
129
130
131
132
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 }