1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.plugins.war.packaging;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.war.Overlay;
26  import org.apache.maven.plugins.war.util.PathSet;
27  import org.codehaus.plexus.util.FileUtils;
28  
29  
30  
31  
32  
33  
34  public class OverlayPackagingTask extends AbstractWarPackagingTask {
35      private final Overlay overlay;
36  
37      
38  
39  
40  
41      public OverlayPackagingTask(Overlay overlay, Overlay currentProjectOverlay) {
42          if (overlay == null) {
43              throw new NullPointerException("overlay could not be null.");
44          }
45          if (overlay.equals(currentProjectOverlay)) {
46              throw new IllegalStateException("Could not handle the current project with this task.");
47          }
48          this.overlay = overlay;
49      }
50  
51      @Override
52      public void performPackaging(WarPackagingContext context) throws MojoExecutionException {
53          context.getLog()
54                  .debug("OverlayPackagingTask performPackaging overlay.getTargetPath() " + overlay.getTargetPath());
55          if (overlay.shouldSkip()) {
56              context.getLog().info("Skipping overlay [" + overlay + "]");
57          } else {
58              try {
59                  context.getLog().info("Processing overlay [" + overlay + "]");
60  
61                  
62                  final File tmpDir = unpackOverlay(context, overlay);
63  
64                  
65                  final PathSet includes = getFilesToIncludes(tmpDir, overlay.getIncludes(), overlay.getExcludes());
66  
67                  
68                  if (null == overlay.getTargetPath()) {
69                      copyFiles(overlay.getId(), context, tmpDir, includes, overlay.isFiltered());
70                  } else {
71                      
72                      
73                      String targetPath = overlay.getTargetPath();
74                      if (!targetPath.endsWith("/")) {
75                          targetPath = targetPath + "/";
76                      }
77                      copyFiles(overlay.getId(), context, tmpDir, includes, targetPath, overlay.isFiltered());
78                  }
79              } catch (IOException e) {
80                  throw new MojoExecutionException("Failed to copy file for overlay [" + overlay + "]", e);
81              }
82          }
83      }
84  
85      
86  
87  
88  
89  
90  
91  
92  
93  
94  
95      protected File unpackOverlay(WarPackagingContext context, Overlay overlay) throws MojoExecutionException {
96          final File tmpDir = getOverlayTempDirectory(context, overlay);
97  
98          
99          if (FileUtils.sizeOfDirectory(tmpDir) == 0
100                 || overlay.getArtifact().getFile().lastModified() > tmpDir.lastModified()) {
101             doUnpack(context, overlay.getArtifact().getFile(), tmpDir);
102         } else {
103             context.getLog().debug("Overlay [" + overlay + "] was already unpacked");
104         }
105         return tmpDir;
106     }
107 
108     
109 
110 
111 
112 
113 
114 
115     protected File getOverlayTempDirectory(WarPackagingContext context, Overlay overlay) {
116         final File groupIdDir = new File(context.getOverlaysWorkDirectory(), overlay.getGroupId());
117         if (!groupIdDir.exists()) {
118             groupIdDir.mkdir();
119         }
120         String directoryName = overlay.getArtifactId();
121         if (overlay.getClassifier() != null) {
122             directoryName = directoryName + "-" + overlay.getClassifier();
123         }
124         final File result = new File(groupIdDir, directoryName);
125         if (!result.exists()) {
126             result.mkdirs();
127         }
128         return result;
129     }
130 }