Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 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;
 
 
Launches a Groovy shell bound to the current project. Note that this mojo requires Groovy >= 1.5.0. Note that it references the plugin ClassLoader to pull in dependencies Groovy didn't include (for things like Ant for AntBuilder, Ivy for

Author(s):
Keegan Witt
Since:
1.1
Grab:
and Jansi for Groovysh).
Goal:
shell
Configurator:
include-project-test-dependencies
RequiresDependencyResolution:
test
 
 public class ShellMojo extends AbstractToolsMojo {

    
Groovy shell verbosity level. Should be one of:
  • QUIET
  • INFO
  • DEBUG
  • VERBOSE

Parameter:
default-value="QUIET"
 
     protected String verbosity;

    
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 invocation failure) occurs. Throwing this exception causes a "BUILD FAILURE" message to be displayed
 
     public void execute() throws MojoExecutionExceptionMojoFailureException {
          = new ClassWrangler(Thread.currentThread().getContextClassLoader(), getLog());
 
         if (groovyVersionSupportsAction()) {
             logPluginClasspath();
             if (getLog().isDebugEnabled()) {
                 try {
                     getLog().debug("Project test classpath:\n" + .getTestClasspathElements());
                 } catch (DependencyResolutionRequiredException e) {
                     getLog().warn("Unable to log project test classpath"e);
                 }
             }
 
             final SecurityManager sm = System.getSecurityManager();
             try {
                 if (!) {
                     System.setSecurityManager(new NoExitSecurityManager());
                 }
 
                 // get classes we need with reflection
                 Class shellClass = .getClass("org.codehaus.groovy.tools.shell.Groovysh");
                 Class bindingClass = .getClass("groovy.lang.Binding");
                 Class ioClass = .getClass("org.codehaus.groovy.tools.shell.IO");
                 Class verbosityClass = .getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
                 Class loggerClass = .getClass("org.codehaus.groovy.tools.shell.util.Logger");
 
                 // create shell to run
                 Object shell = setupShell(shellClassbindingClassioClassverbosityClassloggerClass);
 
                 // run the shell
                 ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(shellClass"run"String.class), shell, (Stringnull);
             } 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 or the plugin?"e);
             } catch (InvocationTargetException e) {
                 if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
                    throw new MojoExecutionException("Unable to get a JLine class from classpath.  This might be because of a JLine version mismatch.  If you are using Groovy < 2.2.0-beta-1, make sure you include JLine 1.0 as a runtime dependency in your project or the plugin."e);
                } else {
                    throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath."e);
                }
            } catch (IllegalAccessException e) {
                throw new MojoExecutionException("Unable to access 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);
            } finally {
                if (!) {
                    System.setSecurityManager(sm);
                }
            }
        } else {
            getLog().error("Your Groovy version (" + .getGroovyVersionString() + ") doesn't support running a shell.  The minimum version of Groovy required is " +  + ".  Skipping shell startup.");
        }
    }

    
Creates the Groovysh to run.

Parameters:
shellClass the Groovysh class
bindingClass the Binding class
ioClass the IO class
verbosityClass the Verbosity
loggerClass the Logger class
Returns:
the Groovysh shell to run
Throws:
java.lang.InstantiationException when a class needed for setting up a shell cannot be instantiated
java.lang.IllegalAccessException when a method needed for setting up a shell cannot be accessed
java.lang.reflect.InvocationTargetException when a reflection invocation needed for setting up a shell cannot be completed
    protected Object setupShell(final Class shellClassfinal Class bindingClassfinal Class ioClassfinal Class verbosityClassfinal Class loggerClassthrows InvocationTargetExceptionIllegalAccessExceptionInstantiationException {
        Object binding = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(bindingClass));
        initializeProperties();
        if () {
            for (Object k : .keySet()) {
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(bindingClass"setVariable"String.classObject.class), bindingk.get(k));
            }
        } else {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(bindingClass"setVariable"String.classObject.class), binding"properties");
        }
        Object io = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(ioClass));
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(ioClass"setVerbosity"verbosityClass), io, ReflectionUtils.invokeStaticMethod(ReflectionUtils.findMethod(verbosityClass"forName"String.class), ));
        ReflectionUtils.findField(loggerClass"io"ioClass).set(nullio);
        return ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(shellClassClassLoader.classbindingClassioClass), Thread.currentThread().getContextClassLoader(), bindingio);
    }
New to GrepCode? Check out our FAQ X