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

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