@SuppressWarnings({"unchecked", "rawtypes"})
  private int executeScriptWithCaching(CommandLine commandLine, String scriptName, String env) {
    List<File> potentialScripts;
    List<File> allScripts = getAvailableScripts();
    GantBinding binding = new GantBinding();
    binding.setVariable("scriptName", scriptName);

    setDefaultInputStream(binding);

    // Now find what scripts match the one requested by the user.
    potentialScripts = getPotentialScripts(scriptName, allScripts);

    if (potentialScripts.size() == 0) {
      try {
        File aliasFile = new File(settings.getUserHome(), ".grails/.aliases");
        if (aliasFile.exists()) {
          Properties aliasProperties = new Properties();
          aliasProperties.load(new FileReader(aliasFile));
          if (aliasProperties.containsKey(commandLine.getCommandName())) {
            String aliasValue = (String) aliasProperties.get(commandLine.getCommandName());
            String[] aliasPieces = aliasValue.split(" ");
            String commandName = aliasPieces[0];
            String correspondingScriptName = GrailsNameUtils.getNameFromScript(commandName);
            potentialScripts = getPotentialScripts(correspondingScriptName, allScripts);

            if (potentialScripts.size() > 0) {
              String[] additionalArgs = new String[aliasPieces.length - 1];
              System.arraycopy(aliasPieces, 1, additionalArgs, 0, additionalArgs.length);
              insertArgumentsInFrontOfExistingArguments(commandLine, additionalArgs);
            }
          }
        }
      } catch (Exception e) {
        console.error(e);
      }
    }

    // First try to load the script from its file. If there is no
    // file, then attempt to load it as a pre-compiled script. If
    // that fails, then let the user know and then exit.
    if (potentialScripts.size() > 0) {
      potentialScripts = (List) DefaultGroovyMethods.unique(potentialScripts);
      final File scriptFile = potentialScripts.get(0);
      if (!isGrailsProject() && !isExternalScript(scriptFile)) {
        return handleScriptExecutedOutsideProjectError();
      }
      return executeScriptFile(commandLine, scriptName, env, binding, scriptFile);
    }

    return attemptPrecompiledScriptExecute(commandLine, scriptName, env, binding, allScripts);
  }
  private void setDefaultInputStream(GantBinding binding) {

    // Gant does not initialise the default input stream for
    // the Ant project, so we manually do it here.
    AntBuilder antBuilder = (AntBuilder) binding.getVariable("ant");
    Project p = antBuilder.getAntProject();

    try {
      System.setIn(originalIn);
      p.setInputHandler(new CommandLineInputHandler());
      p.setDefaultInputStream(originalIn);
    } catch (NoSuchMethodError nsme) {
      // will only happen due to a bug in JRockit
      // note - the only approach that works is to loop through the public methods
      for (Method m : p.getClass().getMethods()) {
        if ("setDefaultInputStream".equals(m.getName())
            && m.getParameterTypes().length == 1
            && InputStream.class.equals(m.getParameterTypes()[0])) {
          try {
            m.invoke(p, originalIn);
            break;
          } catch (Exception e) {
            // shouldn't happen, but let it bubble up to the catch(Throwable)
            throw new RuntimeException(e);
          }
        }
      }
    }
  }
 @SuppressWarnings("rawtypes")
 private void cleanup(GantResult result, GantBinding binding) {
   if (result != null) {
     Class cls = GantMetaClass.class;
     try {
       Field methodsInvoked = cls.getDeclaredField("methodsInvoked");
       methodsInvoked.setAccessible(true);
       Set methodsInvokedSet = (Set) methodsInvoked.get(cls);
       if (methodsInvokedSet != null) {
         methodsInvokedSet.clear();
       }
     } catch (NoSuchFieldException e) {
       // ignore
     } catch (IllegalAccessException e) {
       // ignore
     }
   }
   System.setIn(originalIn);
   System.setOut(originalOut);
   GrailsPluginUtils.clearCaches();
   Map variables = binding.getVariables();
   Object pluginsSettingsObject = variables.get("pluginsSettings");
   if (pluginsSettingsObject instanceof PluginBuildSettings) {
     ((PluginBuildSettings) pluginsSettingsObject).clearCache();
   }
   GroovySystem.getMetaClassRegistry().removeMetaClass(GantBinding.class);
   GroovySystem.getMetaClassRegistry().removeMetaClass(Gant.class);
 }
  private int executeScriptFile(
      CommandLine commandLine,
      String scriptName,
      String env,
      GantBinding binding,
      Resource scriptFile) {
    // We can now safely set the default environment
    String scriptFileName = getScriptNameFromFile(scriptFile);
    setRunningEnvironment(commandLine, env);
    binding.setVariable("scriptName", scriptFileName);

    // Setup the script to call.
    ScriptBindingInitializer bindingInitializer =
        new ScriptBindingInitializer(commandLine, settings, pluginPathSupport, isInteractive);
    Gant gant = new Gant(bindingInitializer.initBinding(binding, scriptName), classLoader);
    gant.setUseCache(true);
    gant.setCacheDirectory(scriptCacheDir);
    GantResult result = null;
    try {
      gant.loadScript(scriptFile.getURL());
      result = executeWithGantInstance(gant, DO_NOTHING_CLOSURE);
      return result.exitCode;
    } catch (IOException e) {
      console.error("I/O exception loading script [" + e.getMessage() + "]: " + e.getMessage());
      return 1;
    } finally {
      cleanup(result, binding);
    }
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private int executeScriptWithCaching(CommandLine commandLine, String scriptName, String env) {
    List<Resource> potentialScripts;
    List<Resource> allScripts = getAvailableScripts();
    GantBinding binding = new GantBinding();
    binding.setVariable("scriptName", scriptName);

    setDefaultInputStream(binding);

    // Now find what scripts match the one requested by the user.
    boolean exactMatchFound = false;
    potentialScripts = new ArrayList<Resource>();
    for (Resource scriptPath : allScripts) {
      String scriptFileName =
          scriptPath
              .getFilename()
              .substring(0, scriptPath.getFilename().length() - 7); // trim .groovy extension
      if (scriptFileName.endsWith("_")) {
        scriptsAllowedOutsideOfProject.add(scriptPath);
        scriptFileName = scriptFileName.substring(0, scriptFileName.length() - 1);
      }

      if (scriptFileName.equals(scriptName)) {
        potentialScripts.add(scriptPath);
        exactMatchFound = true;
        continue;
      }

      if (!exactMatchFound && ScriptNameResolver.resolvesTo(scriptName, scriptFileName)) {
        potentialScripts.add(scriptPath);
      }
    }

    // First try to load the script from its file. If there is no
    // file, then attempt to load it as a pre-compiled script. If
    // that fails, then let the user know and then exit.
    if (potentialScripts.size() > 0) {
      potentialScripts = (List) DefaultGroovyMethods.unique(potentialScripts);
      final Resource scriptFile = potentialScripts.get(0);
      if (!isGrailsProject() && !isExternalScript(scriptFile)) {
        return handleScriptExecutedOutsideProjectError();
      }
      return executeScriptFile(commandLine, scriptName, env, binding, scriptFile);
    }

    return attemptPrecompiledScriptExecute(commandLine, scriptName, env, binding, allScripts);
  }
  private int attemptPrecompiledScriptExecute(
      CommandLine commandLine,
      String scriptName,
      String env,
      GantBinding binding,
      List<File> allScripts) {
    console.updateStatus("Running pre-compiled script");

    // Must be called before the binding is initialised.
    setRunningEnvironment(commandLine, env);

    // Get Gant to load the class by name using our class loader.
    ScriptBindingInitializer bindingInitializer =
        new ScriptBindingInitializer(
            commandLine, classLoader, settings, pluginPathSupport, isInteractive);
    Gant gant = new Gant(bindingInitializer.initBinding(binding, scriptName), classLoader);

    try {
      loadScriptClass(gant, scriptName);
    } catch (ScriptNotFoundException e) {
      if (!isInteractive || InteractiveMode.isActive()) {
        throw e;
      }
      scriptName = fixScriptName(scriptName, allScripts);
      if (scriptName == null) {
        throw e;
      }

      try {
        loadScriptClass(gant, scriptName);
      } catch (ScriptNotFoundException ce) {
        return executeScriptWithCaching(commandLine, scriptName, env);
      }

      // at this point if they were calling a script that has a non-default
      // env (e.g. war or test-app) it wouldn't have been correctly set, so
      // set it now, but only if they didn't specify the env (e.g. "grails test war" -> "grails test
      // war")

      if (Boolean.TRUE.toString().equals(System.getProperty(Environment.DEFAULT))) {
        commandLine.setCommand(GrailsNameUtils.getScriptName(scriptName));
        env = commandLine.lookupEnvironmentForCommand();
        binding.setVariable("grailsEnv", env);
        settings.setGrailsEnv(env);
        System.setProperty(Environment.KEY, env);
        settings.setDefaultEnv(false);
        System.setProperty(Environment.DEFAULT, Boolean.FALSE.toString());
      }
    }

    return executeWithGantInstance(gant, DO_NOTHING_CLOSURE, binding).exitCode;
  }