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