Example #1
0
  /** Initializes the script, loading any required dependencies and running the script. */
  private void init() {
    int repos = 0;
    for (String repo : script.getHeaders("m2repo")) {
      Map<String, Object> args = Maps.newHashMap();
      args.put("name", "repo_" + repos++);
      args.put("root", repo);
      args.put("m2compatible", true);
      Grape.addResolver(args);
    }

    for (String dependency : script.getHeaders("dependency")) {
      String[] parts = dependency.split(":");
      if (parts.length >= 3) classLoader.loadDependency(parts[0], parts[1], parts[2]);
    }

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(classLoader);

      Script groovyScript = shell.parse(script.getBody(), scriptName);
      binding.setProperty("log", log);
      groovyScript.setMetaClass(new ScriptMetaClass(groovyScript.getMetaClass()));

      groovyScript.setBinding(binding);
      groovyScript.run();
    } catch (CompilationFailedException e) {
      log.error("Compilation of Groovy script failed: ", e);
      throw new RuntimeException("Compilation of Groovy script failed", e);
    } finally {
      Thread.currentThread().setContextClassLoader(cl);
    }
  }
Example #2
0
 /** Adds the specified URL to the classpath for this Groovy environment. */
 public void addToClasspath(URL url) {
   log.debug("trying to add " + url + " to " + classLoader);
   classLoader.addURL(url);
 }