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