1 12 13 package org.eclipse.ant.internal.ui.antsupport; 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileNotFoundException ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.PrintStream ; 22 import java.text.MessageFormat ; import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 33 import org.apache.tools.ant.AntTypeDefinition; 34 import org.apache.tools.ant.BuildEvent; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.BuildListener; 37 import org.apache.tools.ant.BuildLogger; 38 import org.apache.tools.ant.ComponentHelper; 39 import org.apache.tools.ant.DefaultLogger; 40 import org.apache.tools.ant.DemuxOutputStream; 41 import org.apache.tools.ant.Diagnostics; 42 import org.apache.tools.ant.Main; 43 import org.apache.tools.ant.Project; 44 import org.apache.tools.ant.ProjectHelper; 45 import org.apache.tools.ant.Target; 46 import org.apache.tools.ant.Task; 47 import org.apache.tools.ant.TaskAdapter; 48 import org.apache.tools.ant.util.FileUtils; 49 import org.eclipse.ant.internal.ui.antsupport.logger.RemoteAntBuildLogger; 50 51 55 public class InternalAntRunner { 56 57 60 public static final int MSG_PROJECT_HELP= Project.MSG_DEBUG + 1; 61 62 private List buildListeners; 63 64 private String buildFileLocation; 65 66 69 private Vector targets; 70 71 private Map userProperties; 72 73 private Project currentProject; 74 75 private BuildLogger buildLogger= null; 76 77 private Map eclipseSpecifiedTasks; 78 private Map eclipseSpecifiedTypes; 79 80 83 private String antVersionNumber= null; 84 85 86 private int messageOutputLevel = Project.MSG_INFO; 87 88 89 private boolean emacsMode = false; 90 91 92 private boolean projectHelp = false; 93 94 95 private PrintStream out = System.out; 96 97 98 private PrintStream err = System.err; 99 100 106 private String loggerClassname = null; 107 108 109 private String [] extraArguments = null; 110 111 private boolean scriptExecuted= false; 112 113 private List propertyFiles= new ArrayList (); 114 115 118 private String inputHandlerClassname = null; 119 120 125 private boolean keepGoing= false; 126 127 131 private boolean allowInput = true; 132 133 private String fEarlyErrorMessage= null; 134 135 public static void main(String [] args) { 136 try { 137 new InternalAntRunner().run(getArrayList(args)); 138 } catch (Throwable t) { 139 t.printStackTrace(); 140 System.exit(1); 141 } 142 System.exit(0); 143 } 144 145 private void addBuildListeners(Project project) { 146 String className= null; 147 try { 148 BuildLogger logger= createLogger(); 149 if (logger != null) { 150 project.addBuildListener(logger); 151 } 152 if (buildListeners != null) { 153 for (Iterator iterator = buildListeners.iterator(); iterator.hasNext();) { 154 className = (String ) iterator.next(); 155 Class listener = Class.forName(className); 156 project.addBuildListener((BuildListener) listener.newInstance()); 157 } 158 } 159 } catch (ClassCastException e) { 160 String message = MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.{0}_which_was_specified_to_be_a_build_listener_is_not_an_instance_of_org.apache.tools.ant.BuildListener._1"), new String []{className}); logMessage(null, message, Project.MSG_ERR); 162 throw new BuildException(message, e); 163 } catch (BuildException e) { 164 throw e; 165 } catch (Exception e) { 166 throw new BuildException(e); 167 } 168 } 169 170 175 private void parseBuildFile(Project project) { 176 File buildFile = new File (getBuildFileLocation()); 177 if (!buildFile.exists()) { 178 throw new BuildException(MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Buildfile__{0}_does_not_exist_!_1"), new String []{buildFile.getAbsolutePath()})); 180 } 181 if (!buildFile.isFile()) { 182 throw new BuildException(MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Buildfile__{0}_is_not_a_file_1"), new String []{buildFile.getAbsolutePath()})); 184 } 185 186 if (!isVersionCompatible("1.5")) { parseBuildFile(project, buildFile); 188 } else { 189 ProjectHelper helper = ProjectHelper.getProjectHelper(); 190 project.addReference("ant.projectHelper", helper); helper.parse(project, buildFile); 192 } 193 } 194 195 198 private void parseBuildFile(Project project, File buildFile) { 199 ProjectHelper.configureProject(project, buildFile); 200 } 201 202 private void printArguments(Project project) { 203 if ((messageOutputLevel != Project.MSG_DEBUG) && (messageOutputLevel != Project.MSG_VERBOSE)) { 204 return; 205 } 206 StringBuffer sb = new StringBuffer (); 207 for (int i = 0; i < extraArguments.length; i++) { 208 sb.append(extraArguments[i]); 209 sb.append(' '); 210 } 211 project.log(MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Arguments__{0}_2"), new String []{sb.toString().trim()})); } 213 214 215 221 private void printTargets(Project project) { 222 logMessage(project, "", MSG_PROJECT_HELP); int maxLength = 0; 228 Enumeration ptargets = project.getTargets().elements(); 229 String targetName; 230 String targetDescription; 231 Target currentTarget; 232 List topNames = new ArrayList (); 235 List topDescriptions = new ArrayList (); 236 List subNames = new ArrayList (); 237 238 while (ptargets.hasMoreElements()) { 239 currentTarget = (Target) ptargets.nextElement(); 240 targetName = currentTarget.getName(); 241 targetDescription = currentTarget.getDescription(); 242 if (targetDescription == null) { 243 subNames.add(targetName); 244 } else { 245 topNames.add(targetName); 246 topDescriptions.add(targetDescription); 247 if (targetName.length() > maxLength) { 248 maxLength = targetName.length(); 249 } 250 } 251 } 252 253 Collections.sort(subNames); 254 Collections.sort(topNames); 255 Collections.sort(topDescriptions); 256 257 String defaultTargetName = project.getDefaultTarget(); 258 if (defaultTargetName != null && !"".equals(defaultTargetName)) { List defaultName = new ArrayList (1); 260 List defaultDesc = null; 261 defaultName.add(defaultTargetName); 262 263 int indexOfDefDesc = topNames.indexOf(defaultTargetName); 264 if (indexOfDefDesc >= 0) { 265 defaultDesc = new ArrayList (1); 266 defaultDesc.add(topDescriptions.get(indexOfDefDesc)); 267 } 268 printTargets(project, defaultName, defaultDesc, RemoteAntMessages.getString("InternalAntRunner.Default_target__3"), maxLength); 270 } 271 272 printTargets(project, topNames, topDescriptions, RemoteAntMessages.getString("InternalAntRunner.Main_targets__4"), maxLength); printTargets(project, subNames, null, RemoteAntMessages.getString("InternalAntRunner.Subtargets__5"), 0); } 275 276 285 private void printTargets(Project project, List names, List descriptions, String heading, int maxlen) { 286 String lSep = System.getProperty("line.separator"); 289 String spaces = " "; while (spaces.length() < maxlen) { 291 spaces += spaces; 292 } 293 StringBuffer msg = new StringBuffer (); 294 msg.append(heading + lSep + lSep); 295 for (int i = 0; i < names.size(); i++) { 296 msg.append(' '); 297 msg.append(names.get(i)); 298 if (descriptions != null) { 299 msg.append(spaces.substring(0, maxlen - ((String ) names.get(i)).length() + 2)); 300 msg.append(descriptions.get(i)); 301 } 302 msg.append(lSep); 303 } 304 logMessage(project, msg.toString(), Project.MSG_INFO); 305 } 306 307 311 private void run(List argList) { 312 setCurrentProject(new Project()); 313 if (isVersionCompatible("1.6.3")) { new ExecutorSetter().setExecutor(getCurrentProject()); 315 } 316 Throwable error = null; 317 PrintStream originalErr = System.err; 318 PrintStream originalOut = System.out; 319 InputStream originalIn= System.in; 320 321 SecurityManager originalSM= System.getSecurityManager(); 322 scriptExecuted= true; 323 try { 324 if (argList != null && (argList.remove("-projecthelp") || argList.remove("-p"))) { projectHelp = true; 326 } 327 getCurrentProject().init(); 328 if (argList != null) { 329 scriptExecuted= preprocessCommandLine(argList); 330 331 if (!scriptExecuted) { 332 return; 333 } 334 } 335 336 boolean exceptionState= processProperties(argList); 337 338 addBuildListeners(getCurrentProject()); 339 340 addInputHandler(getCurrentProject()); 341 342 remapSystemIn(); 343 System.setOut(new PrintStream (new DemuxOutputStream(getCurrentProject(), false))); 344 System.setErr(new PrintStream (new DemuxOutputStream(getCurrentProject(), true))); 345 346 if (!projectHelp) { 347 fireBuildStarted(getCurrentProject()); 348 } 349 350 if (fEarlyErrorMessage != null) { 351 logMessage(getCurrentProject(), fEarlyErrorMessage, Project.MSG_ERR); 355 if (exceptionState) { 356 throw new BuildException(fEarlyErrorMessage); 357 } 358 } 359 360 setProperties(getCurrentProject()); 363 364 if (argList != null && !argList.isEmpty()) { 365 try { 366 scriptExecuted= processCommandLine(argList); 367 } catch (BuildException e) { 368 scriptExecuted= false; 369 throw e; 370 } 371 } 372 if (!scriptExecuted) { 373 return; 374 } 375 376 if (allowInput && (inputHandlerClassname != null && inputHandlerClassname.length() > 0)) { 378 if (isVersionCompatible("1.6")) { getCurrentProject().setDefaultInputStream(originalIn); 380 } 381 } else { 382 System.setProperty("eclipse.ant.noInput", "true"); if (isVersionCompatible("1.5") && (inputHandlerClassname == null || inputHandlerClassname.length() == 0)) { InputHandlerSetter setter= new InputHandlerSetter(); 387 setter.setInputHandler(getCurrentProject(), "org.eclipse.ant.internal.ui.antsupport.inputhandler.FailInputHandler"); } 389 } 390 391 getCurrentProject().log(MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Build_file__{0}_1"), new String []{getBuildFileLocation()})); 393 setTasks(); 394 setTypes(); 395 396 if (isVersionCompatible("1.6")) { getCurrentProject().setKeepGoingMode(keepGoing); 398 } 399 400 parseBuildFile(getCurrentProject()); 401 402 if (projectHelp) { 403 printHelp(getCurrentProject()); 404 scriptExecuted= false; 405 return; 406 } 407 408 if (extraArguments != null) { 409 printArguments(getCurrentProject()); 410 } 411 412 System.setSecurityManager(new AntSecurityManager(originalSM, Thread.currentThread())); 413 414 if (targets == null) { 415 targets= new Vector (1); 416 } 417 if (targets.isEmpty() && getCurrentProject().getDefaultTarget() != null) { 418 targets.add(getCurrentProject().getDefaultTarget()); 419 } 420 if (!isVersionCompatible("1.6.3")) { getCurrentProject().addReference("eclipse.ant.targetVector", targets); } 423 getCurrentProject().executeTargets(targets); 424 } catch (AntSecurityException e) { 425 } catch (Throwable e) { 427 error = e; 428 } finally { 429 System.setErr(originalErr); 430 System.setOut(originalOut); 431 System.setIn(originalIn); 432 if (System.getSecurityManager() instanceof AntSecurityManager) { 433 System.setSecurityManager(originalSM); 434 } 435 436 if (!projectHelp) { 437 fireBuildFinished(getCurrentProject(), error); 438 } 439 440 if (err != originalErr) { 442 err.close(); 443 } 444 if (out != originalOut) { 445 out.close(); 446 } 447 } 448 } 449 450 private void setTasks() { 451 if (eclipseSpecifiedTasks != null) { 452 Iterator itr= eclipseSpecifiedTasks.keySet().iterator(); 453 String taskName; 454 String taskClassName; 455 while (itr.hasNext()) { 456 taskName= (String ) itr.next(); 457 taskClassName= (String ) eclipseSpecifiedTasks.get(taskName); 458 459 if (isVersionCompatible("1.6")) { AntTypeDefinition def= new AntTypeDefinition(); 461 def.setName(taskName); 462 def.setClassName(taskClassName); 463 def.setClassLoader(this.getClass().getClassLoader()); 464 def.setAdaptToClass(Task.class); 465 def.setAdapterClass(TaskAdapter.class); 466 ComponentHelper.getComponentHelper(getCurrentProject()).addDataTypeDefinition(def); 467 } else { 468 try { 469 Class taskClass = Class.forName(taskClassName); 470 getCurrentProject().addTaskDefinition(taskName, taskClass); 471 } catch (ClassNotFoundException e) { 472 String message= MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.161"), new String []{taskClassName, taskName}); getCurrentProject().log(message, Project.MSG_WARN); 474 } 475 } 476 } 477 } 478 } 479 480 private void setTypes() { 481 if (eclipseSpecifiedTypes != null) { 482 Iterator itr= eclipseSpecifiedTypes.keySet().iterator(); 483 String typeName; 484 String typeClassName; 485 while (itr.hasNext()) { 486 typeName = (String ) itr.next(); 487 typeClassName= (String ) eclipseSpecifiedTypes.get(typeName); 488 if (isVersionCompatible("1.6")) { AntTypeDefinition def = new AntTypeDefinition(); 490 def.setName(typeName); 491 def.setClassName(typeClassName); 492 def.setClassLoader(this.getClass().getClassLoader()); 493 ComponentHelper.getComponentHelper(getCurrentProject()).addDataTypeDefinition(def); 494 } else { 495 try { 496 Class typeClass = Class.forName(typeClassName); 497 getCurrentProject().addDataTypeDefinition(typeName, typeClass); 498 } catch (ClassNotFoundException e) { 499 String message= MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.162"), new String []{typeClassName, typeName}); getCurrentProject().log(message, Project.MSG_WARN); 501 } 502 } 503 } 504 } 505 } 506 507 private void remapSystemIn() { 508 if (!isVersionCompatible("1.6")) { return; 510 } 511 DemuxInputStreamSetter setter= new DemuxInputStreamSetter(); 512 setter.remapSystemIn(getCurrentProject()); 513 } 514 515 521 private BuildLogger createLogger() { 522 if (loggerClassname == null) { 523 buildLogger= new DefaultLogger(); 524 } else if (!"".equals(loggerClassname)) { try { 526 buildLogger = (BuildLogger) (Class.forName(loggerClassname).newInstance()); 527 } catch (ClassCastException e) { 528 String message = MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.{0}_which_was_specified_to_perform_logging_is_not_an_instance_of_org.apache.tools.ant.BuildLogger._2"), new String []{loggerClassname}); logMessage(null, message, Project.MSG_ERR); 530 throw new BuildException(message, e); 531 } catch (Exception e) { 532 String message = MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Unable_to_instantiate_logger__{0}_6"), new String []{loggerClassname}); logMessage(null, message, Project.MSG_ERR); 534 throw new BuildException(message, e); 535 } 536 } 537 538 if (buildLogger != null) { 539 buildLogger.setMessageOutputLevel(messageOutputLevel); 540 buildLogger.setOutputPrintStream(out); 541 buildLogger.setErrorPrintStream(err); 542 buildLogger.setEmacsMode(emacsMode); 543 if (buildLogger instanceof RemoteAntBuildLogger) { 544 ((RemoteAntBuildLogger) buildLogger).configure(userProperties); 545 } 546 } 547 548 return buildLogger; 549 } 550 551 555 private void fireBuildStarted(Project project) { 556 if (!isVersionCompatible("1.5")) { BuildEvent event = new BuildEvent(project); 558 for (Iterator iterator = project.getBuildListeners().iterator(); iterator.hasNext();) { 559 BuildListener listener = (BuildListener) iterator.next(); 560 listener.buildStarted(event); 561 } 562 } else { 563 project.fireBuildStarted(); 564 } 565 } 566 567 private void fireBuildFinished(Project project, Throwable error) { 568 if (error == null && scriptExecuted) { 569 logMessage(project, RemoteAntMessages.getString("InternalAntRunner.BUILD_SUCCESSFUL_1"), messageOutputLevel); } 571 if (!isVersionCompatible("1.5")) { BuildEvent event = new BuildEvent(project); 573 event.setException(error); 574 Iterator iter = project.getBuildListeners().iterator(); 575 while (iter.hasNext()) { 576 BuildListener listener = (BuildListener) iter.next(); 577 listener.buildFinished(event); 578 } 579 } else { 580 project.fireBuildFinished(error); 581 } 582 } 583 584 private void logMessage(Project project, String message, int priority) { 585 if (project != null) { 586 project.log(message, priority); 587 } else { 588 if (buildListeners != null) { 589 project = new Project(); 590 BuildEvent event = new BuildEvent(project); 591 event.setMessage(message, priority); 592 for (Iterator iterator = buildListeners.iterator(); iterator.hasNext();) { 595 try { 596 BuildListener listener = (BuildListener) iterator.next(); 597 listener.messageLogged(event); 598 } catch (ClassCastException e) { 599 } 602 } 603 } 604 } 605 } 606 607 612 private void setBuildFileLocation(String buildFileLocation) { 613 this.buildFileLocation = buildFileLocation; 614 if (getCurrentProject() != null) { 615 getCurrentProject().setUserProperty("ant.file", buildFileLocation); } 617 } 618 619 private String getBuildFileLocation() { 620 if (buildFileLocation == null) { 621 buildFileLocation = new File ("build.xml").getAbsolutePath(); } 623 return buildFileLocation; 624 } 625 626 630 private void setMessageOutputLevel(int level) { 631 messageOutputLevel = level; 632 if (buildLogger != null) { 633 buildLogger.setMessageOutputLevel(level); 634 } 635 } 636 637 641 private String getAntVersionNumber() throws BuildException { 642 if (antVersionNumber == null) { 643 try { 644 Properties props = new Properties (); 645 InputStream in = Main.class.getResourceAsStream("/org/apache/tools/ant/version.txt"); props.load(in); 647 in.close(); 648 String versionNumber= props.getProperty("VERSION"); antVersionNumber= versionNumber; 650 } catch (IOException ioe) { 651 throw new BuildException(MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Could_not_load_the_version_information._{0}_9"), new String []{ioe.getMessage()})); } catch (NullPointerException npe) { 653 throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.Could_not_load_the_version_information._10")); } 655 } 656 return antVersionNumber; 657 } 658 659 664 private boolean isVersionCompatible(String comparison) { 665 String version= getAntVersionNumber(); 666 return version.compareTo(comparison) >= 0; 667 } 668 669 private boolean preprocessCommandLine(List commands) { 670 671 String arg = getArgument(commands, "-listener"); while (arg != null) { 673 if (arg.length() == 0) { 674 throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.You_must_specify_a_classname_when_using_the_-listener_argument_1")); } 676 if (buildListeners == null) { 677 buildListeners= new ArrayList (1); 678 } 679 buildListeners.add(arg); 680 arg = getArgument(commands, "-listener"); } 682 683 arg = getArgument(commands, "-logger"); if (arg != null) { 685 loggerClassname = arg; 687 } 688 arg = getArgument(commands, "-logger"); if (arg != null) { 690 throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.Only_one_logger_class_may_be_specified_1")); } 692 693 arg = getArgument(commands, "-inputhandler"); if (arg != null) { 695 if (arg.length() == 0) { 696 throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.You_must_specify_a_classname_when_using_the_-inputhandler_argument_1")); } 698 inputHandlerClassname = arg; 699 } 700 arg = getArgument(commands, "-inputhandler"); if (arg != null) { 702 throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.Only_one_input_handler_class_may_be_specified._2")); } 704 return true; 705 } 706 707 711 private boolean processCommandLine(List commands) { 712 713 if (commands.remove("-help") || commands.remove("-h")) { printUsage(); 715 return false; 716 } 717 718 if (commands.remove("-version")) { printVersion(); 720 return false; 721 } 722 723 if (commands.remove("-verbose") || commands.remove("-v")) { printVersion(); 725 setMessageOutputLevel(Project.MSG_VERBOSE); 726 } 727 728 if (commands.remove("-debug") || commands.remove("-d")) { printVersion(); 730 setMessageOutputLevel(Project.MSG_DEBUG); 731 } 732 733 if (commands.remove("-quiet") || commands.remove("-q")) { setMessageOutputLevel(Project.MSG_WARN); 735 } 736 737 if (commands.remove("-emacs") || commands.remove("-e")) { emacsMode = true; 739 if (buildLogger != null) { 740 buildLogger.setEmacsMode(true); 741 } 742 } 743 744 if (commands.remove("-diagnostics")) { if (!isVersionCompatible("1.5")) { throw new BuildException(RemoteAntMessages.getString("InternalAntRunner.The_diagnositics_options_is_an_Ant_1.5.*_feature._Please_update_your_Ant_classpath_to_include_an_Ant_version_greater_than_this._4")); } 748 try { 749 Diagnostics.doReport(System.out); 750 } catch (NullPointerException e) { 751 logMessage(getCurrentProject(), RemoteAntMessages.getString("InternalAntRunner.ANT_HOME_must_be_set_to_use_Ant_diagnostics_2"), Project.MSG_ERR); } 753 return false; 754 } 755 756 String arg = getArgument(commands, "-logfile"); if (arg == null) { 758 arg = getArgument(commands, "-l"); } 760 if (arg != null) { 761 if (arg.length() == 0) { 762 String message= RemoteAntMessages.getString("InternalAntRunner.You_must_specify_a_log_file_when_using_the_-log_argument_3"); logMessage(currentProject, message, Project.MSG_ERR); 764 throw new BuildException(message); 765 } 766 try { 767 createLogFile(arg); 768 } catch (IOException e) { 769 logMessage(getCurrentProject(), MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Could_not_write_to_the_specified_log_file__{0}._Make_sure_the_path_exists_and_you_have_write_permissions._2"), new String []{arg}), Project.MSG_ERR); return false; 772 } 773 774 } 775 776 arg = getArgument(commands, "-buildfile"); if (arg == null) { 778 arg = getArgument(commands, "-file"); if (arg == null) { 780 arg = getArgument(commands, "-f"); } 782 } 783 784 if (arg != null) { 785 if (arg.length() == 0) { 786 String message= RemoteAntMessages.getString("InternalAntRunner.You_must_specify_a_buildfile_when_using_the_-buildfile_argument_4"); logMessage(currentProject, message, Project.MSG_ERR); 788 throw new BuildException(message); 789 } 790 setBuildFileLocation(arg); 791 } 792 793 if (isVersionCompatible("1.6")) { if (commands.remove("-k") || commands.remove("-keep-going")) { keepGoing= true; 796 } 797 if (commands.remove("-noinput")) { allowInput= false; 799 } 800 arg= getArgument(commands, "-lib"); if (arg != null) { 802 logMessage(currentProject, RemoteAntMessages.getString("InternalAntRunner.157"), Project.MSG_ERR); return false; 804 } 805 } 806 807 arg= getArgument(commands, "-find"); if (arg == null) { 809 arg= getArgument(commands, "-s"); } 811 if (arg != null) { 812 logMessage(currentProject, RemoteAntMessages.getString("InternalAntRunner.-find_not_supported"), Project.MSG_ERR); return false; 814 } 815 816 processTasksAndTypes(commands); 817 818 if (!commands.isEmpty()) { 819 processUnrecognizedCommands(commands); 820 } 821 822 if (!commands.isEmpty()) { 823 processTargets(commands); 824 } 825 826 return true; 827 } 828 829 private void processTasksAndTypes(List commands) { 830 String arg = getArgument(commands, "-eclipseTask"); while (arg != null) { 832 if (eclipseSpecifiedTasks == null) { 833 eclipseSpecifiedTasks= new HashMap (); 834 } 835 int index= arg.indexOf(','); 836 if (index != -1) { 837 String name= arg.substring(0, index); 838 String className= arg.substring(index + 1); 839 eclipseSpecifiedTasks.put(name, className); 840 } 841 arg = getArgument(commands, "-eclipseTask"); } 843 844 arg = getArgument(commands, "-eclipseType"); while (arg != null) { 846 if (eclipseSpecifiedTypes == null) { 847 eclipseSpecifiedTypes= new HashMap (); 848 } 849 int index= arg.indexOf(','); 850 if (index != -1) { 851 String name= arg.substring(0, index); 852 String className= arg.substring(index + 1); 853 eclipseSpecifiedTypes.put(name, className); 854 } 855 arg = getArgument(commands, "-eclipseType"); } 857 } 858 859 869 private void processUnrecognizedCommands(List commands) { 870 int p = -1; 871 872 for (int i = commands.size() - 1; i >= 0; i--) { 874 if (((String ) commands.get(0)).startsWith("-")) { p = i; 876 break; 877 } 878 } 879 if (p < 0) { return; } 880 881 String s = ""; for (int i = 0; i <= p; i++) { 884 s += " " + ((String ) commands.get(0)); commands.remove(0); 886 } 887 888 String message = MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Unknown_argument__{0}_2"), new Object []{ s.substring(1) }); logMessage(currentProject, message, Project.MSG_WARN); 891 } 892 893 894 897 private void processTargets(List commands) { 898 if (targets == null) { 899 targets = new Vector (commands.size()); 900 } 901 for (Iterator iter = commands.iterator(); iter.hasNext();) { 902 targets.add(iter.next()); 903 } 904 } 905 906 912 private void createLogFile(String fileName) throws FileNotFoundException , IOException { 913 File logFile = getFileRelativeToBaseDir(fileName); 914 915 out = new PrintStream (new FileOutputStream (logFile)); 917 err = out; 918 logMessage(getCurrentProject(), MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Using_{0}_file_as_build_log._1"), new String []{logFile.getCanonicalPath()}), Project.MSG_INFO); if (buildLogger != null) { 920 buildLogger.setErrorPrintStream(err); 921 buildLogger.setOutputPrintStream(out); 922 } 923 } 924 925 private File getFileRelativeToBaseDir(String fileName) { 926 File parentFile= null; 927 928 String base= getCurrentProject().getUserProperty("basedir"); if (base != null) { 930 parentFile= new File (base); 931 } else { 932 parentFile= new File (getBuildFileLocation()).getParentFile(); 934 } 935 936 return FileUtils.newFileUtils().resolveFile(parentFile, fileName); 938 } 939 940 945 private boolean processProperties(List commands) { 946 boolean exceptionToBeThrown= false; 947 String arg= getArgument(commands, "-propertyfile"); while (arg != null) { 950 if (!isVersionCompatible("1.5")) { fEarlyErrorMessage= RemoteAntMessages.getString("InternalAntRunner.Specifying_property_files_is_a_Ant_1.5.*_feature._Please_update_your_Ant_classpath._6"); break; 953 } 954 if (arg.length() == 0) { 955 fEarlyErrorMessage= RemoteAntMessages.getString("InternalAntRunner.You_must_specify_a_property_filename_when_using_the_-propertyfile_argument_3"); exceptionToBeThrown= true; 957 break; 958 } 959 960 propertyFiles.add(arg); 961 arg= getArgument(commands, "-propertyfile"); } 963 964 if (propertyFiles != null && !propertyFiles.isEmpty()) { 965 loadPropertyFiles(); 966 } 967 968 if (commands != null) { 969 processMinusDProperties(commands); 970 } 971 return exceptionToBeThrown; 972 } 973 974 private void processMinusDProperties(List commands) { 975 String [] args = (String []) commands.toArray(new String [commands.size()]); 976 for (int i = 0; i < args.length; i++) { 977 String arg = args[i]; 978 if (arg.startsWith("-D")) { String name = arg.substring(2, arg.length()); 980 String value = null; 981 int posEq = name.indexOf("="); if (posEq == 0) { 983 value= name.substring(1); 984 name= ""; } else if (posEq > 0 && posEq != name.length() - 1) { 986 value = name.substring(posEq + 1).trim(); 987 name = name.substring(0, posEq); 988 } 989 990 if (value == null) { 991 continue; 993 } 994 if (userProperties == null) { 995 userProperties= new HashMap (); 996 } 997 userProperties.put(name, value); 998 commands.remove(args[i]); 999 } 1000 } 1001 } 1002 1003 private void setProperties(Project project) { 1004 setBuiltInProperties(project); 1005 if (userProperties != null) { 1006 for (Iterator iterator = userProperties.entrySet().iterator(); iterator.hasNext();) { 1007 Map.Entry entry = (Map.Entry ) iterator.next(); 1008 project.setUserProperty((String ) entry.getKey(), (String ) entry.getValue()); 1009 } 1010 } 1011 } 1012 1013 private void setBuiltInProperties(Project project) { 1014 project.setUserProperty("ant.file", getBuildFileLocation()); project.setUserProperty("ant.version", Main.getAntVersion()); } 1017 1018 1021 private void printHelp(Project project) { 1022 if (project.getDescription() != null) { 1023 logMessage(project, project.getDescription(), Project.MSG_INFO); 1024 } 1025 printTargets(project); 1026 } 1027 1028 1032 private void printVersion() { 1033 logMessage(getCurrentProject(), Main.getAntVersion(), Project.MSG_INFO); 1034 } 1035 1036 1039 private void printUsage() { 1040 String lSep = System.getProperty("line.separator"); StringBuffer msg = new StringBuffer (); 1042 msg.append("ant ["); msg.append(RemoteAntMessages.getString("InternalAntRunner.options_13")); msg.append("] ["); msg.append(RemoteAntMessages.getString("InternalAntRunner.target_15")); msg.append(" ["); msg.append(RemoteAntMessages.getString("InternalAntRunner.target_15")); msg.append("2 ["); msg.append(RemoteAntMessages.getString("InternalAntRunner.target_15")); msg.append("3] ...]]"); msg.append(lSep); 1052 msg.append(RemoteAntMessages.getString("InternalAntRunner.Options___21")); msg.append(lSep); 1054 msg.append("\t-help, -h\t\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.print_this_message_23")); msg.append(lSep); 1057 msg.append("\t-projecthelp, -p\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.print_project_help_information_25")); msg.append(lSep); 1060 msg.append("\t-version\t\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.print_the_version_information_and_exit_27")); msg.append(lSep); 1063 msg.append("\t-diagnostics\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.12")); msg.append(lSep); 1066 msg.append(RemoteAntMessages.getString("InternalAntRunner.13")); msg.append(lSep); 1068 msg.append("\t-quiet, -q\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.be_extra_quiet_29")); msg.append(lSep); 1071 msg.append("\t-verbose, -v\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.be_extra_verbose_31")); msg.append(lSep); 1074 msg.append("\t-debug, -d\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.print_debugging_information_33")); msg.append(lSep); 1077 msg.append("\t-emacs, -e\t\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.produce_logging_information_without_adornments_35")); msg.append(lSep); 1080 msg.append("\t-logfile\t<file>\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.use_given_file_for_log_37")); msg.append(lSep); 1083 msg.append("\t\t-l\t<file>"); msg.append(RemoteAntMessages.getString("InternalAntRunner.1")); msg.append(lSep); 1086 msg.append("\t-logger <classname>\t\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.the_class_which_is_to_perform_logging_39")); msg.append(lSep); 1089 msg.append("\t-listener <classname>\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.add_an_instance_of_class_as_a_project_listener_41")); msg.append(lSep); 1092 msg.append("\t-noinput\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.158")); msg.append(lSep); 1095 msg.append("\t-buildfile\t<file>\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.use_given_buildfile_43")); msg.append(lSep); 1098 msg.append("\t\t-file\t<file>"); msg.append(RemoteAntMessages.getString("InternalAntRunner.1")); msg.append(lSep); 1101 msg.append("\t\t-f\t\t<file>"); msg.append(RemoteAntMessages.getString("InternalAntRunner.1")); msg.append(lSep); 1104 msg.append("\t-D<property>=<value>\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.use_value_for_given_property_45")); msg.append(lSep); 1107 msg.append("\t-keep-going, -k"); msg.append(RemoteAntMessages.getString("InternalAntRunner.159")); msg.append(lSep); 1110 msg.append(RemoteAntMessages.getString("InternalAntRunner.160")); msg.append(lSep); 1112 msg.append("\t-propertyfile <name>\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.19")); msg.append(lSep); 1115 msg.append(RemoteAntMessages.getString("InternalAntRunner.20")); msg.append(lSep); 1117 msg.append("\t-inputhandler <class>\t"); msg.append(RemoteAntMessages.getString("InternalAntRunner.22")); msg.append(lSep); 1120 1121 logMessage(getCurrentProject(), msg.toString(), Project.MSG_INFO); 1122 } 1123 1124 1131 private String getArgument(List commands, String param) { 1132 if (commands == null) { 1133 return null; 1134 } 1135 int index = commands.indexOf(param); 1136 if (index == -1) { 1137 return null; 1138 } 1139 commands.remove(index); 1140 if (index == commands.size()) { return ""; } 1143 1144 String command = (String ) commands.get(index); 1145 if (command.startsWith("-")) { return ""; } 1148 1149 commands.remove(index); 1150 return command; 1151 } 1152 1153 1156 private static ArrayList getArrayList(String [] args) { 1157 if (args == null) { 1158 return null; 1159 } 1160 ArrayList result = new ArrayList (args.length); 1164 for (int i = 0; i < args.length; i++) { 1165 result.add(args[i]); 1166 } 1167 return result; 1168 } 1169 1170 private Project getCurrentProject() { 1171 return currentProject; 1172 } 1173 1174 private void setCurrentProject(Project currentProject) { 1175 this.currentProject = currentProject; 1176 } 1177 1178 1182 private void loadPropertyFiles() { 1183 Iterator itr= propertyFiles.iterator(); 1184 while (itr.hasNext()) { 1185 String filename= (String ) itr.next(); 1186 File file= getFileRelativeToBaseDir(filename); 1187 Properties props = new Properties (); 1188 FileInputStream fis = null; 1189 try { 1190 fis = new FileInputStream (file); 1191 props.load(fis); 1192 } catch (IOException e) { 1193 fEarlyErrorMessage= MessageFormat.format(RemoteAntMessages.getString("InternalAntRunner.Could_not_load_property_file_{0}__{1}_4"), new String []{filename, e.getMessage()}); } finally { 1195 if (fis != null) { 1196 try { 1197 fis.close(); 1198 } catch (IOException e){ 1199 } 1200 } 1201 } 1202 1203 if (userProperties == null) { 1204 userProperties= new HashMap (); 1205 } 1206 Enumeration propertyNames = props.propertyNames(); 1207 while (propertyNames.hasMoreElements()) { 1208 String name = (String ) propertyNames.nextElement(); 1209 if (userProperties.get(name) == null) { 1212 userProperties.put(name, props.getProperty(name)); 1213 } 1214 } 1215 } 1216 } 1217 1218 1224 private void addInputHandler(Project project) { 1225 if (!isVersionCompatible("1.5")) { return; 1227 } 1228 InputHandlerSetter setter= new InputHandlerSetter(); 1229 setter.setInputHandler(project, inputHandlerClassname); 1230 } 1231} | Popular Tags |