View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Handles an overlay.
31   *
32   * @author Stephane Nicoll
33   */
34  public class OverlayPackagingTask extends AbstractWarPackagingTask {
35      private final Overlay overlay;
36  
37      /**
38       * @param overlay {@link #overlay}
39       * @param currentProjectOverlay current overlay.
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                  // Step1: Extract if necessary
62                  final File tmpDir = unpackOverlay(context, overlay);
63  
64                  // Step2: setup
65                  final PathSet includes = getFilesToIncludes(tmpDir, overlay.getIncludes(), overlay.getExcludes());
66  
67                  // Copy
68                  if (null == overlay.getTargetPath()) {
69                      copyFiles(overlay.getId(), context, tmpDir, includes, overlay.isFiltered());
70                  } else {
71                      // overlay.getTargetPath() must ended with /
72                      // if not we add it
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       * Unpacks the specified overlay.
87       *
88       * Makes sure to skip the unpack process if the overlay has already been unpacked.
89       *
90       * @param context the packaging context
91       * @param overlay the overlay
92       * @return the directory containing the unpacked overlay
93       * @throws MojoExecutionException if an error occurred while unpacking the overlay
94       */
95      protected File unpackOverlay(WarPackagingContext context, Overlay overlay) throws MojoExecutionException {
96          final File tmpDir = getOverlayTempDirectory(context, overlay);
97  
98          // TODO: not sure it's good, we should reuse the markers of the dependency plugin
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      * Returns the directory to use to unpack the specified overlay.
110      *
111      * @param context the packaging context
112      * @param overlay the overlay
113      * @return the temp directory for the overlay
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 }