001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.plugin.tools;
018    
019    import org.apache.maven.artifact.DependencyResolutionRequiredException;
020    import org.codehaus.gmaven.feature.Component;
021    import org.codehaus.gmaven.feature.Configuration;
022    import org.codehaus.gmaven.plugin.ComponentMojoSupport;
023    import org.codehaus.gmaven.runtime.Shell;
024    import org.codehaus.gmaven.runtime.loader.realm.RealmManager;
025    import org.codehaus.plexus.classworlds.realm.ClassRealm;
026    
027    import java.util.List;
028    
029    /**
030     * Launches the Groovy Shell (aka. <tt>groovysh</tt>).
031     *
032     * @goal shell
033     * @requiresProject false
034     * @requiresDependencyResolution test
035     * @since 1.0-beta-2
036     * 
037     * @version $Id: ShellMojo.java 11 2009-07-16 09:25:38Z user57 $
038     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
039     */
040    public class ShellMojo
041        extends ComponentMojoSupport
042    {
043        /**
044         * Enable the <em>legacy</em> shell.
045         *
046         * @parameter expression="${legacy}" default-value="false"
047         *
048         * @noinspection UnusedDeclaration
049         */
050        private boolean legacy;
051    
052        /**
053         * Enable verbose output (aka. <tt>groovysh --verbose</tt>).
054         *
055         * @parameter expression="${verbose}" default-value="false"
056         *
057         * @noinspection UnusedDeclaration
058         */
059        private boolean verbose;
060    
061        /**
062         * Enable debug output (aka. <tt>groovysh --debug</tt>).
063         *
064         * @parameter expression="${debug}" default-value="false"
065         *
066         * @noinspection UnusedDeclaration
067         */
068        private boolean debug;
069    
070        /**
071         * Suppress superfluous output (aka. <tt>groovysh --quiet</tt>).
072         *
073         * @parameter expression="${quiet}" default-value="false"
074         *
075         * @noinspection UnusedDeclaration
076         */
077        private boolean quiet;
078    
079        /**
080         * Enable or disable use of ANSI colors (aka. <tt>groovysh --color</tt>).  Normally auto-detected.
081         *
082         * @parameter expression="${color}"
083         *
084         * @noinspection UnusedDeclaration
085         */
086        private Boolean color;
087    
088        /**
089         * Specify the terminal type to use (aka. <tt>groovysh --terminal</tt>).  Normally auto-detected.
090         * Full class name or one of "unix", "win", "windows", "false", "off" or "none" expected.
091         *
092         * @parameter expression="${terminal}"
093         *
094         * @noinspection UnusedDeclaration
095         */
096        private String terminal;
097    
098        /**
099         * Optional arguments to pass to the shell when executing.
100         *
101         * @parameter expression="${args}"
102         *
103         * @noinspection UnusedDeclaration
104         */
105        private String args;
106    
107        /**
108         * @component
109         *
110         * @noinspection UnusedDeclaration
111         */
112        private RealmManager realmManager;
113    
114        public ShellMojo() {
115            super(Shell.KEY);
116        }
117    
118        protected List getProjectClasspathElements() throws DependencyResolutionRequiredException {
119            return project.getTestClasspathElements();
120        }
121        
122        protected void configure(final Configuration config) throws Exception {
123            assert config != null;
124    
125            config.set(Shell.Keys.LEGACY, legacy);
126    
127            if (!legacy) {
128                config.set(Shell.Keys.VERBOSE, verbose);
129                config.set(Shell.Keys.DEBUG, debug);
130                config.set(Shell.Keys.QUIET, quiet);
131                config.set(Shell.Keys.COLOR, color);
132                config.set(Shell.Keys.TERMINAL, terminal);
133                config.set(Shell.Keys.ARGS, args);
134            }
135        }
136    
137        protected void process(final Component component) throws Exception {
138            assert component != null;
139    
140            Shell shell = (Shell) component;
141    
142            ClassRealm realm = realmManager.createComponentRealm(provider(), createClassPath());
143    
144            shell.execute(realm);
145    
146            realmManager.releaseComponentRealm(realm);
147        }
148    }