1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package nl.toolforge.karma.core.cmd.impl; |
20 |
|
|
21 |
|
|
22 |
|
import nl.toolforge.karma.core.cmd.Command; |
23 |
|
import nl.toolforge.karma.core.cmd.CommandDescriptor; |
24 |
|
import nl.toolforge.karma.core.cmd.CommandException; |
25 |
|
import nl.toolforge.karma.core.cmd.CommandFactory; |
26 |
|
import nl.toolforge.karma.core.cmd.CommandLoadException; |
27 |
|
import nl.toolforge.karma.core.cmd.CommandResponse; |
28 |
|
import nl.toolforge.karma.core.cmd.event.ErrorEvent; |
29 |
|
import nl.toolforge.karma.core.cmd.event.ExceptionEvent; |
30 |
|
import nl.toolforge.karma.core.cmd.event.MessageEvent; |
31 |
|
import nl.toolforge.karma.core.cmd.event.SimpleMessage; |
32 |
|
import nl.toolforge.karma.core.cmd.util.DependencyException; |
33 |
|
import nl.toolforge.karma.core.cmd.util.DependencyHelper; |
34 |
|
import nl.toolforge.karma.core.cmd.util.DependencyPath; |
35 |
|
import nl.toolforge.karma.core.cmd.util.DescriptorReader; |
36 |
|
import nl.toolforge.karma.core.manifest.ManifestException; |
37 |
|
import nl.toolforge.karma.core.module.Module; |
38 |
|
import nl.toolforge.karma.core.module.ModuleDigester; |
39 |
|
import nl.toolforge.karma.core.module.ModuleTypeException; |
40 |
|
import org.apache.commons.logging.Log; |
41 |
|
import org.apache.commons.logging.LogFactory; |
42 |
|
import org.apache.tools.ant.BuildException; |
43 |
|
import org.apache.tools.ant.Project; |
44 |
|
import org.apache.tools.ant.Target; |
45 |
|
import org.apache.tools.ant.taskdefs.Copy; |
46 |
|
import org.apache.tools.ant.taskdefs.Ear; |
47 |
|
import org.apache.tools.ant.taskdefs.Jar; |
48 |
|
import org.apache.tools.ant.taskdefs.Mkdir; |
49 |
|
import org.apache.tools.ant.taskdefs.War; |
50 |
|
import org.apache.tools.ant.taskdefs.Zip; |
51 |
|
import org.apache.tools.ant.types.FileSet; |
52 |
|
import org.apache.tools.ant.types.FilterSet; |
53 |
|
import org.xml.sax.SAXException; |
54 |
|
|
55 |
|
import java.io.File; |
56 |
|
import java.io.IOException; |
57 |
|
import java.util.Hashtable; |
58 |
|
import java.util.Iterator; |
59 |
|
import java.util.Map; |
60 |
|
import java.util.Set; |
61 |
|
import java.util.regex.Matcher; |
62 |
|
import java.util.regex.Pattern; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public class PackageModule extends AbstractBuildCommand { |
71 |
|
|
72 |
|
public static final String COMMAND_NAME = "package-module"; |
73 |
|
|
74 |
0 |
private static final Log logger = LogFactory.getLog(PackageModule.class); |
75 |
|
|
76 |
0 |
private CommandResponse commandResponse = new CommandResponse(); |
77 |
|
|
78 |
|
public PackageModule(CommandDescriptor descriptor) { |
79 |
0 |
super(descriptor); |
80 |
0 |
} |
81 |
|
|
82 |
|
public void execute() throws CommandException { |
83 |
|
|
84 |
0 |
super.execute(); |
85 |
|
|
86 |
0 |
DependencyHelper helper = new DependencyHelper(getCurrentManifest()); |
87 |
|
|
88 |
|
try { |
89 |
0 |
boolean dependenciesChecked = false; |
90 |
0 |
while (!dependenciesChecked) { |
91 |
|
try { |
92 |
0 |
helper.getModuleDependencies(getCurrentModule(), false, true); |
93 |
0 |
dependenciesChecked = true; |
94 |
0 |
} catch (DependencyException de) { |
95 |
0 |
if (de.getErrorCode().equals(DependencyException.DEPENDENCY_NOT_FOUND)) { |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
0 |
String dep = (String) de.getMessageArguments()[0]; |
100 |
|
try { |
101 |
0 |
Module module = getCurrentManifest().getModule(dep); |
102 |
0 |
Command command = null; |
103 |
|
try { |
104 |
|
|
105 |
0 |
getCommandResponse().addEvent( |
106 |
|
new MessageEvent(this, class="keyword">new SimpleMessage("Module `{0}` is needed, but is not packaged yet. Doing that now.", new Object[]{module.getName()}))); |
107 |
|
|
108 |
|
String commandLineString; |
109 |
0 |
if (!getCommandLine().hasOption("n")) { |
110 |
0 |
commandLineString = "pam -m " + module.getName(); |
111 |
|
} else { |
112 |
0 |
commandLineString = "pam -n -m " + module.getName(); |
113 |
|
} |
114 |
0 |
logger.debug("Going to: "+commandLineString); |
115 |
0 |
command = CommandFactory.getInstance().getCommand(commandLineString); |
116 |
0 |
command.setContext(getContext()); |
117 |
0 |
command.registerCommandResponseListener(getResponseListener()); |
118 |
0 |
command.execute(); |
119 |
|
|
120 |
|
|
121 |
0 |
} catch (CommandLoadException e) { |
122 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
123 |
|
} finally { |
124 |
0 |
if ( command != null ) { |
125 |
0 |
command.deregisterCommandResponseListener(getResponseListener()); |
126 |
|
} |
127 |
0 |
} |
128 |
0 |
} catch (ManifestException me) { |
129 |
|
|
130 |
0 |
throw de; |
131 |
0 |
} |
132 |
|
} else { |
133 |
|
|
134 |
0 |
throw de; |
135 |
|
} |
136 |
0 |
} |
137 |
|
} |
138 |
0 |
} catch (DependencyException e) { |
139 |
0 |
logger.error(e); |
140 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
141 |
0 |
} catch (ModuleTypeException e) { |
142 |
0 |
logger.error(e); |
143 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
144 |
0 |
} |
145 |
|
|
146 |
|
try { |
147 |
|
|
148 |
0 |
if ( ! getCommandLine().hasOption("n") ) { |
149 |
|
|
150 |
0 |
logger.info("Going to run the unit tests before packaging."); |
151 |
|
|
152 |
0 |
Command command = null; |
153 |
|
try { |
154 |
|
|
155 |
0 |
String commandLineString = "tm -n -m " + module.getName(); |
156 |
|
|
157 |
0 |
command = CommandFactory.getInstance().getCommand(commandLineString); |
158 |
0 |
command.setContext(getContext()); |
159 |
0 |
command.registerCommandResponseListener(getResponseListener()); |
160 |
0 |
command.execute(); |
161 |
|
|
162 |
0 |
} catch (CommandException ce) { |
163 |
0 |
if (ce.getErrorCode().equals(CommandException.TEST_FAILED)) { |
164 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
165 |
0 |
throw new CommandException(ce, CommandException.PACKAGE_FAILED, class="keyword">new Object[]{module.getName()}); |
166 |
0 |
} else if (ce.getErrorCode().equals(CommandException.NO_TEST_DIR)) { |
167 |
|
|
168 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
169 |
|
} else { |
170 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
171 |
|
} |
172 |
0 |
} catch (CommandLoadException e) { |
173 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
174 |
|
} finally { |
175 |
0 |
if ( command != null ) { |
176 |
0 |
command.deregisterCommandResponseListener(getResponseListener()); |
177 |
|
} |
178 |
0 |
} |
179 |
|
|
180 |
|
} else { |
181 |
0 |
logger.info("User has explicitly disabled running the unit tests."); |
182 |
0 |
Command command = null; |
183 |
|
try { |
184 |
|
|
185 |
0 |
String commandLineString = "bm -n -m " + module.getName(); |
186 |
0 |
logger.debug("Going to: "+commandLineString); |
187 |
0 |
command = CommandFactory.getInstance().getCommand(commandLineString); |
188 |
0 |
command.setContext(getContext()); |
189 |
0 |
command.registerCommandResponseListener(getResponseListener()); |
190 |
0 |
command.execute(); |
191 |
0 |
} catch (CommandException ce) { |
192 |
0 |
if (ce.getErrorCode().equals(CommandException.DEPENDENCY_DOES_NOT_EXIST) || |
193 |
|
ce.getErrorCode().equals(CommandException.BUILD_FAILED) || |
194 |
|
ce.getErrorCode().equals(DependencyException.DEPENDENCY_NOT_FOUND) ) { |
195 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
196 |
0 |
throw new CommandException(ce, CommandException.PACKAGE_FAILED, class="keyword">new Object[]{module.getName()}); |
197 |
0 |
} else if (ce.getErrorCode().equals(CommandException.NO_SRC_DIR)) { |
198 |
|
|
199 |
|
} else { |
200 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
201 |
|
} |
202 |
0 |
} catch (CommandLoadException e) { |
203 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
204 |
|
} finally { |
205 |
0 |
if ( command != null ) { |
206 |
0 |
command.deregisterCommandResponseListener(getResponseListener()); |
207 |
|
} |
208 |
0 |
} |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
0 |
String archiveName = helper.resolveArchiveName(getCurrentModule()); |
213 |
0 |
File packageName = new File(getBuildEnvironment().getModuleBuildRootDirectory(), archiveName); |
214 |
|
|
215 |
0 |
if (getCurrentModule().getType().equals(Module.JAVA_WEB_APPLICATION)) { |
216 |
0 |
packageWar(packageName); |
217 |
0 |
} else if (getCurrentModule().getType().equals(Module.JAVA_ENTERPRISE_APPLICATION)) { |
218 |
0 |
packageEar(packageName); |
219 |
0 |
} else if (getCurrentModule().getType().equals(Module.JAVA_SOURCE_MODULE)) { |
220 |
0 |
packageJar(packageName); |
221 |
0 |
} else if (getCurrentModule().getType().equals(Module.OTHER_MODULE)) { |
222 |
0 |
packageOther(packageName); |
223 |
|
} else { |
224 |
0 |
throw new CommandException(CommandException.PACKAGE_FAILED_WRONG_MODULE_TYPE, class="keyword">new Object[]{getCurrentModule()}); |
225 |
|
} |
226 |
|
|
227 |
0 |
SimpleMessage message = |
228 |
|
new SimpleMessage( |
229 |
|
getFrontendMessages().getString("message.MODULE_PACKAGED"), |
230 |
|
new Object[] {getCurrentModule().getName(), archiveName}); |
231 |
0 |
commandResponse.addEvent(new MessageEvent(this, message)); |
232 |
|
|
233 |
0 |
} catch (DependencyException d) { |
234 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
235 |
0 |
} catch (ModuleTypeException d) { |
236 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
237 |
0 |
} |
238 |
0 |
} |
239 |
|
|
240 |
|
private void packageOther(File packageName) throws CommandException { |
241 |
0 |
Project project = getProjectInstance(); |
242 |
|
|
243 |
0 |
Target target = new Target(); |
244 |
0 |
target.setName("run"); |
245 |
0 |
target.setProject(project); |
246 |
|
|
247 |
0 |
project.addTarget(target); |
248 |
|
|
249 |
0 |
executeDelete(getBuildEnvironment().getModuleBuildDirectory(), "*.zip"); |
250 |
|
|
251 |
0 |
Copy copy = null; |
252 |
0 |
FileSet fileSet = null; |
253 |
|
|
254 |
|
|
255 |
0 |
if (getCurrentModule().getBaseDir().exists()) { |
256 |
0 |
copy = (Copy) project.createTask("copy"); |
257 |
0 |
copy.setProject(getProjectInstance()); |
258 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
259 |
0 |
copy.setOverwrite(true); |
260 |
0 |
copy.setIncludeEmptyDirs(false); |
261 |
|
|
262 |
0 |
fileSet = new FileSet(); |
263 |
0 |
fileSet.setDir(getCurrentModule().getBaseDir()); |
264 |
0 |
fileSet.setIncludes("**/*"); |
265 |
0 |
fileSet.setExcludes("history.xml,module-descriptor.xml,.*"); |
266 |
|
|
267 |
0 |
copy.addFileset(fileSet); |
268 |
0 |
target.addTask(copy); |
269 |
|
} else { |
270 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("No resources available."))); |
271 |
|
} |
272 |
0 |
project.executeTarget("run"); |
273 |
|
|
274 |
0 |
if (getBuildEnvironment().getModulePackageDirectory().exists()) { |
275 |
0 |
Target target2 = new Target(); |
276 |
0 |
target2.setName("zip"); |
277 |
0 |
target2.setProject(project); |
278 |
|
|
279 |
0 |
project.addTarget(target2); |
280 |
0 |
Zip zip = (Zip) project.createTask("zip"); |
281 |
0 |
zip.setProject(getProjectInstance()); |
282 |
0 |
zip.setDestFile(packageName); |
283 |
0 |
zip.setBasedir(getBuildEnvironment().getModulePackageDirectory()); |
284 |
0 |
target2.addTask(zip); |
285 |
0 |
project.executeTarget("zip"); |
286 |
|
} else { |
287 |
0 |
throw new CommandException(CommandException.PACKAGE_FAILED_NOTHING_TO_PACKAGE, class="keyword">new Object[]{getCurrentModule()}); |
288 |
|
} |
289 |
0 |
} |
290 |
|
|
291 |
|
private void packageJar(File packageName) throws CommandException { |
292 |
|
|
293 |
|
try { |
294 |
0 |
Project project = getProjectInstance(); |
295 |
|
|
296 |
0 |
Target target = new Target(); |
297 |
0 |
target.setName("run"); |
298 |
0 |
target.setProject(project); |
299 |
|
|
300 |
0 |
project.addTarget(target); |
301 |
|
|
302 |
0 |
executeDelete(getBuildEnvironment().getModuleBuildDirectory(), "*.jar"); |
303 |
|
|
304 |
0 |
Copy copy = null; |
305 |
0 |
FileSet fileSet = null; |
306 |
|
|
307 |
|
|
308 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Copying the resources..."))); |
309 |
0 |
if (new File(getCurrentModule().getBaseDir(), "src/resources").exists()) { |
310 |
0 |
copy = (Copy) project.createTask("copy"); |
311 |
0 |
copy.setProject(getProjectInstance()); |
312 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
313 |
0 |
copy.setOverwrite(true); |
314 |
0 |
copy.setIncludeEmptyDirs(false); |
315 |
|
|
316 |
0 |
fileSet = new FileSet(); |
317 |
0 |
fileSet.setDir(new File(getCurrentModule().getBaseDir(), "src/resources")); |
318 |
0 |
fileSet.setIncludes("**/*"); |
319 |
|
|
320 |
0 |
copy.addFileset(fileSet); |
321 |
0 |
target.addTask(copy); |
322 |
|
} else { |
323 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("No resources available."))); |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Copying the META-INF..."))); |
328 |
0 |
if (new File(getCurrentModule().getBaseDir(), "src/META-INF").exists()) { |
329 |
0 |
copy = (Copy) project.createTask("copy"); |
330 |
0 |
copy.setProject(getProjectInstance()); |
331 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
332 |
0 |
copy.setOverwrite(true); |
333 |
0 |
copy.setIncludeEmptyDirs(false); |
334 |
|
|
335 |
0 |
fileSet = new FileSet(); |
336 |
0 |
fileSet.setDir(new File(getCurrentModule().getBaseDir(), "src")); |
337 |
0 |
fileSet.setIncludes("META-INF/**"); |
338 |
|
|
339 |
0 |
copy.addFileset(fileSet); |
340 |
0 |
target.addTask(copy); |
341 |
|
} else { |
342 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("No META-INF available."))); |
343 |
|
} |
344 |
|
|
345 |
|
|
346 |
0 |
if (getCompileDirectory().exists()) { |
347 |
0 |
copy = (Copy) project.createTask("copy"); |
348 |
0 |
copy.setProject(getProjectInstance()); |
349 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
350 |
0 |
copy.setOverwrite(true); |
351 |
0 |
copy.setIncludeEmptyDirs(false); |
352 |
|
|
353 |
0 |
fileSet = new FileSet(); |
354 |
0 |
fileSet.setDir(getCompileDirectory()); |
355 |
0 |
fileSet.setIncludes("**/*.class"); |
356 |
|
|
357 |
0 |
copy.addFileset(fileSet); |
358 |
0 |
target.addTask(copy); |
359 |
|
} |
360 |
0 |
project.executeTarget("run"); |
361 |
|
|
362 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Packaging..."))); |
363 |
0 |
Target target2 = new Target(); |
364 |
0 |
target2.setName("jar"); |
365 |
0 |
target2.setProject(project); |
366 |
|
|
367 |
0 |
project.addTarget(target2); |
368 |
0 |
if (getBuildEnvironment().getModulePackageDirectory().exists()) { |
369 |
0 |
Jar jar = (Jar) project.createTask("jar"); |
370 |
0 |
jar.setProject(getProjectInstance()); |
371 |
0 |
jar.setDestFile(packageName); |
372 |
0 |
jar.setBasedir(getBuildEnvironment().getModulePackageDirectory()); |
373 |
0 |
jar.setExcludes("*.jar"); |
374 |
0 |
target2.addTask(jar); |
375 |
|
|
376 |
0 |
project.executeTarget("jar"); |
377 |
|
} else { |
378 |
0 |
throw new CommandException(CommandException.PACKAGE_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
379 |
|
} |
380 |
0 |
} catch (BuildException e) { |
381 |
0 |
e.printStackTrace(); |
382 |
0 |
if (logger.isDebugEnabled()) { |
383 |
0 |
commandResponse.addEvent(new ExceptionEvent(this, e)); |
384 |
|
} |
385 |
0 |
throw new CommandException(e, CommandException.PACKAGE_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
386 |
0 |
} catch (ModuleTypeException e) { |
387 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
388 |
0 |
} |
389 |
|
|
390 |
0 |
} |
391 |
|
|
392 |
|
private void packageWar(File packageName) throws CommandException { |
393 |
|
|
394 |
0 |
Project project = getProjectInstance(); |
395 |
|
|
396 |
0 |
Target target = new Target(); |
397 |
0 |
target.setName("run"); |
398 |
0 |
target.setProject(project); |
399 |
|
|
400 |
0 |
project.addTarget(target); |
401 |
|
|
402 |
0 |
DependencyHelper helper = new DependencyHelper(getCurrentManifest()); |
403 |
|
|
404 |
|
try { |
405 |
0 |
executeDelete(getBuildEnvironment().getModuleBuildDirectory(), "*.war"); |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
Copy copy; |
410 |
0 |
File webdir = new File(class="keyword">new File(getCurrentModule().getBaseDir(), "src"), "web"); |
411 |
|
FileSet fileSet; |
412 |
|
|
413 |
|
|
414 |
0 |
Mkdir mkdir = (Mkdir) project.createTask("mkdir"); |
415 |
0 |
mkdir.setDir(getBuildEnvironment().getModulePackageDirectory()); |
416 |
0 |
target.addTask(mkdir); |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
0 |
if (webdir.exists()) { |
421 |
0 |
copy = (Copy) project.createTask("copy"); |
422 |
0 |
copy.setProject(getProjectInstance()); |
423 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
424 |
0 |
copy.setOverwrite(true); |
425 |
0 |
copy.setIncludeEmptyDirs(false); |
426 |
|
|
427 |
0 |
fileSet = new FileSet(); |
428 |
0 |
fileSet.setDir(webdir); |
429 |
0 |
fileSet.setIncludes("**"); |
430 |
0 |
fileSet.setExcludes("WEB-INF/web.xml"); |
431 |
|
|
432 |
0 |
copy.addFileset(fileSet); |
433 |
0 |
target.addTask(copy); |
434 |
|
} |
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
0 |
Set deps = helper.getAllDependencies(getCurrentModule(),false, true); |
440 |
0 |
if (!deps.isEmpty()) { |
441 |
|
|
442 |
0 |
copy = (Copy) project.createTask("copy"); |
443 |
0 |
copy.setProject(getProjectInstance()); |
444 |
0 |
copy.setTodir(new File(getBuildEnvironment().getModulePackageDirectory(), "WEB-INF/lib")); |
445 |
0 |
copy.setFlatten(true); |
446 |
0 |
copy.setIncludeEmptyDirs(false); |
447 |
|
|
448 |
|
|
449 |
|
|
450 |
0 |
Iterator it = deps.iterator(); |
451 |
0 |
while (it.hasNext()) { |
452 |
0 |
DependencyPath path = (DependencyPath) it.next(); |
453 |
0 |
fileSet = new FileSet(); |
454 |
0 |
fileSet.setFile(path.getFullPath()); |
455 |
0 |
copy.addFileset(fileSet); |
456 |
|
} |
457 |
|
|
458 |
0 |
target.addTask(copy); |
459 |
|
} |
460 |
|
|
461 |
|
|
462 |
|
|
463 |
0 |
War war = (War) project.createTask("war"); |
464 |
0 |
war.setProject(getProjectInstance()); |
465 |
0 |
war.setDestFile(packageName); |
466 |
0 |
war.setBasedir(getBuildEnvironment().getModulePackageDirectory()); |
467 |
0 |
war.setWebxml(new File(getCurrentModule().getBaseDir(), "src/web/WEB-INF/web.xml".replace('/', File.separatorChar))); |
468 |
|
|
469 |
0 |
target.addTask(war); |
470 |
0 |
project.executeTarget("run"); |
471 |
|
|
472 |
0 |
} catch (BuildException e) { |
473 |
0 |
e.printStackTrace(); |
474 |
0 |
if (logger.isDebugEnabled()) { |
475 |
0 |
commandResponse.addEvent(new ExceptionEvent(this, e)); |
476 |
|
} |
477 |
0 |
throw new CommandException(e, CommandException.PACKAGE_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
478 |
0 |
} catch (DependencyException d) { |
479 |
0 |
d.printStackTrace(); |
480 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
481 |
0 |
} catch (ModuleTypeException d) { |
482 |
0 |
d.printStackTrace(); |
483 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
484 |
0 |
} |
485 |
|
|
486 |
0 |
} |
487 |
|
|
488 |
|
private void packageEar(File packageName) throws CommandException { |
489 |
|
|
490 |
0 |
Project project = getProjectInstance(); |
491 |
|
|
492 |
0 |
Target target = new Target(); |
493 |
0 |
target.setName("run"); |
494 |
0 |
target.setProject(project); |
495 |
|
|
496 |
0 |
project.addTarget(target); |
497 |
|
|
498 |
0 |
DependencyHelper helper = new DependencyHelper(getCurrentManifest()); |
499 |
|
|
500 |
|
try { |
501 |
|
|
502 |
|
|
503 |
|
|
504 |
|
|
505 |
0 |
DescriptorReader reader = new DescriptorReader(DescriptorReader.APPLICATION_XML); |
506 |
|
try { |
507 |
0 |
reader.parse(new File(getCurrentModule().getBaseDir(), "src/META-INF")); |
508 |
0 |
} catch (IOException e) { |
509 |
0 |
throw new CommandException(CommandException.PACKAGE_FAILED_NO_APPLICATION_XML, class="keyword">new Object[]{module.getName()}); |
510 |
0 |
} catch (SAXException e) { |
511 |
0 |
throw new CommandException(CommandException.PACKAGE_FAILED_INVALID_APPLICATION_XML, class="keyword">new Object[]{module.getName()}); |
512 |
0 |
} |
513 |
|
|
514 |
|
|
515 |
|
|
516 |
|
|
517 |
0 |
Map map = new Hashtable(); |
518 |
0 |
for (Iterator it = reader.getModuleNames().iterator(); it.hasNext(); ) { |
519 |
0 |
String moduleName = ((StringBuffer) it.next()).toString(); |
520 |
0 |
Pattern p = Pattern.compile("@("+ModuleDigester.NAME_PATTERN_STRING+")@"); |
521 |
0 |
Matcher m = p.matcher(moduleName); |
522 |
|
|
523 |
0 |
if (m.matches()) { |
524 |
0 |
moduleName = m.group(1); |
525 |
|
Module mod; |
526 |
|
try { |
527 |
0 |
mod = getCurrentManifest().getModule(moduleName); |
528 |
0 |
} catch (ManifestException me) { |
529 |
0 |
throw new DependencyException(DependencyException.EAR_DEPENDENCY_NOT_FOUND, class="keyword">new Object[]{moduleName}); |
530 |
0 |
} |
531 |
|
|
532 |
0 |
map.put(moduleName, helper.resolveArchiveName(mod)); |
533 |
|
|
534 |
0 |
if (!helper.hasModuleDependency(getCurrentModule(), mod, true)) { |
535 |
0 |
throw new DependencyException(DependencyException.EAR_DEPENDENCY_NOT_DEFINED, class="keyword">new Object[]{moduleName}); |
536 |
|
} |
537 |
0 |
} else { |
538 |
|
|
539 |
|
} |
540 |
|
} |
541 |
|
|
542 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Deleting previous ear file."))); |
543 |
0 |
executeDelete(getBuildEnvironment().getModuleBuildDirectory(), "*.ear"); |
544 |
|
|
545 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Copying META-INF dir."))); |
546 |
0 |
Copy copyMetaInf = (Copy) project.createTask("copy"); |
547 |
0 |
copyMetaInf.setProject(getProjectInstance()); |
548 |
0 |
copyMetaInf.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
549 |
0 |
copyMetaInf.setOverwrite(true); |
550 |
0 |
copyMetaInf.setIncludeEmptyDirs(false); |
551 |
|
|
552 |
0 |
FileSet fileSet = new FileSet(); |
553 |
0 |
fileSet.setDir(new File(getCurrentModule().getBaseDir(), "src")); |
554 |
0 |
fileSet.setIncludes("META-INF/**"); |
555 |
|
|
556 |
|
|
557 |
|
|
558 |
0 |
helper.createModuleDependenciesFilter(module); |
559 |
0 |
FilterSet filterSet = copyMetaInf.createFilterSet(); |
560 |
0 |
filterSet.setFiltersfile(new File(getBuildEnvironment().getModuleBuildDirectory(), DependencyHelper.MODULE_DEPENDENCIES_PROPERTIES)); |
561 |
|
|
562 |
0 |
copyMetaInf.addFileset(fileSet); |
563 |
0 |
target.addTask(copyMetaInf); |
564 |
|
|
565 |
|
|
566 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Copying module dependencies"))); |
567 |
0 |
Set moduleDeps = helper.getModuleDependencies(getCurrentModule(), false, true); |
568 |
0 |
if (!moduleDeps.isEmpty()) { |
569 |
0 |
Copy copy = (Copy) project.createTask("copy"); |
570 |
0 |
copy.setProject(getProjectInstance()); |
571 |
0 |
copy.setTodir(getBuildEnvironment().getModulePackageDirectory()); |
572 |
0 |
copy.setFlatten(true); |
573 |
0 |
copy.setIncludeEmptyDirs(false); |
574 |
|
|
575 |
0 |
Iterator it = moduleDeps.iterator(); |
576 |
0 |
while (it.hasNext()) { |
577 |
0 |
DependencyPath path = (DependencyPath) it.next(); |
578 |
0 |
fileSet = new FileSet(); |
579 |
0 |
fileSet.setFile(path.getFullPath()); |
580 |
0 |
copy.addFileset(fileSet); |
581 |
|
} |
582 |
0 |
target.addTask(copy); |
583 |
|
} else { |
584 |
0 |
logger.info("No module dependencies to package."); |
585 |
|
} |
586 |
|
|
587 |
|
|
588 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Copying jar dependencies"))); |
589 |
0 |
Set jarDeps = helper.getJarDependencies(getCurrentModule(), true); |
590 |
0 |
if (!jarDeps.isEmpty()) { |
591 |
0 |
Copy copy = (Copy) project.createTask("copy"); |
592 |
0 |
copy.setProject(getProjectInstance()); |
593 |
0 |
copy.setTodir(new File(getBuildEnvironment().getModulePackageDirectory(), "lib")); |
594 |
0 |
copy.setFlatten(true); |
595 |
0 |
copy.setIncludeEmptyDirs(false); |
596 |
|
|
597 |
0 |
Iterator it = jarDeps.iterator(); |
598 |
0 |
while (it.hasNext()) { |
599 |
0 |
DependencyPath path = (DependencyPath) it.next(); |
600 |
0 |
fileSet = new FileSet(); |
601 |
0 |
fileSet.setFile(path.getFullPath()); |
602 |
0 |
copy.addFileset(fileSet); |
603 |
|
} |
604 |
0 |
target.addTask(copy); |
605 |
|
} else { |
606 |
0 |
logger.info("No jar dependencies to package."); |
607 |
|
} |
608 |
0 |
project.executeTarget("run"); |
609 |
|
|
610 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Creating ear"))); |
611 |
0 |
Target target2 = new Target(); |
612 |
0 |
target2.setName("ear"); |
613 |
0 |
target2.setProject(project); |
614 |
|
|
615 |
0 |
project.addTarget(target2); |
616 |
|
|
617 |
0 |
Ear ear = (Ear) project.createTask("ear"); |
618 |
0 |
ear.setProject(getProjectInstance()); |
619 |
0 |
ear.setDestFile(packageName); |
620 |
0 |
ear.setBasedir(getBuildEnvironment().getModulePackageDirectory()); |
621 |
0 |
ear.setExcludes("META-INF/application.xml"); |
622 |
0 |
ear.setAppxml(new File(getBuildEnvironment().getModulePackageDirectory(), "META-INF/application.xml".replace('/', File.separatorChar))); |
623 |
|
|
624 |
0 |
target2.addTask(ear); |
625 |
0 |
project.executeTarget("ear"); |
626 |
0 |
} catch (BuildException e) { |
627 |
0 |
e.printStackTrace(); |
628 |
0 |
if (logger.isDebugEnabled()) { |
629 |
0 |
commandResponse.addEvent(new ExceptionEvent(this, e)); |
630 |
|
} |
631 |
0 |
throw new CommandException(e, CommandException.PACKAGE_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
632 |
0 |
} catch (DependencyException d) { |
633 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
634 |
0 |
} catch (ModuleTypeException d) { |
635 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
636 |
0 |
} |
637 |
|
|
638 |
0 |
} |
639 |
|
|
640 |
|
|
641 |
|
|
642 |
|
|
643 |
|
|
644 |
|
|
645 |
|
public CommandResponse getCommandResponse() { |
646 |
0 |
return commandResponse; |
647 |
|
} |
648 |
|
|
649 |
|
} |