ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant Main.java
Date Thu, 23 Jan 2003 16:33:25 GMT
bodewig     2003/01/23 08:33:25

  Modified:    .        WHATSNEW
               src/main/org/apache/tools/ant Main.java
  Log:
  handle errors in command line arguments by throwing exception, making
  Jon Skeet happy and fixing
  
  PR: 16123
  
  Revision  Changes    Path
  1.346     +3 -0      jakarta-ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
  retrieving revision 1.345
  retrieving revision 1.346
  diff -u -r1.345 -r1.346
  --- WHATSNEW	22 Jan 2003 12:36:40 -0000	1.345
  +++ WHATSNEW	23 Jan 2003 16:33:24 -0000	1.346
  @@ -99,6 +99,9 @@
   
   * <jar update="true"> would remove the original manifest.
   
  +* Ant will now exit with a return code of 1 if it encounters problems
  +  with the command line arguments.
  +
   Other changes:
   --------------
   * **/.DS_Store has been added to the list of default pattern excludes.
  
  
  
  1.77      +41 -43    jakarta-ant/src/main/org/apache/tools/ant/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Main.java,v
  retrieving revision 1.76
  retrieving revision 1.77
  diff -u -r1.76 -r1.77
  --- Main.java	8 Jan 2003 10:05:23 -0000	1.76
  +++ Main.java	23 Jan 2003 16:33:24 -0000	1.77
  @@ -178,10 +178,11 @@
               Diagnostics.validateVersion();
               m = new Main(args);
           } catch (Throwable exc) {
  +            handleLogfile();
               printMessage(exc);
               System.exit(1);
           }
  -
  +        
           if (additionalUserProperties != null) {
               for (Enumeration e = additionalUserProperties.keys();
                       e.hasMoreElements();) {
  @@ -204,24 +205,33 @@
               exc.printStackTrace();
               printMessage(exc);
           } finally {
  -            if (isLogFileUsed) {
  -                if (out != null) {
  -                    try {
  -                        out.close();
  -                    } catch (final Exception e) {
  -                        //ignore
  -                    }
  -                }
  -                if (err != null) {
  -                    try {
  -                        err.close();
  -                    } catch (final Exception e) {
  -                        //ignore
  -                    }
  +            handleLogfile();
  +        }
  +        System.exit(exitCode);
  +    }
  +
  +    /**
  +     * Close logfiles, if we have been writing to them.
  +     *
  +     * @since Ant 1.6
  +     */
  +    private static void handleLogfile() {
  +        if (isLogFileUsed) {
  +            if (out != null) {
  +                try {
  +                    out.close();
  +                } catch (final Exception e) {
  +                    //ignore
  +                }
  +            }
  +            if (err != null) {
  +                try {
  +                    err.close();
  +                } catch (final Exception e) {
  +                    //ignore
                   }
               }
           }
  -        System.exit(exitCode);
       }
   
       /**
  @@ -235,10 +245,6 @@
           start(args, null, null);
       }
   
  -    // XXX: (Jon Skeet) Error handling appears to be inconsistent here.
  -    // Sometimes there's just a return statement, and sometimes a
  -    // BuildException is thrown. What's the rationale for when to do
  -    // what?
       /**
        * Sole constructor, which parses and deals with command line
        * arguments.
  @@ -285,13 +291,11 @@
                       String msg = "Cannot write on the specified log file. "
                           + "Make sure the path exists and you have write "
                           + "permissions.";
  -                    System.out.println(msg);
  -                    return;
  +                    throw new BuildException(msg);
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
                       String msg = "You must specify a log file when " +
                           "using the -log argument";
  -                    System.out.println(msg);
  -                    return;
  +                    throw new BuildException(msg);
                   }
               } else if (arg.equals("-buildfile") || arg.equals("-file")
                          || arg.equals("-f")) {
  @@ -301,8 +305,7 @@
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
                       String msg = "You must specify a buildfile when " +
                           "using the -buildfile argument";
  -                    System.out.println(msg);
  -                    return;
  +                    throw new BuildException(msg);
                   }
               } else if (arg.equals("-listener")) {
                   try {
  @@ -311,8 +314,7 @@
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
                       String msg = "You must specify a classname when " +
                           "using the -listener argument";
  -                    System.out.println(msg);
  -                    return;
  +                    throw new BuildException(msg);
                   }
               } else if (arg.startsWith("-D")) {
   
  @@ -335,34 +337,31 @@
                       name = name.substring(0, posEq);
                   } else if (i < args.length - 1) {
                       value = args[++i];
  -                       }
  +                }
   
                   definedProps.put(name, value);
               } else if (arg.equals("-logger")) {
                   if (loggerClassname != null) {
  -                    System.out.println("Only one logger class may "
  +                    throw new BuildException("Only one logger class may "
                           + " be specified.");
  -                    return;
                   }
                   try {
                       loggerClassname = args[++i];
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
  -                    System.out.println("You must specify a classname when " +
  -                                       "using the -logger argument");
  -                    return;
  +                    throw new BuildException("You must specify a classname when"
  +                                             + " using the -logger argument");
                   }
               } else if (arg.equals("-inputhandler")) {
                   if (inputHandlerClassname != null) {
  -                    System.out.println("Only one input handler class may " +
  -                                       "be specified.");
  -                    return;
  +                    throw new BuildException("Only one input handler class may "
  +                                             + "be specified.");
                   }
                   try {
                       inputHandlerClassname = args[++i];
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
  -                    System.out.println("You must specify a classname when " +
  -                                       "using the -inputhandler argument");
  -                    return;
  +                    throw new BuildException("You must specify a classname when"
  +                                             + " using the -inputhandler"
  +                                             + " argument");
                   }
               } else if (arg.equals("-emacs")) {
                   emacsMode = true;
  @@ -383,15 +382,14 @@
                   } catch (ArrayIndexOutOfBoundsException aioobe) {
                       String msg = "You must specify a property filename when " +
                           "using the -propertyfile argument";
  -                    System.out.println(msg);
  -                    return;
  +                    throw new BuildException(msg);
                   }
               } else if (arg.startsWith("-")) {
                   // we don't have any more args to recognize!
                   String msg = "Unknown argument: " + arg;
                   System.out.println(msg);
                   printUsage();
  -                return;
  +                throw new BuildException("");
               } else {
                   // if it's no other arg, it may be the target
                   targets.addElement(arg);
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message