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