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