1 package org.apache.maven.plugin.war.overlay;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.ListIterator;
26 import java.util.Set;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
30 import org.apache.maven.plugin.war.Overlay;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40 public class OverlayManager
41 {
42 private final List<Overlay> overlays;
43
44 private final MavenProject project;
45
46 private final List<Artifact> artifactsOverlays;
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public OverlayManager( List<Overlay> overlays, MavenProject project, String defaultIncludes,
61 String defaultExcludes, Overlay currentProjectOverlay )
62 throws InvalidOverlayConfigurationException
63 {
64 this.overlays = new ArrayList<Overlay>();
65 if ( overlays != null )
66 {
67 this.overlays.addAll( overlays );
68 }
69 this.project = project;
70
71 this.artifactsOverlays = getOverlaysAsArtifacts();
72
73
74 initialize( defaultIncludes, defaultExcludes, currentProjectOverlay );
75
76 }
77
78
79
80
81
82
83 public List<Overlay> getOverlays()
84 {
85 return overlays;
86 }
87
88
89
90
91
92
93 public List<String> getOverlayIds()
94 {
95 final List<String> result = new ArrayList<String>();
96 for ( Overlay overlay : overlays )
97 {
98 result.add( overlay.getId() );
99 }
100 return result;
101
102 }
103
104
105
106
107
108
109
110
111
112 void initialize( String defaultIncludes, String defaultExcludes, Overlay currentProjectOverlay )
113 throws InvalidOverlayConfigurationException
114 {
115
116
117
118 final List<Artifact> configuredWarArtifacts = new ArrayList<Artifact>();
119 final ListIterator<Overlay> it = overlays.listIterator();
120 while ( it.hasNext() )
121 {
122 Overlay overlay = it.next();
123 if ( overlay == null )
124 {
125 throw new InvalidOverlayConfigurationException( "overlay could not be null." );
126 }
127
128 if ( overlay.isCurrentProject() )
129 {
130 overlay = currentProjectOverlay;
131 it.set( overlay );
132 }
133
134 if ( Arrays.equals( Overlay.DEFAULT_INCLUDES, overlay.getIncludes() )
135 && Arrays.equals( Overlay.DEFAULT_EXCLUDES, overlay.getExcludes() ) )
136 {
137 overlay.setIncludes( defaultIncludes );
138 overlay.setExcludes( defaultExcludes );
139 }
140
141 final Artifact artifact = getAssociatedArtifact( overlay );
142 if ( artifact != null )
143 {
144 configuredWarArtifacts.add( artifact );
145 overlay.setArtifact( artifact );
146 }
147 }
148
149
150 for ( Artifact artifact : artifactsOverlays )
151 {
152 if ( !configuredWarArtifacts.contains( artifact ) )
153 {
154
155
156 overlays.add( new DefaultOverlay( artifact, defaultIncludes, defaultExcludes ) );
157 }
158 }
159
160
161 for ( Overlay overlay : overlays )
162 {
163 if ( overlay.equals( currentProjectOverlay ) )
164 {
165 return;
166 }
167 }
168 overlays.add( 0, currentProjectOverlay );
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182 Artifact getAssociatedArtifact( final Overlay overlay )
183 throws InvalidOverlayConfigurationException
184 {
185 if ( overlay.isCurrentProject() )
186 {
187 return null;
188 }
189
190 for ( Artifact artifact : artifactsOverlays )
191 {
192
193 if ( compareOverlayWithArtifact( overlay, artifact ) )
194 {
195 return artifact;
196 }
197 }
198
199
200 @SuppressWarnings( "unchecked" )
201 Set<Artifact> projectArtifacts = this.project.getDependencyArtifacts();
202 if ( projectArtifacts != null )
203 {
204 for ( Artifact artifact : projectArtifacts )
205 {
206 if ( compareOverlayWithArtifact( overlay, artifact ) )
207 {
208 return artifact;
209 }
210 }
211 }
212 throw new InvalidOverlayConfigurationException( "overlay [" + overlay + "] is not a dependency of the project." );
213
214 }
215
216
217
218
219
220
221
222
223 private boolean compareOverlayWithArtifact( Overlay overlay, Artifact artifact )
224 {
225 return ( StringUtils.equals( overlay.getGroupId(), artifact.getGroupId() )
226 && StringUtils.equals( overlay.getArtifactId(), artifact.getArtifactId() )
227 && StringUtils.equals( overlay.getType(), artifact.getType() )
228
229 && StringUtils.equals( StringUtils.defaultString( overlay.getClassifier() ),
230 StringUtils.defaultString( artifact.getClassifier() ) ) );
231 }
232
233
234
235
236
237
238 private List<Artifact> getOverlaysAsArtifacts()
239 {
240 ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
241 @SuppressWarnings( "unchecked" )
242 final Set<Artifact> artifacts = project.getArtifacts();
243
244 final List<Artifact> result = new ArrayList<Artifact>();
245 for ( Artifact artifact : artifacts )
246 {
247 if ( !artifact.isOptional() && filter.include( artifact ) && ( "war".equals( artifact.getType() ) ) )
248 {
249 result.add( artifact );
250 }
251 }
252 return result;
253 }
254 }