Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.codehaus.gmavenplus.mojo;
 
 import java.net.URL;
Executes Groovy scripts (in the pom or external).

Author(s):
Keegan Witt
Goal:
execute
Configurator:
include-project-test-dependencies
RequiresDependencyResolution:
test
ThreadSafe:
 
 public class ExecuteMojo extends AbstractGroovyMojo {

    
The minimum version of Groovy that this mojo supports.
 
     protected static final Version MIN_GROOVY_VERSION = new Version(1, 5, 0);

    
Groovy scripts to run (in order). Can be an actual Groovy script or a URL to a Groovy script (local or remote).

Parameter:
Required:
 
     protected String[] scripts;

    
Whether to continue executing remaining scripts when a script fails.

Parameter:
default-value="false"
 
     protected boolean continueExecuting;

    
The encoding of script files.

Parameter:
default-value="${project.build.sourceEncoding}"
 
     protected String sourceEncoding;

    
Properties to make available in scripts. By default it will include
settings
a org.apache.maven.settings.Settings object of the current Maven settings
project
a org.apache.maven.project.Project object of the current Maven project
session
a org.apache.maven.execution.MavenSession object of the current Maven session
pluginArtifacts
a list of org.apache.maven.artifact.Artifact objects of this plugin's artifacts
localRepository
a org.apache.maven.artifact.repository.ArtifactRepository object of Maven's local repository
reactorProjects
a list of org.apache.maven.project.MavenProject objects currently loaded by the reactor
These can be overridden.

Parameter:
 
     protected Properties properties = new Properties();

    
Executes this mojo.

Throws:
org.apache.maven.plugin.MojoExecutionException If an unexpected problem occurs. Throwing this exception causes a "BUILD ERROR" message to be displayed
org.apache.maven.plugin.MojoFailureException If an expected problem (such as a compilation failure) occurs. Throwing this exception causes a "BUILD FAILURE" message to be displayed
    public void execute() throws MojoExecutionExceptionMojoFailureException {
        if (groovyVersionSupportsAction()) {
            logGroovyVersion("execute");
            if ( == null || . == 0) {
                getLog().info("No scripts specified for execution.  Skipping.");
                return;
            }
            try {
                // get classes we need with reflection
                Class<?> groovyShellClass = Class.forName("groovy.lang.GroovyShell");
                // create a GroovyShell to run scripts in
                Object shell = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyShellClass));
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"settings");
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"project");
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"session");
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"pluginArtifacts");
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"localRepository");
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shell"reactorProjects");
                // this is intentionally after the default properties so that the user can override if desired
                for (String key : .stringPropertyNames()) {
                    ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"setProperty"String.classObject.class), shellkey.getProperty(key));
                }
                // TODO: load configurable (compile, test, runtime, or system) dependencies onto classpath before executing so they can be used in scripts?
                // run the scripts
                int scriptNum = 1;
                for (String script : ) {
                    Closer closer = Closer.create();
                    try {
                        try {
                            URL url = new URL(script);
                            // it's a URL to a script
                            getLog().info("Fetching Groovy script from " + url.toString() + ".");
                            BufferedReader reader = closer.register(new BufferedReader(new InputStreamReader(url.openStream(), )));
                            StringBuilder scriptSource = new StringBuilder();
                            String line;
                            while ((line = reader.readLine()) != null) {
                                scriptSource.append(line).append("\n");
                            }
                            if (!scriptSource.toString().isEmpty()) {
                                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"evaluate"String.class), shellscriptSource.toString());
                            }
                        } catch (MalformedURLException e) {
                            // it's not a URL to a script, treat as a script body
                            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"evaluate"String.class), shellscript);
                        } catch (Throwable throwable) {
                            throw closer.rethrow(throwable);
                        } finally {
                            closer.close();
                        }
                    } catch (IOException ioe) {
                        if () {
                            getLog().error("An Exception occurred while executing script " + scriptNum + ".  Continuing to execute remaining scripts."ioe);
                        } else {
                            throw new MojoExecutionException("An Exception occurred while executing script " + scriptNum + "."ioe);
                        }
                    }
                    scriptNum++;
                }
            } catch (ClassNotFoundException e) {
                throw new MojoExecutionException("Unable to get a Groovy class from classpath.  Do you have Groovy as a compile dependency in your project?"e);
            } catch (InvocationTargetException e) {
                throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath."e);
            } catch (InstantiationException e) {
                throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath."e);
            } catch (IllegalAccessException e) {
                throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath."e);
            }
        } else {
            getLog().error("Your Groovy version (" + getGroovyVersion() + ") script execution.  The minimum version of Groovy required is " +  + ".  Skipping script execution.");
        }
    }

    
Determines whether this mojo can be run with the version of Groovy supplied. Must be >= 1.8.2 because not all the classes needed were available and functioning correctly in previous versions.

Returns:
true only if the version of Groovy supports this mojo.
    protected boolean groovyVersionSupportsAction() {
        return getGroovyVersion() != null && getGroovyVersion().compareTo() >= 0;
    }
New to GrepCode? Check out our FAQ X