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 | { |
67 | 67 | this.overlays = new ArrayList<Overlay>(); |
68 | 67 | if ( overlays != null ) |
69 | |
{ |
70 | 67 | this.overlays.addAll( overlays ); |
71 | |
} |
72 | 67 | this.project = project; |
73 | |
|
74 | 67 | this.artifactsOverlays = getOverlaysAsArtifacts(); |
75 | |
|
76 | |
|
77 | 67 | initialize( defaultIncludes, defaultExcludes, currentProjectOverlay ); |
78 | |
|
79 | 66 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public List<Overlay> getOverlays() |
88 | |
{ |
89 | 79 | return overlays; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public List<String> getOverlayIds() |
98 | |
{ |
99 | 62 | final List<String> result = new ArrayList<String>(); |
100 | 62 | for ( Overlay overlay : overlays ) |
101 | |
{ |
102 | 96 | result.add( overlay.getId() ); |
103 | |
} |
104 | 62 | 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 | 67 | final List<Artifact> configuredWarArtifacts = new ArrayList<Artifact>(); |
124 | 67 | final ListIterator<Overlay> it = overlays.listIterator(); |
125 | 99 | while ( it.hasNext() ) |
126 | |
{ |
127 | 33 | Overlay overlay = (Overlay) it.next(); |
128 | 33 | if ( overlay == null ) |
129 | |
{ |
130 | 0 | throw new InvalidOverlayConfigurationException( "overlay could not be null." ); |
131 | |
} |
132 | |
|
133 | 33 | if ( overlay.isCurrentProject() ) |
134 | |
{ |
135 | 4 | overlay = currentProjectOverlay; |
136 | 4 | it.set( overlay ); |
137 | |
} |
138 | |
|
139 | 33 | if ( Arrays.equals( Overlay.DEFAULT_INCLUDES, overlay.getIncludes() ) |
140 | |
&& Arrays.equals( Overlay.DEFAULT_EXCLUDES, overlay.getExcludes() ) ) |
141 | |
{ |
142 | 27 | overlay.setIncludes( defaultIncludes ); |
143 | 27 | overlay.setExcludes( defaultExcludes ); |
144 | |
} |
145 | |
|
146 | 33 | final Artifact artifact = getAssociatedArtifact( overlay ); |
147 | 32 | if ( artifact != null ) |
148 | |
{ |
149 | 28 | configuredWarArtifacts.add( artifact ); |
150 | 28 | overlay.setArtifact( artifact ); |
151 | |
} |
152 | 32 | } |
153 | |
|
154 | |
|
155 | 66 | for ( Artifact artifact : artifactsOverlays ) |
156 | |
{ |
157 | 34 | if ( !configuredWarArtifacts.contains( artifact ) ) |
158 | |
{ |
159 | |
|
160 | |
|
161 | 11 | overlays.add( new DefaultOverlay( artifact, defaultIncludes, defaultExcludes ) ); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
|
166 | 66 | for ( Overlay overlay : overlays ) |
167 | |
{ |
168 | 37 | if ( overlay.equals( currentProjectOverlay ) ) |
169 | |
{ |
170 | 4 | return; |
171 | |
} |
172 | |
} |
173 | 62 | overlays.add( 0, currentProjectOverlay ); |
174 | 62 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
Artifact getAssociatedArtifact( final Overlay overlay ) |
189 | |
throws InvalidOverlayConfigurationException |
190 | |
{ |
191 | 33 | if ( overlay.isCurrentProject() ) |
192 | |
{ |
193 | 4 | return null; |
194 | |
} |
195 | |
|
196 | 29 | for ( Artifact artifact : artifactsOverlays ) |
197 | |
{ |
198 | |
|
199 | 46 | if ( compareOverlayWithArtifact( overlay, artifact ) ) |
200 | |
{ |
201 | 25 | return artifact; |
202 | |
} |
203 | |
} |
204 | |
|
205 | |
|
206 | |
@SuppressWarnings( "unchecked" ) |
207 | 4 | Set<Artifact> projectArtifacts = this.project.getDependencyArtifacts(); |
208 | 4 | if ( projectArtifacts != null ) |
209 | |
{ |
210 | 3 | for ( Artifact artifact : projectArtifacts ) |
211 | |
{ |
212 | 3 | if ( compareOverlayWithArtifact( overlay, artifact ) ) |
213 | |
{ |
214 | 3 | return artifact; |
215 | |
} |
216 | |
} |
217 | |
} |
218 | 1 | 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 | 49 | 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 | 67 | ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME ); |
248 | |
@SuppressWarnings( "unchecked" ) |
249 | 67 | final Set<Artifact> artifacts = project.getArtifacts(); |
250 | |
|
251 | 67 | final List<Artifact> result = new ArrayList<Artifact>(); |
252 | 67 | for ( Artifact artifact : artifacts ) |
253 | |
{ |
254 | 61 | if ( !artifact.isOptional() && filter.include( artifact ) && ( "war".equals( artifact.getType() ) ) ) |
255 | |
{ |
256 | 35 | result.add( artifact ); |
257 | |
} |
258 | |
} |
259 | 67 | return result; |
260 | |
} |
261 | |
} |