1 package org.apache.maven.plugins.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.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
32
33
34
35
36 public class OverlayPackagingTask
37 extends AbstractWarPackagingTask
38 {
39 private final Overlay overlay;
40
41
42
43
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
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
77 final File tmpDir = unpackOverlay( context, overlay );
78
79
80 final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() );
81
82
83 if ( null == overlay.getTargetPath() )
84 {
85 copyFiles( overlay.getId(), context, tmpDir, includes, overlay.isFiltered() );
86 }
87 else
88 {
89
90
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
108
109
110
111
112
113
114
115
116 protected File unpackOverlay( WarPackagingContext context, Overlay overlay )
117 throws MojoExecutionException
118 {
119 final File tmpDir = getOverlayTempDirectory( context, overlay );
120
121
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
136
137
138
139
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 }