You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AntBuilder.java 26KB

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