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;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  
27  /**
28   * <p>
29   * An overlay is a skeleton WAR added to another WAR project in order to inject a functionality, resources or any other
30   * shared component.</p>
31   *
32   * <p>Note that a particular WAR dependency can be added multiple times as an overlay with different includes/excludes
33   * filter; this allows building a fine grained overwriting policy.</p>
34   *
35   * <p>The current project can also be described as an overlay and can not be specified twice. An overlay with no groupId
36   * and no artifactId represents the current project.</p>
37   *
38   * @author Stephane Nicoll
39   */
40  public class Overlay {
41  
42      /**
43       * The list of default includes.
44       */
45      public static final String[] DEFAULT_INCLUDES = new String[] {"**/**"};
46  
47      /**
48       * The list of default excludes.
49       */
50      public static final String[] DEFAULT_EXCLUDES = new String[] {"META-INF/MANIFEST.MF"};
51  
52      private String id;
53  
54      private String groupId;
55  
56      private String artifactId;
57  
58      private String classifier = null;
59  
60      private String[] includes = DEFAULT_INCLUDES;
61  
62      private String[] excludes = DEFAULT_EXCLUDES;
63  
64      private boolean filtered = false;
65  
66      private boolean skip = false;
67  
68      private Artifact artifact;
69  
70      private String targetPath;
71  
72      /** default overlay type is war */
73      private String type = "war";
74  
75      /**
76       * Create instance.
77       */
78      public Overlay() {
79          super();
80      }
81  
82      /**
83       * @param groupId {@link #groupId}
84       * @param artifactId {@link #artifactId}
85       */
86      public Overlay(String groupId, String artifactId) {
87          this();
88          this.groupId = groupId;
89          this.artifactId = artifactId;
90      }
91  
92      /**
93       * Specify whether this overlay represents the current project or not.
94       *
95       * @return true if the overlay represents the current project, false otherwise
96       */
97      public boolean isCurrentProject() {
98          return (groupId == null && artifactId == null);
99      }
100 
101     /**
102      * @return {@link Overlay} instance.
103      */
104     public static Overlay createInstance() {
105         Overlay overlay = new Overlay();
106         overlay.setId("currentBuild");
107         return overlay;
108     }
109 
110     // Getters and Setters
111 
112     /**
113      * @return The id.
114      */
115     public String getId() {
116         if (id == null) {
117             final StringBuilder sb = new StringBuilder();
118             sb.append(getGroupId()).append(":").append(getArtifactId());
119             if (getClassifier() != null) {
120                 sb.append(":").append(getClassifier());
121             }
122             id = sb.toString();
123         }
124         return id;
125     }
126 
127     /**
128      * @param id The id.
129      */
130     public void setId(String id) {
131         this.id = id;
132     }
133 
134     /**
135      * @return {@link #groupId}
136      */
137     public String getGroupId() {
138         return groupId;
139     }
140 
141     /**
142      * @param groupId {@link #groupId}
143      */
144     public void setGroupId(String groupId) {
145         this.groupId = groupId;
146     }
147 
148     /**
149      * @return {@link #artifactId}
150      */
151     public String getArtifactId() {
152         return artifactId;
153     }
154 
155     /**
156      * @param artifactId {@link #artifactId}
157      */
158     public void setArtifactId(String artifactId) {
159         this.artifactId = artifactId;
160     }
161 
162     /**
163      * @return {@link #classifier}
164      */
165     public String getClassifier() {
166         return classifier;
167     }
168 
169     /**
170      * @param classifier {@link #classifier}
171      */
172     public void setClassifier(String classifier) {
173         this.classifier = classifier;
174     }
175 
176     /**
177      * @return {@link #includes}
178      */
179     public String[] getIncludes() {
180         return includes;
181     }
182 
183     /**
184      * @param includes {@link #includes}
185      */
186     public void setIncludes(String includes) {
187         this.includes = parse(includes);
188     }
189 
190     /**
191      * @param includes {@link #includes}
192      */
193     public void setIncludes(String[] includes) {
194         this.includes = includes;
195     }
196 
197     /**
198      * @return {@link #excludes}
199      */
200     public String[] getExcludes() {
201         return excludes;
202     }
203 
204     /**
205      * @param excludes {@link #excludes}
206      */
207     public void setExcludes(String excludes) {
208         this.excludes = parse(excludes);
209     }
210 
211     /**
212      * @param excludes {@link #excludes}
213      */
214     public void setExcludes(String[] excludes) {
215         this.excludes = excludes;
216     }
217 
218     /**
219      * @return {@link #filtered}
220      */
221     public boolean isFiltered() {
222         return filtered;
223     }
224 
225     /**
226      * @param filtered {@link #filtered}
227      */
228     public void setFiltered(boolean filtered) {
229         this.filtered = filtered;
230     }
231 
232     /**
233      * @return {@link #skip}
234      */
235     public boolean shouldSkip() {
236         return skip;
237     }
238 
239     /**
240      * @param skip {@link #skip}
241      */
242     public void setSkip(boolean skip) {
243         this.skip = skip;
244     }
245 
246     /**
247      * @return {@link #artifact}
248      */
249     public Artifact getArtifact() {
250         return artifact;
251     }
252 
253     /**
254      * @param artifact {@link #artifact}
255      */
256     public void setArtifact(Artifact artifact) {
257         this.artifact = artifact;
258     }
259 
260     /**
261      * @return {@link #targetPath}
262      */
263     public String getTargetPath() {
264         return targetPath;
265     }
266 
267     /**
268      * @param targetPath {@link #targetPath}
269      */
270     public void setTargetPath(String targetPath) {
271         this.targetPath = targetPath;
272     }
273 
274     /**
275      * @return {@link #type}
276      */
277     public String getType() {
278         return type;
279     }
280 
281     /**
282      * @param type {@link #type}
283      */
284     public void setType(String type) {
285         this.type = type;
286     }
287 
288     @Override
289     public String toString() {
290         return " id " + getId();
291     }
292 
293     @Override
294     public boolean equals(Object o) {
295         if (this == o) {
296             return true;
297         }
298         if (o == null || getClass() != o.getClass()) {
299             return false;
300         }
301 
302         Overlay overlay = (Overlay) o;
303 
304         if (excludes != null ? !Arrays.equals(excludes, overlay.excludes) : overlay.excludes != null) {
305             return false;
306         }
307         if (getId() != null ? !getId().equals(overlay.getId()) : overlay.getId() != null) {
308             return false;
309         }
310         if (includes != null ? !Arrays.equals(includes, overlay.includes) : overlay.includes != null) {
311             return false;
312         }
313 
314         return true;
315     }
316 
317     @Override
318     public int hashCode() {
319         int result;
320         result = (getId() != null ? getId().hashCode() : 0);
321         result = 31 * result + (includes != null ? includes.hashCode() : 0);
322         result = 31 * result + (excludes != null ? excludes.hashCode() : 0);
323         return result;
324     }
325 
326     private String[] parse(String s) {
327         final List<String> result = new ArrayList<>();
328         if (s == null) {
329             return result.toArray(new String[result.size()]);
330         } else {
331             String[] tokens = s.split(",");
332             for (String token : tokens) {
333                 result.add(token.trim());
334             }
335             return result.toArray(new String[result.size()]);
336         }
337     }
338 }