diff --git a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/CarrotGroovyRunner.java b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/CarrotGroovyRunner.java index e24c060b..84e6828b 100644 --- a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/CarrotGroovyRunner.java +++ b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/CarrotGroovyRunner.java @@ -12,7 +12,10 @@ import java.io.File; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; +import org.slf4j.Logger; +import org.slf4j.impl.MavenLoggerFactory; /** * groovy script executor which exposes maven project @@ -20,9 +23,12 @@ public class CarrotGroovyRunner { private final MavenProject project; + + private final Log log; - public CarrotGroovyRunner(final MavenProject project) { + public CarrotGroovyRunner(final MavenProject project, Log log) { this.project = project; + this.log = log; } public Binding binding() { @@ -37,7 +43,7 @@ public Binding binding() { public Object execute(final File script) throws Exception { - final GroovyShell shell = new GroovyShell(binding()); + final GroovyShell shell = createShell(); final Object result = shell.evaluate(script); @@ -47,12 +53,25 @@ public Object execute(final File script) throws Exception { public Object execute(final String script) throws Exception { - final GroovyShell shell = new GroovyShell(binding()); + final GroovyShell shell = createShell(); final Object result = shell.evaluate(script); return result; } + + public GroovyShell createShell() { + + final GroovyShell shell = new GroovyShell(binding()); + shell.getContext().setProperty("log", createLogger()); + return shell; + } + + private Logger createLogger() { + String loggerName = String.format("%s.%s.Script", project.getGroupId(), + project.getArtifactId()); + return MavenLoggerFactory.getLogger(loggerName, log); + } } diff --git a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyBase.java b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyBase.java index 2e767208..5eb91722 100644 --- a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyBase.java +++ b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyBase.java @@ -35,6 +35,14 @@ public abstract class GroovyBase extends CarrotMojo { * @parameter default-value="" */ protected String groovyText; + + /** + * whether to log the script text during the build (The script might be long, + * or contain sensitive data) + * + * @parameter default-value="true" + */ + protected boolean logText; /** * should load all system properties from {@link System#getenv()} @@ -72,7 +80,7 @@ protected CarrotGroovyRunner newRunner() throws Exception { final MavenProject project = new MavenProjectAdaptor(mavenProps); - final CarrotGroovyRunner runner = new CarrotGroovyRunner(project); + final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, getLog()); return runner; diff --git a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyExec.java b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyExec.java index 1c0541d6..1708e9d6 100644 --- a/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyExec.java +++ b/carrot-maven-aws-plugin/src/main/java/com/carrotgarden/maven/aws/grv/GroovyExec.java @@ -59,7 +59,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (groovyText != null && groovyText.length() != 0) { - getLog().info("groovy exec text : " + singleLine(groovyText)); + getLog().info("groovy exec text : " + (logText? singleLine(groovyText): "[suppressed]")); runner.execute(groovyText); diff --git a/carrot-maven-aws-plugin/src/test/java/com/carrotgarden/maven/aws/grv/TestCarrotGroovyRunner.java b/carrot-maven-aws-plugin/src/test/java/com/carrotgarden/maven/aws/grv/TestCarrotGroovyRunner.java index 18efbeb1..83227702 100644 --- a/carrot-maven-aws-plugin/src/test/java/com/carrotgarden/maven/aws/grv/TestCarrotGroovyRunner.java +++ b/carrot-maven-aws-plugin/src/test/java/com/carrotgarden/maven/aws/grv/TestCarrotGroovyRunner.java @@ -14,8 +14,10 @@ import java.util.Properties; import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.junit.Test; +import org.mockito.Mockito; public class TestCarrotGroovyRunner { @@ -31,7 +33,7 @@ public void testFile() throws Exception { when(project.getProperties()).thenReturn(properties); - final CarrotGroovyRunner runner = new CarrotGroovyRunner(project); + final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, Mockito.mock(Log.class)); final File script = new File("./src/test/resources/script.groovy"); @@ -40,7 +42,7 @@ public void testFile() throws Exception { assertEquals(result, "result"); assertEquals(properties.get("prop-key"), "prop-value-2"); - + } @Test @@ -55,7 +57,7 @@ public void testString() throws Exception { when(project.getProperties()).thenReturn(properties); - final CarrotGroovyRunner runner = new CarrotGroovyRunner(project); + final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, Mockito.mock(Log.class)); final File file = new File("./src/test/resources/script.groovy"); diff --git a/carrot-maven-aws-plugin/src/test/resources/script.groovy b/carrot-maven-aws-plugin/src/test/resources/script.groovy index 5c8096c6..098d8d5e 100644 --- a/carrot-maven-aws-plugin/src/test/resources/script.groovy +++ b/carrot-maven-aws-plugin/src/test/resources/script.groovy @@ -22,5 +22,7 @@ def result = "result" println "result = $result" +log.info "testing the logger" + return result