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 public class OverlayPackagingTask
36 extends AbstractWarPackagingTask
37 {
38 private final Overlay overlay;
39
40
41
42
43
44 public OverlayPackagingTask( Overlay overlay, Overlay currentProjectOverlay )
45 {
46 if ( overlay == null )
47 {
48 throw new NullPointerException( "overlay could not be null." );
49 }
50 if ( overlay.equals( currentProjectOverlay ) )
51 {
52 throw new IllegalStateException( "Could not handle the current project with this task." );
53 }
54 this.overlay = overlay;
55 }
56
57 @Override
58 public void performPackaging( WarPackagingContext context )
59 throws MojoExecutionException
60 {
61 context.getLog().debug( "OverlayPackagingTask performPackaging overlay.getTargetPath() "
62 + overlay.getTargetPath() );
63 if ( overlay.shouldSkip() )
64 {
65 context.getLog().info( "Skipping overlay [" + overlay + "]" );
66 }
67 else
68 {
69 try
70 {
71 context.getLog().info( "Processing overlay [" + overlay + "]" );
72
73
74 final File tmpDir = unpackOverlay( context, overlay );
75
76
77 final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() );
78
79
80 if ( null == overlay.getTargetPath() )
81 {
82 copyFiles( overlay.getId(), context, tmpDir, includes, overlay.isFiltered() );
83 }
84 else
85 {
86
87
88 String targetPath = overlay.getTargetPath();
89 if ( !targetPath.endsWith( "/" ) )
90 {
91 targetPath = targetPath + "/";
92 }
93 copyFiles( overlay.getId(), context, tmpDir, includes, targetPath, overlay.isFiltered() );
94 }
95 }
96 catch ( IOException e )
97 {
98 throw new MojoExecutionException( "Failed to copy file for overlay [" + overlay + "]", e );
99 }
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112
113 protected File unpackOverlay( WarPackagingContext context, Overlay overlay )
114 throws MojoExecutionException
115 {
116 final File tmpDir = getOverlayTempDirectory( context, overlay );
117
118
119 if ( FileUtils.sizeOfDirectory( tmpDir ) == 0
120 || overlay.getArtifact().getFile().lastModified() > tmpDir.lastModified() )
121 {
122 doUnpack( context, overlay.getArtifact().getFile(), tmpDir );
123 }
124 else
125 {
126 context.getLog().debug( "Overlay [" + overlay + "] was already unpacked" );
127 }
128 return tmpDir;
129 }
130
131
132
133
134
135
136
137
138 protected File getOverlayTempDirectory( WarPackagingContext context, Overlay overlay )
139 {
140 final File groupIdDir = new File( context.getOverlaysWorkDirectory(), overlay.getGroupId() );
141 if ( !groupIdDir.exists() )
142 {
143 groupIdDir.mkdir();
144 }
145 String directoryName = overlay.getArtifactId();
146 if ( overlay.getClassifier() != null )
147 {
148 directoryName = directoryName + "-" + overlay.getClassifier();
149 }
150 final File result = new File( groupIdDir, directoryName );
151 if ( !result.exists() )
152 {
153 result.mkdirs();
154 }
155 return result;
156 }
157 }