1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.internal.tools.ant.taskdefs;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.ZipFileSet;
import org.aspectj.internal.tools.build.BuildSpec;
import org.aspectj.internal.tools.build.Builder;
import org.aspectj.internal.tools.build.Messager;
import org.aspectj.internal.tools.build.Module;
import org.aspectj.internal.tools.build.Modules;
import org.aspectj.internal.tools.build.ProductModule;
import org.aspectj.internal.tools.build.Util;
/**
* Implement Builder in Ant.
*/
public class AntBuilder extends Builder {
/*
* XXX This just constructs and uses Ant Task objects,
* which in some cases causes the tasks to fail.
*/
/**
* Factory for a Builder.
* @param config the String configuration, where only substrings
* "verbose" and "useEclipseCompiles" are significant
* @param project the owning Project for all tasks (not null)
* @param tempDir the File path to a temporary dir for side effects (may be null)
* @return a Builder for this project and configuration
*/
public static Builder getBuilder(String config, Project project, File tempDir) {
boolean useEclipseCompiles = false;
boolean verbose = false;
if (null != config) {
if (-1 != config.indexOf("useEclipseCompiles")) {
useEclipseCompiles = true;
}
if (-1 != config.indexOf("verbose")) {
verbose = true;
}
}
Messager handler = new ProjectMessager(project);
Builder result = new ProductBuilder(project, tempDir, useEclipseCompiles, handler);
if (verbose) {
result.setVerbose(true);
}
return result;
}
/**
* Make and register target for this module and antecedants.
* This ensures that the (direct) depends list is generated
* for each target.
* This depends on topoSort to detect cycles. XXX unverified
*/
private static void makeTargetsForModule(
final Module module,
final Hashtable targets,
final boolean rebuild) {
Target target = (Target) targets.get(module.name);
if (null == target) {
// first add the target
target = new Target();
target.setName(module.name);
List req = module.getRequired();
StringBuffer depends = new StringBuffer();
boolean first = true;
for (Iterator iterator = req.iterator(); iterator.hasNext();) {
Module reqModule = (Module) iterator.next();
if (rebuild || reqModule.outOfDate(false)) {
if (!first) {
depends.append(",");
} else {
first = false;
}
depends.append(reqModule.name);
}
}
if (0 < depends.length()) {
target.setDepends(depends.toString());
}
targets.put(module.name, target);
// then recursively add any required modules
for (Iterator iterator = module.getRequired().iterator();
iterator.hasNext();
) {
Module reqModule = (Module) iterator.next();
if (rebuild || reqModule.outOfDate(false)) {
makeTargetsForModule(reqModule, targets, rebuild);
}
}
}
}
private final Project project; // XXX s.b. used only in setupTask
protected AntBuilder(Project project, File tempDir, boolean useEclipseCompiles,
Messager handler) {
super(tempDir, useEclipseCompiles, handler);
this.project = project;
Util.iaxIfNull(project, "project");
}
/**
* Initialize task with project and "ajbuild-" + name as name. (Using bm-
* prefix distinguishes these tasks from tasks found in the build script.)
* @param task the Task to initialize - not null
* @param name the String name suffix for the task
* @return true unless some error
*/
protected boolean setupTask(Task task, String name) {
task.setProject(project);
task.setTaskName("ajbuild-" + name);
return true;
}
/**
* Copy file, optionally filtering.
* (Filters set in project.)
* @param fromFile the readable File source to copy
* @param toFile the writable File destination file
* @param boolean filter if true, enable filtering
* @see org.aspectj.internal.tools.build.Builder#copyFile(File, File, boolean)
*/
protected boolean copyFile(File fromFile, File toFile, boolean filter) {
Copy copy = makeCopyTask(filter);
copy.setFile(fromFile);
copy.setTofile(toFile);
executeTask(copy);
return true;
}
/**
* (Filters set in project.)
* @see org.aspectj.internal.tools.ant.taskdefs.Builder#copyFiles(File, File, String, String, boolean)
*/
protected boolean copyFiles(
File fromDir,
File toDir,
String includes,
String excludes,
boolean filter) {
Copy copy = makeCopyTask(filter);
copy.setTodir(toDir);
FileSet fileset = new FileSet();
fileset.setDir(fromDir);
if (null != includes) {
fileset.setIncludes(includes);
}
if (null != excludes) {
fileset.setExcludes(excludes);
}
copy.addFileset(fileset);
executeTask(copy);
return false;
}
protected void copyFileset(File toDir, FileSet fileSet, boolean filter) {
Copy copy = makeCopyTask(filter);
copy.addFileset(fileSet);
copy.setTodir(toDir);
executeTask(copy);
}
/**
* @param filter if FILTER_ON, use filters
*/
protected Copy makeCopyTask(boolean filter) {
Copy copy = new Copy();
setupTask(copy, "copy");
if (FILTER_ON == filter) {
copy.setFiltering(true);
}
return copy;
}
protected boolean compile(
Module module,
File classesDir,
boolean useExistingClasses,
List errors) {
// -- source paths
Path path = new Path(project);
boolean hasSourceDirectories = false;
for (Iterator iter = module.getSrcDirs().iterator(); iter.hasNext();) {
File file = (File) iter.next();
path.createPathElement().setLocation(file);
if (!hasSourceDirectories) {
hasSourceDirectories = true;
}
}
if (!classesDir.exists() && !classesDir.mkdirs()) {
errors.add("compile - unable to create " + classesDir);
return false;
}
if (!hasSourceDirectories) { // none - dump minimal file and exit
File minFile = new File(classesDir, module.name);
FileWriter fw = null;
try {
fw = new FileWriter(minFile);
fw.write(module.name);
} catch (IOException e) {
errors.add("IOException writing "
+ module.name
+ " to "
+ minFile
+ ": "
+ Util.renderException(e));
} finally {
Util.close(fw);
}
return true; // nothing to compile - ok
}
if (useExistingClasses) {
return true;
}
// XXX test whether build.compiler property takes effect automatically
// I suspect it requires the proper adapter setup.
Javac javac = new Javac();
setupTask(javac, "javac");
javac.setDestdir(classesDir);
javac.setSrcdir(path);
path = null;
// -- classpath
Path classpath = new Path(project);
boolean hasLibraries = setupClasspath(module, classpath);
// need to add system classes??
boolean inEclipse = true; // XXX detect, fork only in eclipse
if (hasLibraries && inEclipse) {
javac.setFork(true); // XXX otherwise never releases library jars
}
// -- set output directory
classpath.createPathElement().setLocation(classesDir);
javac.setClasspath(classpath);
// misc
javac.setDebug(true);
javac.setTarget("1.1"); // 1.1 class files - Javac in 1.4 uses 1.4
// compile
try {
return executeTask(AspectJSupport.wrapIfNeeded(module, javac));
} catch (BuildException e) {
String args = "" + Arrays.asList(javac.getCurrentCompilerArgs());
errors.add("BuildException compiling " + module.toLongString() + args
+ ": " + Util.renderException(e));
return false;
} finally {
javac.init(); // be nice to let go of classpath libraries...
}
}
public boolean setupClasspath(Module module, Path classpath) { // XXX fix test access
boolean hasLibraries = false;
// required libraries
for (Iterator iter = module.getLibJars().iterator(); iter.hasNext();) {
File file = (File) iter.next();
classpath.createPathElement().setLocation(file);
if (!hasLibraries) {
hasLibraries = true;
}
}
// required modules and their exported libraries
for (Iterator iter = module.getRequired().iterator(); iter.hasNext();) {
Module required = (Module) iter.next();
classpath.createPathElement().setLocation(required.getModuleJar());
if (!hasLibraries) {
hasLibraries = true;
}
// also put on classpath libraries exported from required module
// XXX exported modules not supported
for (Iterator iterator = required.getExportedLibJars().iterator();
iterator.hasNext();
) {
classpath.createPathElement().setLocation((File) iterator.next());
}
}
return hasLibraries;
}
/**
* Merge classes directory and any merge jars into module jar
* with any specified manifest file.
* META-INF directories are excluded.
*/
protected boolean assemble(Module module, File classesDir, List errors) {
if (!buildingEnabled) {
return false;
}
// ---- zip result up
Zip zip = new Zip();
setupTask(zip, "zip");
zip.setDestFile(module.getModuleJar());
ZipFileSet zipfileset = null;
// -- merge any resources in any of the src directories
for (Iterator iter = module.getSrcDirs().iterator(); iter.hasNext();) {
File srcDir = (File) iter.next();
zipfileset = new ZipFileSet();
zipfileset.setProject(project);
zipfileset.setDir(srcDir);
zipfileset.setIncludes(RESOURCE_PATTERN);
zip.addZipfileset(zipfileset);
}
// -- merge any merge jars
List mergeJars = module.getMerges();
// final boolean useManifest = false;
if (0 < mergeJars.size()) {
for (Iterator iter = mergeJars.iterator(); iter.hasNext();) {
File mergeJar = (File) iter.next();
zipfileset = new ZipFileSet();
zipfileset.setProject(project);
zipfileset.setSrc(mergeJar);
zipfileset.setIncludes("**/*");
zipfileset.setExcludes("META-INF/manifest.mf"); // XXXFileLiteral
zipfileset.setExcludes("meta-inf/manifest.MF");
zipfileset.setExcludes("META-INF/MANIFEST.mf");
zipfileset.setExcludes("meta-inf/MANIFEST.MF");
zip.addZipfileset(zipfileset);
}
}
// merge classes; put any meta-inf/manifest.mf here
File metaInfDir = new File(classesDir, "META-INF");
Util.deleteContents(metaInfDir);
// -- manifest
File manifest = new File(module.moduleDir, module.name + ".mf.txt"); // XXXFileLiteral
if (Util.canReadFile(manifest)) {
if (Util.canReadDir(metaInfDir) || metaInfDir.mkdirs()) {
// Jar spec requires a MANIFEST.MF not a manifest.mf
copyFile(manifest, new File(metaInfDir, "MANIFEST.MF"), FILTER_ON); // XXXFileLiteral
} else {
errors.add("have manifest, but unable to create " + metaInfDir);
return false;
}
}
zipfileset = new ZipFileSet();
zipfileset.setProject(project);
zipfileset.setDir(classesDir);
zipfileset.setIncludes("**/*");
zip.addZipfileset(zipfileset);
try {
handler.log("assembling " + module + " in " + module.getModuleJar());
return executeTask(zip)
// zip returns true when it doesn't create zipfile
// because there are no entries to add, so verify done
&& Util.canReadFile(module.getModuleJar());
} catch (BuildException e) {
errors.add("BuildException zipping " + module + ": " + e.getMessage());
return false;
} finally {
module.clearOutOfDate();
}
}
/**
* @see org.aspectj.internal.tools.build.Builder#buildAntecedants(Module)
*/
protected String[] getAntecedantModuleNames(Module module, boolean rebuild) {
Hashtable targets = new Hashtable();
makeTargetsForModule(module, targets, rebuild);
// XXX bug: doc says topoSort returns String, but returns Target
Collection result = project.topoSort(module.name, targets);
// XXX is it topoSort that should detect cycles?
int size = result.size();
if (0 == result.size()) {
return new String[0];
}
ArrayList toReturn = new ArrayList();
for (Iterator iter = result.iterator(); iter.hasNext();) {
Target target = (Target) iter.next();
String name = target.getName();
if (null == name) {
throw new Error("null name?");
} else {
toReturn.add(name);
}
}
// topoSort always returns module.name
if ((1 == size)
&& module.name.equals(toReturn.get(0))
&& !module.outOfDate(false)) {
return new String[0];
}
return (String[]) toReturn.toArray(new String[0]);
}
/**
* Generate Module.assembledJar with merge of itself and all antecedants
*/
protected boolean assembleAll(Module module, Messager handler) {
if (!buildingEnabled) {
return false;
}
Util.iaxIfNull(module, "module");
Util.iaxIfNull(handler, "handler");
if (module.outOfDate(false)) {
throw new IllegalStateException("module out of date: " + module);
}
// ---- zip result up
Zip zip = new Zip();
setupTask(zip, "zip");
zip.setDestFile(module.getAssembledJar());
ZipFileSet zipfileset = null;
ArrayList known = module.findKnownJarAntecedants();
// -- merge any antecedents, less any manifest
for (Iterator iter = known.iterator(); iter.hasNext();) {
File jarFile = (File) iter.next();
zipfileset = new ZipFileSet();
zipfileset.setProject(project);
zipfileset.setSrc(jarFile);
zipfileset.setIncludes("**/*");
zipfileset.setExcludes("META-INF/MANIFEST.MF"); // XXXFileLiteral
zipfileset.setExcludes("META-INF/manifest.mf");
zipfileset.setExcludes("meta-inf/manifest.mf");
zipfileset.setExcludes("meta-inf/MANIFEST.MF");
zip.addZipfileset(zipfileset);
}
// merge the module jar itself, including same manifest (?)
zipfileset = new ZipFileSet();
zipfileset.setProject(project);
zipfileset.setSrc(module.getModuleJar());
zip.addZipfileset(zipfileset);
try {
handler.log("assembling all " + module + " in " + module.getAssembledJar());
if (verbose) {
handler.log("knownAntecedants: " + known);
}
return executeTask(zip);
} catch (BuildException e) {
handler.logException("BuildException zipping " + module, e);
return false;
} finally {
module.clearOutOfDate();
}
}
/**
* @see org.aspectj.internal.tools.ant.taskdefs.Builder#buildInstaller(BuildSpec, String)
*/
protected boolean buildInstaller(
BuildSpec buildSpec,
String targDirPath) {
return false;
}
/** task.execute() and any advice */
protected boolean executeTask(Task task) {
if (!buildingEnabled) {
return false;
}
task.execute();
return true;
}
/**
* Support for compiling basic AspectJ projects.
* Projects may only compile all (and only) their source directories;
* aspectpath, inpath, etc. are not supported.
* To load the compiler, this assumes the user has either defined
* a project property "aspectj.home" or that there exists
* <code>{module-dir}/lib/aspectj/lib/aspectj[tools|rt].jar</code>.
*/
static class AspectJSupport {
static final String AJCTASK = "org.aspectj.tools.ant.taskdefs.AjcTask";
static final String ASPECTJRT_JAR_VARIABLE = "ASPECTJRT_LIB";
static final String ASPECTJLIB_RPATH = "/lib/aspectj/lib";
static final Map nameToAspectjrtjar = new HashMap();
static final String NONE = "NONE";
/**
* If this module should be compiled with AspectJ,
* return a task to do so.
* @param module the Module to compile
* @param javac the Javac compile commands
* @return javac or a Task to compile with AspectJ if needed
*/
static Task wrapIfNeeded(Module module, Javac javac) {
final Project project = javac.getProject();
Path runtimeJar = null;
if (runtimeJarOnClasspath(module)) {
// yes aspectjrt.jar on classpath
} else if (module.getClasspathVariables().contains(ASPECTJRT_JAR_VARIABLE)) {
// yes, in variables - find aspectjrt.jar to add to classpath
runtimeJar = getAspectJLib(project, module, "aspectjrt.jar");
} else {
// no
return javac;
}
Path aspectjtoolsJar = getAspectJLib(project, module, "aspectjtools.jar");
return aspectJTask(javac, aspectjtoolsJar, runtimeJar);
}
/** @return true if aspectjrt.jar is on classpath */
private static boolean runtimeJarOnClasspath(Module module) {
for (Iterator iter = module.getLibJars().iterator(); iter.hasNext();) {
File file = (File) iter.next();
if ("aspectjrt.jar".equals(file.getName())) {
return true;
}
}
return false;
}
static Path getAspectJLib(Project project, Module module, String name) {
String libDir = project.getProperty("aspectj.home");
if (null == libDir) {
libDir = project.getProperty("ASPECTJ_HOME");
}
if (null != libDir) {
libDir += File.separator + "lib";
} else {
libDir = module.getFullPath(ASPECTJLIB_RPATH);
}
return new Path(project, libDir + File.separator + name);
}
/**
* Wrap AspectJ compiler as Task.
* Only works for javac-like source compilation of everything
* under srcDir.
* Written reflectively to compile in the build module,
* which can't depend on the whole tree.
* TODO output from ajc is hidden on failure.
* @param javac the Javac specification
* @param toolsJar the Path to the aspectjtools.jar
* @param runtimeJar the Path to the aspectjrt.jar
* @return javac or another Task invoking the AspectJ compiler
*/
static Task aspectJTask(Javac javac, Path toolsJar, Path runtimeJar) {
Object task = null;
try {
String url = "file:" + toolsJar.toString().replace('\\', '/');
ClassLoader loader = new URLClassLoader(new URL[] {new URL(url)});
Class c = loader.loadClass(AJCTASK);
task = c.newInstance();
Project project = javac.getProject();
Method m = c.getMethod("setupAjc", new Class[] {Javac.class});
m.invoke(task, new Object[] {javac});
m = c.getMethod("setFork", new Class[] {boolean.class});
m.invoke(task, new Object[] {Boolean.TRUE});
m = c.getMethod("setForkclasspath", new Class[] {Path.class});
m.invoke(task, new Object[] {toolsJar});
m = c.getMethod("setSourceRoots", new Class[] {Path.class});
m.invoke(task, new Object[] {javac.getSrcdir()});
if (null != runtimeJar) {
m = c.getMethod("setClasspath", new Class[] {Path.class});
m.invoke(task, new Object[] {runtimeJar});
}
} catch (BuildException e) {
throw e;
} catch (Throwable t) {
throw new BuildException(t);
}
return (Task) task;
}
private AspectJSupport() {throw new Error("no instances");}
}
}
// finally caught by failing to comply with proper ant initialization
// /**
// * Build a module that has a build script.
// * @param buildSpec the module to build
// * @param buildScript the script file
// * @throws BuildException if build fails
// */
// private void buildByScript(BuildSpec buildSpec, File buildScript)
// throws BuildException {
// Ant ant = new Ant();
// ant.setProject(getProject());
// ant.setAntfile(buildScript.getAbsolutePath());
// ant.setDescription("building module " + buildSpec.module);
// ant.setDir(buildScript.getParentFile());
// ant.setInheritAll(true);
// ant.setInheritRefs(false);
// ant.setLocation(getLocation());
// ant.setOwningTarget(getOwningTarget());
// // by convention, for build.xml, use module name to publish
// ant.setTarget(buildSpec.module);
// ant.setTaskName("ant");
// loadAntProperties(ant, buildSpec);
// ant.execute();
// }
//
// /** override definitions */
// private void loadAntProperties(Ant ant, BuildSpec buildSpec) {
// Property property = ant.createProperty();
// property.setName(BuildSpec.baseDir_NAME);
// property.setFile(buildSpec.baseDir);
// property = ant.createProperty();
// property.setName(buildSpec.distDir_NAME);
// property.setFile(buildSpec.distDir);
// property = ant.createProperty();
// property.setName(BuildSpec.tempDir_NAME);
// property.setFile(buildSpec.tempDir);
// property = ant.createProperty();
// property.setName(BuildSpec.jarDir_NAME);
// property.setFile(buildSpec.jarDir);
// property = ant.createProperty();
// property.setName(BuildSpec.stagingDir_NAME);
// property.setFile(buildSpec.stagingDir);
// }
/**
* Segregate product-building API's from module-building APIs for clarity.
* These are called by the superclass if the BuildSpec warrants.
* XXX extremely brittle/arbitrary assumptions.
* @see BuildModule for assumptions
*/
class ProductBuilder extends AntBuilder {
private static String getProductInstallResourcesSrc(BuildSpec buildSpec) {
final String resourcesName = "installer-resources"; // XXXFileLiteral
File dir = buildSpec.productDir.getParentFile();
// String result = null;
if (null == dir) {
return "../../" + resourcesName;
}
dir = dir.getParentFile();
if (null == dir) {
return "../" + resourcesName;
} else {
dir = new File(dir, resourcesName);
return dir.getPath();
}
}
private static String getProductInstallerFileName(BuildSpec buildSpec) { // XXXFileLiteral
return "aspectj-"
+ buildSpec.productDir.getName()
+ "-"
+ Util.shortVersion(buildSpec.version)
+ ".jar";
}
/**
* Calculate name of main, typically InitialCap, and hence installer class.
* @return $$installer$$.org.aspectj." + ProductName + "Installer"
*/
private static String getProductInstallerMainClass(BuildSpec buildSpec) {
String productName = buildSpec.productDir.getName();
String initial = productName.substring(0, 1).toUpperCase();
productName = initial + productName.substring(1);
return "$installer$.org.aspectj." + productName + "Installer"; // XXXNameLiteral
}
/** @see Builder.getBuilder(String, Project, File) */
ProductBuilder(
Project project,
File tempDir,
boolean useEclipseCompiles,
Messager handler) {
super(project, tempDir, useEclipseCompiles, handler);
}
/**
* Build product by discovering any modules to build,
* building those, assembling the product distribution,
* and optionally creating an installer for it.
* @return true on success
*/
protected boolean buildProduct(BuildSpec buildSpec)
throws BuildException {
Util.iaxIfNull(buildSpec, "buildSpec");
// XXX if installer and not out of date, do not rebuild unless rebuild set
if (!buildSpec.trimTesting) {
buildSpec.trimTesting = true;
handler.log("testing trimmed for " + buildSpec);
}
Util.iaxIfNotCanReadDir(buildSpec.productDir, "productDir");
Util.iaxIfNotCanReadDir(buildSpec.baseDir, "baseDir");
Util.iaxIfNotCanWriteDir(buildSpec.distDir, "distDir");
// ---- discover modules to build, and build them
Modules modules = new Modules(
buildSpec.baseDir,
buildSpec.jarDir,
buildSpec.trimTesting,
handler);
ProductModule[] productModules = discoverModules(buildSpec.productDir, modules);
for (int i = 0; i < productModules.length; i++) {
if (buildSpec.verbose) {
handler.log("building product module " + productModules[i]);
}
if (!buildProductModule(productModules[i])) {
return false;
}
}
if (buildSpec.verbose) {
handler.log("assembling product module for " + buildSpec);
}
// ---- assemble product distribution
final String productName = buildSpec.productDir.getName();
final File targDir = new File(buildSpec.distDir, productName);
final String targDirPath = targDir.getPath();
if (targDir.canWrite()) {
Util.deleteContents(targDir);
}
if (!targDir.canWrite() && !targDir.mkdirs()) {
if (buildSpec.verbose) {
handler.log("buildProduct unable to create " + targDir);
}
return false;
}
// filter-copy everything but the binaries
Copy copy = makeCopyTask(true);
copy.setTodir(targDir);
File distDir = new File(buildSpec.productDir, "dist"); // XXXFileLiteral
Util.iaxIfNotCanReadDir(distDir, "product dist directory");
FileSet fileset = new FileSet();
fileset.setDir(distDir);
fileset.setExcludes(Builder.BINARY_SOURCE_PATTERN);
copy.addFileset(fileset);
if (!executeTask(copy)) {
return false;
}
// copy binaries (but not module flag files)
String excludes = null;
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < productModules.length; i++) {
if (0 < buf.length()) {
buf.append(",");
}
buf.append(productModules[i].relativePath);
}
if (0 < buf.length()) {
excludes = buf.toString();
}
}
copy = makeCopyTask(false);
copy.setTodir(targDir);
fileset = new FileSet();
fileset.setDir(distDir);
fileset.setIncludes(Builder.BINARY_SOURCE_PATTERN);
if (null != excludes) {
fileset.setExcludes(excludes);
}
copy.addFileset(fileset);
if (!executeTask(copy)) {
return false;
}
// copy binaries associated with module flag files
for (int i = 0; i < productModules.length; i++) {
ProductModule product = productModules[i];
String targPath = targDirPath + "/" + product.relativePath;
File jarFile = (product.assembleAll
? product.module.getAssembledJar()
: product.module.getModuleJar() );
copyFile(jarFile, new File(targPath), FILTER_OFF);
}
handler.log("created product in " + targDir);
// ---- create installer
if (buildSpec.createInstaller) {
return buildInstaller(buildSpec, targDirPath);
} else {
return true;
}
}
protected boolean buildInstaller(BuildSpec buildSpec, String targDirPath) {
if (buildSpec.verbose) {
handler.log("creating installer for " + buildSpec);
}
AJInstaller installer = new AJInstaller();
setupTask(installer, "installer");
installer.setBasedir(targDirPath);
//installer.setCompress();
File installSrcDir = new File(buildSpec.productDir, "install"); // XXXFileLiteral
Util.iaxIfNotCanReadDir(installSrcDir, "installSrcDir");
installer.setHtmlSrc(installSrcDir.getPath());
String resourcePath = getProductInstallResourcesSrc(buildSpec);
File resourceSrcDir = new File(resourcePath);
Util.iaxIfNotCanReadDir(resourceSrcDir, "resourceSrcDir");
installer.setResourcesSrc(resourcePath);
String name = getProductInstallerFileName(buildSpec);
File outFile = new File(buildSpec.jarDir, name);
installer.setZipfile(outFile.getPath());
installer.setMainclass(getProductInstallerMainClass(buildSpec));
installer.setInstallerclassjar(getBuildJar(buildSpec));
return executeTask(installer);
// -- test installer XXX
// create text setup file
// run installer with setup file
// cleanup installed product
}
private String getBuildJar(BuildSpec buildSpec) {
return buildSpec.baseDir.getPath()
+ "/lib/build/build.jar" ; // XXX
}
// private Module moduleForReplaceFile(File replaceFile, Modules modules) {
// String jarName = moduleAliasFor(replaceFile.getName().toLowerCase());
// if (jarName.endsWith(".jar") || jarName.endsWith(".zip")) { // XXXFileLiteral
// jarName = jarName.substring(0, jarName.length()-4);
// } else {
// throw new IllegalArgumentException("can only replace .[jar|zip]");
// }
// boolean assembleAll = jarName.endsWith("-all");
// String name = (!assembleAll ? jarName : jarName.substring(0, jarName.length()-4));
// return modules.getModule(name);
// }
//
}
class ProjectMessager extends Messager {
private final Project project;
public ProjectMessager(Project project) {
Util.iaxIfNull(project, "project");
this.project = project;
}
public boolean log(String s) {
project.log(s);
return true;
}
public boolean error(String s) {
project.log(s, Project.MSG_ERR);
return true;
}
public boolean logException(String context, Throwable thrown) {
project.log(context + Util.renderException(thrown), Project.MSG_ERR);
return true;
}
}
|