View Javadoc
1   package org.apache.maven.plugins.war;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /**
29   * <p>
30   * An overlay is a skeleton WAR added to another WAR project in order to inject a functionality, resources or any other
31   * shared component.</p>
32   * 
33   * <p>Note that a particular WAR dependency can be added multiple times as an overlay with different includes/excludes
34   * filter; this allows building a fine grained overwriting policy.</p>
35   * 
36   * <p>The current project can also be described as an overlay and can not be specified twice. An overlay with no groupId
37   * and no artifactId represents the current project.</p>
38   *
39   * @author Stephane Nicoll
40   * @version $Id: Overlay.java 1756988 2016-08-20 10:50:13Z khmarbaise $
41   */
42  public class Overlay
43  {
44  
45      /**
46       * The list of default includes.
47       */
48      public static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
49  
50      /**
51       * The list of default excludes.
52       */
53      public static final String[] DEFAULT_EXCLUDES = new String[] { "META-INF/MANIFEST.MF" };
54  
55      private String id;
56  
57      private String groupId;
58  
59      private String artifactId;
60  
61      private String classifier = null;
62  
63      private String[] includes = DEFAULT_INCLUDES;
64  
65      private String[] excludes = DEFAULT_EXCLUDES;
66  
67      private boolean filtered = false;
68  
69      private boolean skip = false;
70  
71      private Artifact artifact;
72  
73      private String targetPath;
74  
75      /** default overlay type is war */
76      private String type = "war";
77  
78      /**
79       * Create instance.
80       */
81      public Overlay()
82      {
83          super();
84      }
85  
86      /**
87       * @param groupId {@link #groupId}
88       * @param artifactId {@link #artifactId}
89       */
90      public Overlay( String groupId, String artifactId )
91      {
92          this();
93          this.groupId = groupId;
94          this.artifactId = artifactId;
95      }
96  
97      /**
98       * Specify whether this overlay represents the current project or not.
99       *
100      * @return true if the overlay represents the current project, false otherwise
101      */
102     public boolean isCurrentProject()
103     {
104         return ( groupId == null && artifactId == null );
105     }
106 
107     /**
108      * @return {@link Overlay} instance.
109      */
110     public static Overlay createInstance()
111     {
112         Overlay overlay = new Overlay();
113         overlay.setId( "currentBuild" );
114         return overlay;
115     }
116 
117     // Getters and Setters
118 
119     /**
120      * @return The id.
121      */
122     public String getId()
123     {
124         if ( id == null )
125         {
126             final StringBuilder sb = new StringBuilder();
127             sb.append( getGroupId() ).append( ":" ).append( getArtifactId() );
128             if ( getClassifier() != null )
129             {
130                 sb.append( ":" ).append( getClassifier() );
131             }
132             id = sb.toString();
133         }
134         return id;
135     }
136 
137     /**
138      * @param id The id.
139      */
140     public void setId( String id )
141     {
142         this.id = id;
143     }
144 
145     /**
146      * @return {@link #groupId}
147      */
148     public String getGroupId()
149     {
150         return groupId;
151     }
152 
153     /**
154      * @param groupId {@link #groupId}
155      */
156     public void setGroupId( String groupId )
157     {
158         this.groupId = groupId;
159     }
160 
161     /**
162      * @return {@link #artifactId}
163      */
164     public String getArtifactId()
165     {
166         return artifactId;
167     }
168 
169     /**
170      * @param artifactId {@link #artifactId}
171      */
172     public void setArtifactId( String artifactId )
173     {
174         this.artifactId = artifactId;
175     }
176 
177     /**
178      * @return {@link #classifier}
179      */
180     public String getClassifier()
181     {
182         return classifier;
183     }
184 
185     /**
186      * @param classifier {@link #classifier}
187      */
188     public void setClassifier( String classifier )
189     {
190         this.classifier = classifier;
191     }
192 
193     /**
194      * @return {@link #includes}
195      */
196     public String[] getIncludes()
197     {
198         return includes;
199     }
200 
201     /**
202      * @param includes {@link #includes}
203      */
204     public void setIncludes( String includes )
205     {
206         this.includes = parse( includes );
207     }
208 
209     /**
210      * @param includes {@link #includes}
211      */
212     public void setIncludes( String[] includes )
213     {
214         this.includes = includes;
215     }
216 
217     /**
218      * @return {@link #excludes}
219      */
220     public String[] getExcludes()
221     {
222         return excludes;
223     }
224 
225     /**
226      * @param excludes {@link #excludes}
227      */
228     public void setExcludes( String excludes )
229     {
230         this.excludes = parse( excludes );
231     }
232 
233     /**
234      * @param excludes {@link #excludes}
235      */
236     public void setExcludes( String[] excludes )
237     {
238         this.excludes = excludes;
239     }
240 
241     /**
242      * @return {@link #filtered}
243      */
244     public boolean isFiltered()
245     {
246         return filtered;
247     }
248 
249     /**
250      * @param filtered {@link #filtered}
251      */
252     public void setFiltered( boolean filtered )
253     {
254         this.filtered = filtered;
255     }
256 
257     /**
258      * @return {@link #skip}
259      */
260     public boolean shouldSkip()
261     {
262         return skip;
263     }
264 
265     /**
266      * @param skip {@link #skip}
267      */
268     public void setSkip( boolean skip )
269     {
270         this.skip = skip;
271     }
272 
273     /**
274      * @return {@link #artifact}
275      */
276     public Artifact getArtifact()
277     {
278         return artifact;
279     }
280 
281     /**
282      * @param artifact {@link #artifact}
283      */
284     public void setArtifact( Artifact artifact )
285     {
286         this.artifact = artifact;
287     }
288 
289     /**
290      * @return {@link #targetPath}
291      */
292     public String getTargetPath()
293     {
294         return targetPath;
295     }
296 
297     /**
298      * @param targetPath {@link #targetPath}
299      */
300     public void setTargetPath( String targetPath )
301     {
302         this.targetPath = targetPath;
303     }
304 
305     /**
306      * @return {@link #type}
307      */
308     public String getType()
309     {
310         return type;
311     }
312 
313     /**
314      * @param type {@link #type}
315      */
316     public void setType( String type )
317     {
318         this.type = type;
319     }
320 
321     /**
322      * {@inheritDoc}
323      */
324     public String toString()
325     {
326         return " id " + getId();
327     }
328 
329     /**
330      * {@inheritDoc}
331      */
332     public boolean equals( Object o )
333     {
334         if ( this == o )
335         {
336             return true;
337         }
338         if ( o == null || getClass() != o.getClass() )
339         {
340             return false;
341         }
342 
343         Overlay overlay = (Overlay) o;
344 
345         if ( excludes != null ? !Arrays.equals( excludes, overlay.excludes ) : overlay.excludes != null )
346         {
347             return false;
348         }
349         if ( getId() != null ? !getId().equals( overlay.getId() ) : overlay.getId() != null )
350         {
351             return false;
352         }
353         if ( includes != null ? !Arrays.equals( includes, overlay.includes ) : overlay.includes != null )
354         {
355             return false;
356         }
357 
358         return true;
359     }
360 
361     /**
362      * {@inheritDoc}
363      */
364     public int hashCode()
365     {
366         int result;
367         result = ( getId() != null ? getId().hashCode() : 0 );
368         result = 31 * result + ( includes != null ? includes.hashCode() : 0 );
369         result = 31 * result + ( excludes != null ? excludes.hashCode() : 0 );
370         return result;
371     }
372 
373     private String[] parse( String s )
374     {
375         final List<String> result = new ArrayList<String>();
376         if ( s == null )
377         {
378             return result.toArray( new String[result.size()] );
379         }
380         else
381         {
382             String[] tokens = s.split( "," );
383             for ( String token : tokens )
384             {
385                 result.add( token.trim() );
386             }
387             return result.toArray( new String[result.size()] );
388         }
389     }
390 
391 }