private void executeGroovyScript() { try { GroovyShell shell = new GroovyShell(); resolvePropertiesUsingLambdas(); properties.forEach((key, value) -> shell.setProperty(key, value)); shell.evaluate(IOUtils.getResourceAsString(scriptPath, GroovyScriptExecutor.class)); LOGGER.info("Groovy script executed"); } catch (IOException e) { throw new RuntimeException("Error reading Groovy script: " + scriptPath, e); } catch (CompilationFailedException e) { throw new RuntimeException("Compilation error were found on: " + scriptPath, e); } }
private GroovyShell prepareScript() { GroovyShell shell = new GroovyShell(); for (Map.Entry<String, Object> entry : properties.entrySet()) { shell.setProperty(entry.getKey(), entry.getValue()); } return shell; }
private Object modifyJavaObject(final Object data, final String variableName, final String groovyPlaceholderAccessExpression, final String setterName, final Object variableValue, final ClassLoader classLoader) { final GroovyShell shell = new GroovyShell(classLoader); shell.setProperty(variableName, data); shell.setProperty("__variableValue__", variableValue); final StringBuilder script = new StringBuilder(); script.append("def __tmp__ ="); if (groovyPlaceholderAccessExpression != null && groovyPlaceholderAccessExpression.trim().length() > 0) { script.append(groovyPlaceholderAccessExpression); } else { script.append(variableName); } script.append(";\n"); script.append("__tmp__."); script.append(setterName); script.append("(__variableValue__);\n"); script.append(variableName); return shell.evaluate(script.toString()); }
protected void executeGroovyScript() throws MojoExecutionException { GroovyShell shell = new GroovyShell(); getLog().info("executing script: " + script.getAbsolutePath()); shell.setProperty("basedir",mavenProject.getBasedir()); for (Map.Entry entry: mavenProject.getProperties().entrySet()) { shell.setProperty((String) entry.getKey(), entry.getValue()); } getLog().info(mavenProject.getBasedir().getAbsolutePath()); try { shell.evaluate(readFile(script.getAbsolutePath())); } catch (IOException e) { throw new MojoExecutionException("error executing script: " + script.getAbsolutePath() + "\n" + e.getMessage() ); } }
@Test public void testInvokeRawMethod() throws Exception { ListMethods itf = list -> { JsonObject combined = new JsonObject(); list.forEach(combined::mergeIn); return combined; }; CompilerConfiguration config = new CompilerConfiguration(); Properties props = new Properties(); props.setProperty("groovy.disabled.global.ast.transformations", "io.vertx.lang.groovy.VertxTransformation"); config.configure(props); GroovyShell shell = new GroovyShell(config); shell.setProperty("itf", itf); Object o = shell.evaluate("return itf.jsonList([new io.vertx.core.json.JsonObject().put('foo', 'foo_value'), new io.vertx.core.json.JsonObject().put('bar', 'bar_value')])"); JsonObject result = (JsonObject) o; assertEquals(result, new JsonObject().put("foo", "foo_value").put("bar", "bar_value")); } }