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