1 | |
package org.apache.maven.plugin.ear; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.plugin.MojoFailureException; |
24 | |
import org.apache.maven.plugin.ear.util.ArtifactRepository; |
25 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
26 | |
|
27 | |
import java.util.Set; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public abstract class AbstractEarModule |
36 | |
implements EarModule |
37 | |
{ |
38 | |
|
39 | |
protected static final String MODULE_ELEMENT = "module"; |
40 | |
|
41 | |
protected static final String JAVA_MODULE = "java"; |
42 | |
|
43 | |
protected static final String ALT_DD = "alt-dd"; |
44 | |
|
45 | |
private String uri; |
46 | |
|
47 | |
private Artifact artifact; |
48 | |
|
49 | |
|
50 | |
|
51 | |
private String groupId; |
52 | |
|
53 | |
private String artifactId; |
54 | |
|
55 | |
private String classifier; |
56 | |
|
57 | |
protected String bundleDir; |
58 | |
|
59 | |
protected String bundleFileName; |
60 | |
|
61 | 4 | protected Boolean excluded = Boolean.FALSE; |
62 | |
|
63 | 4 | protected Boolean unpack = null; |
64 | |
|
65 | |
protected String altDeploymentDescriptor; |
66 | |
|
67 | |
protected EarExecutionContext earExecutionContext; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public AbstractEarModule() |
74 | 0 | { |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public AbstractEarModule( Artifact a ) |
83 | 4 | { |
84 | 4 | this.artifact = a; |
85 | 4 | this.groupId = a.getGroupId(); |
86 | 4 | this.artifactId = a.getArtifactId(); |
87 | 4 | this.classifier = a.getClassifier(); |
88 | 4 | this.bundleDir = null; |
89 | 4 | } |
90 | |
|
91 | |
public void setEarExecutionContext( EarExecutionContext earExecutionContext ) |
92 | |
{ |
93 | 0 | this.earExecutionContext = earExecutionContext; |
94 | 0 | } |
95 | |
|
96 | |
public void resolveArtifact( Set artifacts ) |
97 | |
throws EarPluginException, MojoFailureException |
98 | |
{ |
99 | |
|
100 | 0 | if ( artifact == null ) |
101 | |
{ |
102 | |
|
103 | 0 | if ( groupId == null || artifactId == null ) |
104 | |
{ |
105 | 0 | throw new MojoFailureException( |
106 | |
"Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" ); |
107 | |
} |
108 | 0 | final ArtifactRepository ar = earExecutionContext.getArtifactRepository(); |
109 | 0 | artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier ); |
110 | |
|
111 | 0 | if ( artifact == null ) |
112 | |
{ |
113 | 0 | Set candidates = ar.getArtifacts( groupId, artifactId, getType() ); |
114 | 0 | if ( candidates.size() > 1 ) |
115 | |
{ |
116 | 0 | throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() + |
117 | |
" candidates, please provide a classifier." ); |
118 | |
} |
119 | |
else |
120 | |
{ |
121 | 0 | throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." ); |
122 | |
} |
123 | |
} |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | |
public Artifact getArtifact() |
128 | |
{ |
129 | 0 | return artifact; |
130 | |
} |
131 | |
|
132 | |
public String getUri() |
133 | |
{ |
134 | 4 | if ( uri == null ) |
135 | |
{ |
136 | 0 | if ( getBundleDir() == null ) |
137 | |
{ |
138 | 0 | uri = getBundleFileName(); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | uri = getBundleDir() + getBundleFileName(); |
143 | |
} |
144 | |
} |
145 | 4 | return uri; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public String getGroupId() |
154 | |
{ |
155 | 0 | return groupId; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public String getArtifactId() |
164 | |
{ |
165 | 0 | return artifactId; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public String getClassifier() |
174 | |
{ |
175 | 0 | return classifier; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public String getBundleDir() |
185 | |
{ |
186 | 0 | if ( bundleDir != null ) |
187 | |
{ |
188 | 0 | bundleDir = cleanBundleDir( bundleDir ); |
189 | |
} |
190 | 0 | return bundleDir; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public String getBundleFileName() |
200 | |
{ |
201 | 0 | if ( bundleFileName == null ) |
202 | |
{ |
203 | 0 | bundleFileName = earExecutionContext.getFileNameMapping().mapFileName( artifact ); |
204 | |
} |
205 | 0 | return bundleFileName; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public String getAltDeploymentDescriptor() |
218 | |
{ |
219 | 0 | return altDeploymentDescriptor; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public boolean isExcluded() |
228 | |
{ |
229 | 4 | return excluded.booleanValue(); |
230 | |
} |
231 | |
|
232 | |
public Boolean shouldUnpack() |
233 | |
{ |
234 | 0 | return unpack; |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
protected void writeAltDeploymentDescriptor( XMLWriter writer, String version ) |
244 | |
{ |
245 | 0 | if ( getAltDeploymentDescriptor() != null ) |
246 | |
{ |
247 | 0 | writer.startElement( ALT_DD ); |
248 | 0 | writer.writeText( getAltDeploymentDescriptor() ); |
249 | 0 | writer.endElement(); |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | |
public String toString() |
254 | |
{ |
255 | 0 | StringBuffer sb = new StringBuffer(); |
256 | 0 | sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId ); |
257 | 0 | if ( classifier != null ) |
258 | |
{ |
259 | 0 | sb.append( ":" ).append( classifier ); |
260 | |
} |
261 | 0 | if ( artifact != null ) |
262 | |
{ |
263 | 0 | sb.append( ":" ).append( artifact.getVersion() ); |
264 | |
} |
265 | 0 | return sb.toString(); |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
static String cleanBundleDir( String bundleDir ) |
276 | |
{ |
277 | 5 | if ( bundleDir == null ) |
278 | |
{ |
279 | 0 | return bundleDir; |
280 | |
} |
281 | |
|
282 | |
|
283 | 5 | bundleDir = bundleDir.replace( '\\', '/' ); |
284 | |
|
285 | |
|
286 | 5 | if ( bundleDir.startsWith( "/" ) ) |
287 | |
{ |
288 | 3 | bundleDir = bundleDir.substring( 1, bundleDir.length() ); |
289 | |
} |
290 | |
|
291 | 5 | if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) ) |
292 | |
{ |
293 | |
|
294 | 2 | bundleDir = bundleDir + "/"; |
295 | |
} |
296 | |
|
297 | 5 | return bundleDir; |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
static boolean areNullOrEqual( Object first, Object second ) |
308 | |
{ |
309 | 0 | if ( first != null ) |
310 | |
{ |
311 | 0 | return first.equals( second ); |
312 | |
} |
313 | |
else |
314 | |
{ |
315 | 0 | return second == null; |
316 | |
} |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
void setUri(String uri) { |
325 | 4 | this.uri = uri; |
326 | |
|
327 | 4 | } |
328 | |
} |