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 | 0 | protected Boolean excluded = Boolean.FALSE; |
62 | |
|
63 | 0 | protected Boolean unpack = null; |
64 | |
|
65 | |
protected String altDeploymentDescriptor; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public AbstractEarModule() |
72 | 0 | { |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public AbstractEarModule( Artifact a ) |
81 | 0 | { |
82 | 0 | this.artifact = a; |
83 | 0 | this.groupId = a.getGroupId(); |
84 | 0 | this.artifactId = a.getArtifactId(); |
85 | 0 | this.classifier = a.getClassifier(); |
86 | 0 | this.bundleDir = null; |
87 | 0 | } |
88 | |
|
89 | |
public void resolveArtifact( Set artifacts ) |
90 | |
throws EarPluginException, MojoFailureException |
91 | |
{ |
92 | |
|
93 | 0 | if ( artifact == null ) |
94 | |
{ |
95 | |
|
96 | 0 | if ( groupId == null || artifactId == null ) |
97 | |
{ |
98 | 0 | throw new MojoFailureException( |
99 | |
"Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]" ); |
100 | |
} |
101 | 0 | final ArtifactRepository ar = EarExecutionContext.getInstance().getArtifactRepository(); |
102 | 0 | artifact = ar.getUniqueArtifact( groupId, artifactId, getType(), classifier ); |
103 | |
|
104 | 0 | if ( artifact == null ) |
105 | |
{ |
106 | 0 | Set candidates = ar.getArtifacts( groupId, artifactId, getType() ); |
107 | 0 | if ( candidates.size() > 1 ) |
108 | |
{ |
109 | 0 | throw new MojoFailureException( "Artifact[" + this + "] has " + candidates.size() + |
110 | |
" candidates, please provide a classifier." ); |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | 0 | throw new MojoFailureException( "Artifact[" + this + "] " + "is not a dependency of the project." ); |
115 | |
} |
116 | |
} |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | |
public Artifact getArtifact() |
121 | |
{ |
122 | 0 | return artifact; |
123 | |
} |
124 | |
|
125 | |
public String getUri() |
126 | |
{ |
127 | 0 | if ( uri == null ) |
128 | |
{ |
129 | 0 | if ( getBundleDir() == null ) |
130 | |
{ |
131 | 0 | uri = getBundleFileName(); |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | uri = getBundleDir() + getBundleFileName(); |
136 | |
} |
137 | |
} |
138 | 0 | return uri; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public String getGroupId() |
147 | |
{ |
148 | 0 | return groupId; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public String getArtifactId() |
157 | |
{ |
158 | 0 | return artifactId; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public String getClassifier() |
167 | |
{ |
168 | 0 | return classifier; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public String getBundleDir() |
178 | |
{ |
179 | 0 | if ( bundleDir != null ) |
180 | |
{ |
181 | 0 | bundleDir = cleanBundleDir( bundleDir ); |
182 | |
} |
183 | 0 | return bundleDir; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public String getBundleFileName() |
193 | |
{ |
194 | 0 | if ( bundleFileName == null ) |
195 | |
{ |
196 | 0 | bundleFileName = EarExecutionContext.getInstance().getFileNameMapping().mapFileName( artifact ); |
197 | |
} |
198 | 0 | return bundleFileName; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public String getAltDeploymentDescriptor() |
211 | |
{ |
212 | 0 | return altDeploymentDescriptor; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public boolean isExcluded() |
221 | |
{ |
222 | 0 | return excluded.booleanValue(); |
223 | |
} |
224 | |
|
225 | |
public Boolean shouldUnpack() |
226 | |
{ |
227 | 0 | return unpack; |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
protected void writeAltDeploymentDescriptor( XMLWriter writer, String version ) |
237 | |
{ |
238 | 0 | if ( getAltDeploymentDescriptor() != null ) |
239 | |
{ |
240 | 0 | writer.startElement( ALT_DD ); |
241 | 0 | writer.writeText( getAltDeploymentDescriptor() ); |
242 | 0 | writer.endElement(); |
243 | |
} |
244 | 0 | } |
245 | |
|
246 | |
public String toString() |
247 | |
{ |
248 | 0 | StringBuffer sb = new StringBuffer(); |
249 | 0 | sb.append( getType() ).append( ":" ).append( groupId ).append( ":" ).append( artifactId ); |
250 | 0 | if ( classifier != null ) |
251 | |
{ |
252 | 0 | sb.append( ":" ).append( classifier ); |
253 | |
} |
254 | 0 | if ( artifact != null ) |
255 | |
{ |
256 | 0 | sb.append( ":" ).append( artifact.getVersion() ); |
257 | |
} |
258 | 0 | return sb.toString(); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
static String cleanBundleDir( String bundleDir ) |
269 | |
{ |
270 | 0 | if ( bundleDir == null ) |
271 | |
{ |
272 | 0 | return bundleDir; |
273 | |
} |
274 | |
|
275 | |
|
276 | 0 | bundleDir = bundleDir.replace( '\\', '/' ); |
277 | |
|
278 | |
|
279 | 0 | if ( bundleDir.startsWith( "/" ) ) |
280 | |
{ |
281 | 0 | bundleDir = bundleDir.substring( 1, bundleDir.length() ); |
282 | |
} |
283 | |
|
284 | 0 | if ( bundleDir.length() > 0 && !bundleDir.endsWith( "/" ) ) |
285 | |
{ |
286 | |
|
287 | 0 | bundleDir = bundleDir + "/"; |
288 | |
} |
289 | |
|
290 | 0 | return bundleDir; |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
static boolean areNullOrEqual( Object first, Object second ) |
301 | |
{ |
302 | 0 | if ( first != null ) |
303 | |
{ |
304 | 0 | return first.equals( second ); |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | 0 | return second == null; |
309 | |
} |
310 | |
} |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
void setUri(String uri) { |
318 | 0 | this.uri = uri; |
319 | |
|
320 | 0 | } |
321 | |
} |