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