You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/10/12 08:24:08 UTC

svn commit: r463140 - in /geronimo/genesis/trunk/plugins/script-maven-plugin/src: main/java/org/apache/geronimo/genesis/plugins/script/ main/resources/META-INF/plexus/ site/apt/

Author: jdillon
Date: Wed Oct 11 23:24:07 2006
New Revision: 463140

URL: http://svn.apache.org/viewvc?view=rev&rev=463140
Log:
Split up doExecute() into methods
Starting to add support for a rich groovy object configuration mechanism

Modified:
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/CodeSource.java
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/resources/META-INF/plexus/components.xml
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/CodeSource.java
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/CodeSource.java?view=diff&rev=463140&r1=463139&r2=463140
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/CodeSource.java (original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/CodeSource.java Wed Oct 11 23:24:07 2006
@@ -25,7 +25,7 @@
 import org.apache.maven.plugin.MojoExecutionException;
 
 /**
- * COnfiguration for a scripts code source.
+ * Configuration for a scripts code source.
  *
  * @version $Rev$ $Date$
  */

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java?view=diff&rev=463140&r1=463139&r2=463140
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java (original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java Wed Oct 11 23:24:07 2006
@@ -34,6 +34,9 @@
 import groovy.lang.GroovyClassLoader;
 import groovy.lang.GroovyObject;
 import groovy.lang.GroovyResourceLoader;
+import groovy.lang.MetaClass;
+import groovy.lang.MetaMethod;
+
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -85,7 +88,7 @@
     /**
      * @parameter
      */
-    private DelayedConfiguration custom = null;
+    private DelayedConfiguration custom;
 
     //
     // Maven components
@@ -124,60 +127,57 @@
     protected void doExecute() throws Exception {
         boolean debug = log.isDebugEnabled();
 
-        source.validate();
+        Class type = loadGroovyClass(source);
+        GroovyObject obj = (GroovyObject)type.newInstance();
 
-        ClassLoader parent = getClass().getClassLoader();
-        URL[] urls = getClasspath();
-        URLClassLoader cl = new URLClassLoader(urls, parent);
+        if (custom != null) {
+            log.info("Applying delayed configuration: " + custom);
 
-        // Validate and dump the scriptpath
-        if (scriptpath != null) {
-            log.debug("Scriptpath:");
-            for (int i=0; i < scriptpath.length; i++) {
-                if (scriptpath[i] == null) {
-                    throw new MojoExecutionException("Null element found in scriptpath at index: " + i);
-                }
+            MetaClass meta = obj.getMetaClass();
+            MetaMethod method = meta.pickMethod(obj, "configure", new Object[] { custom });
+            log.info("Using configure method: " + method);
 
-                if (debug) {
-                    log.debug("    " + scriptpath[i]);
+            method.invoke(obj, new Object[] { custom });
+        }
+
+        // Expose logging
+        obj.setProperty("log", log);
+
+        // Create a delegate to allow getProperites() to be fully resolved
+        MavenProject delegate = new MavenProject(project) {
+            private Properties resolvedProperties;
+
+            public Properties getProperties() {
+                if (resolvedProperties == null) {
+                    resolvedProperties = resolveProperties(project.getProperties());
                 }
+                return resolvedProperties;
             }
+        };
+
+        obj.setProperty("project", delegate);
+        obj.setProperty("pom", delegate);
+
+        // Execute the script
+        if (debug) {
+            log.debug("Invoking run() on: " + obj);
         }
+        obj.invokeMethod("run", new Object[0]);
+    }
 
-        //
-        // TODO: Investigate using GroovyScript instead of this...
-        //
+    private Class loadGroovyClass(final CodeSource source) throws Exception {
+        assert source != null;
 
-        GroovyClassLoader loader = new GroovyClassLoader(cl);
-        loader.setResourceLoader(new GroovyResourceLoader()
-        {
-            // Allow peer scripts to be loaded
-            public URL loadGroovySource(final String classname) throws MalformedURLException {
-                String resource = classname.replace('.', '/');
-                if (!resource.startsWith("/")) {
-                    resource = "/" + resource;
-                }
-                resource = resource + ".groovy";
+        boolean debug = log.isDebugEnabled();
 
-                if (scriptpath != null) {
-                    for (int i=0; i<scriptpath.length; i++) {
-                        assert scriptpath[i] != null;
-
-                        File file = new File(scriptpath[i], resource);
-                        if (file.exists()) {
-                            return file.toURL();
-                        }
-                    }
-                }
+        // Make sure the codesource us valid first
+        source.validate();
+
+        Class type;
+        GroovyClassLoader loader = createGroovyClassLoader();
 
-                return null;
-            }
-        });
-        
-        Class groovyClass;
-        
         if (source.getBody() != null) {
-            groovyClass = loader.parseClass(source.getBody());
+            type = loader.parseClass(source.getBody());
         }
         else {
             URL url;
@@ -193,45 +193,86 @@
 
             String fileName = new File(url.getFile()).getName();
             InputStream input = url.openConnection().getInputStream();
-            groovyClass = loader.parseClass(input, fileName);
-            input.close();
+            try {
+                type = loader.parseClass(input, fileName);
+            }
+            finally {
+                input.close();
+            }
         }
-        
-        GroovyObject groovyObject = (GroovyObject)groovyClass.newInstance();
 
-        if (custom != null) {
-            //
-            // TODO: Perform custom configuration processing
-            //
-            log.info("Applying delayed configuration: " + custom);
-        }
+        return type;
+    }
 
-        // Put int a helper to the script object
-        groovyObject.setProperty("script", groovyObject);
+    private GroovyClassLoader createGroovyClassLoader() throws Exception {
+        boolean debug = log.isDebugEnabled();
 
-        // Expose logging
-        groovyObject.setProperty("log", log);
+        ClassLoader parent = getClass().getClassLoader();
+        URL[] urls = getClasspath();
+        URLClassLoader cl = new URLClassLoader(urls, parent);
 
-        // Create a delegate to allow getProperites() to be fully resolved
-        MavenProject delegate = new MavenProject(project) {
-            public Properties resolvedProperties;
+        // Validate and dump the scriptpath
+        if (scriptpath != null) {
+            log.debug("Scriptpath:");
+            for (int i=0; i < scriptpath.length; i++) {
+                if (scriptpath[i] == null) {
+                    throw new MojoExecutionException("Null element found in scriptpath at index: " + i);
+                }
 
-            public Properties getProperties() {
-                if (resolvedProperties == null) {
-                    resolvedProperties = resolveProperties(project.getProperties());
+                if (debug) {
+                    log.debug("    " + scriptpath[i]);
                 }
-                return resolvedProperties;
             }
-        };
+        }
 
-        groovyObject.setProperty("project", delegate);
-        groovyObject.setProperty("pom", delegate);
+        //
+        // TODO: Investigate using GroovyScript instead of this...
+        //
 
-        // Execute the script
-        if (debug) {
-            log.debug("Invoking run() on: " + groovyObject);
+        GroovyClassLoader loader = new GroovyClassLoader(cl);
+
+        // Allow peer scripts to be loaded
+        loader.setResourceLoader(new GroovyResourceLoader() {
+            public URL loadGroovySource(final String classname) throws MalformedURLException {
+                return resolveGroovyScript(classname);
+            }
+        });
+
+        return loader;
+    }
+
+    private URL resolveGroovyScript(final String classname) throws MalformedURLException {
+        assert classname != null;
+
+        String resource = classname.replace('.', '/');
+        if (!resource.startsWith("/")) {
+            resource = "/" + resource;
+        }
+        resource = resource + ".groovy";
+
+        // First check the scriptpath
+        if (scriptpath != null) {
+            for (int i=0; i<scriptpath.length; i++) {
+                assert scriptpath[i] != null;
+
+                File file = new File(scriptpath[i], resource);
+                if (file.exists()) {
+                    return file.toURL();
+                }
+            }
+        }
+
+        // Then check for a class defined in a file next to the main script file
+        File script = source.getFile();
+        if (script != null) {
+            File file = new File(script.getParentFile(), resource);
+            if (file.exists()) {
+                return file.toURL();
+            }
         }
-        groovyObject.invokeMethod("run", new Object[0]);
+
+        // Class was not found
+        return null;
     }
 
     private URL[] getClasspath() throws DependencyResolutionRequiredException, MalformedURLException, MojoExecutionException {

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/resources/META-INF/plexus/components.xml?view=diff&rev=463140&r1=463139&r2=463140
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/resources/META-INF/plexus/components.xml (original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/resources/META-INF/plexus/components.xml Wed Oct 11 23:24:07 2006
@@ -8,7 +8,7 @@
     "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
+        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

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt?view=diff&rev=463140&r1=463139&r2=463140
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt (original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt Wed Oct 11 23:24:07 2006
@@ -35,8 +35,6 @@
 
   * <<<pom>>> - Alias for <<<project>>>
 
-  * <<<script>>> - The compiled script object
-
   * <<<log>>> - The maven plugin logger
 
 * Execute an Inline Groovy Script