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