geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r433508 - in /geronimo/genesis/trunk/plugins: ./ plugin-support/src/main/java/org/apache/geronimo/plugin/ script-maven-plugin/ script-maven-plugin/src/ script-maven-plugin/src/main/ script-maven-plugin/src/main/java/ script-maven-plugin/src...
Date Tue, 22 Aug 2006 04:46:20 GMT
Author: jdillon
Date: Mon Aug 21 21:46:19 2006
New Revision: 433508

URL: http://svn.apache.org/viewvc?rev=433508&view=rev
Log:
Add a custom plugin to execute scripts; currently sports a groovy goal with inline (or remote)
code def

Added:
    geronimo/genesis/trunk/plugins/script-maven-plugin/
    geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml   (with props)
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
  (with props)
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml   (with props)
Modified:
    geronimo/genesis/trunk/plugins/plugin-support/src/main/java/org/apache/geronimo/plugin/MojoSupport.java
    geronimo/genesis/trunk/plugins/pom.xml

Modified: geronimo/genesis/trunk/plugins/plugin-support/src/main/java/org/apache/geronimo/plugin/MojoSupport.java
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/plugin-support/src/main/java/org/apache/geronimo/plugin/MojoSupport.java?rev=433508&r1=433507&r2=433508&view=diff
==============================================================================
--- geronimo/genesis/trunk/plugins/plugin-support/src/main/java/org/apache/geronimo/plugin/MojoSupport.java
(original)
+++ geronimo/genesis/trunk/plugins/plugin-support/src/main/java/org/apache/geronimo/plugin/MojoSupport.java
Mon Aug 21 21:46:19 2006
@@ -16,10 +16,20 @@
 
 package org.apache.geronimo.plugin;
 
+import java.util.List;
+
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.model.Dependency;
 
 /**
  * Support for Mojo implementations.
@@ -61,5 +71,119 @@
 
     protected void doExecute() throws Exception {
         // Sub-class should override
+    }
+
+    protected MavenProject getProject() {
+        throw new Error("Sub-class must override to provide access");
+    }
+
+    protected ArtifactFactory getArtifactFactory() {
+        throw new Error("Sub-class must override to provide access");
+    }
+
+    protected ArtifactResolver getArtifactResolver() {
+        throw new Error("Sub-class must override to provide access");
+    }
+
+    protected ArtifactRepository getArtifactRepository() {
+        throw new Error("Sub-class must override to provide access");
+    }
+
+    /**
+     * Create a new artifact. If no version is specified, it will be retrieved from the dependency
+     * list or from the DependencyManagement section of the pom.
+     */
+    protected Artifact createArtifact(final ArtifactItem item) throws MojoExecutionException
{
+        Artifact artifact;
+
+        if (item.getVersion() == null) {
+            fillMissingArtifactVersion(item);
+
+            if (item.getVersion() == null) {
+                throw new MojoExecutionException("Unable to find artifact version of " +
item.getGroupId()
+                    + ":" + item.getArtifactId() + " in either dependency list or in project's
dependency management.");
+            }
+
+        }
+
+        String classifier = item.getClassifier();
+        if (classifier == null || classifier.equals("")) {
+            artifact = getArtifactFactory().createArtifact(
+                    item.getGroupId(),
+                    item.getArtifactId(),
+                    item.getVersion(),
+                    Artifact.SCOPE_PROVIDED,
+                    item.getType());
+        }
+        else {
+            artifact = getArtifactFactory().createArtifactWithClassifier(
+                    item.getGroupId(),
+                    item.getArtifactId(),
+                    item.getVersion(),
+                    item.getType(),
+                    item.getClassifier());
+        }
+
+        return artifact;
+    }
+
+    /**
+     * Resolves the Artifact from the remote repository if nessessary. If no version is specified,
it will
+     * be retrieved from the dependency list or from the DependencyManagement section of
the pom.
+     */
+    protected Artifact getArtifact(final ArtifactItem item) throws MojoExecutionException
{
+        Artifact artifact = createArtifact(item);
+
+        try {
+            getArtifactResolver().resolve(artifact, getProject().getRemoteArtifactRepositories(),
getArtifactRepository());
+        }
+        catch (ArtifactResolutionException e) {
+            throw new MojoExecutionException("Unable to resolve artifact.", e);
+        }
+        catch (ArtifactNotFoundException e) {
+            throw new MojoExecutionException("Unable to find artifact.", e);
+        }
+
+        return artifact;
+    }
+
+    /**
+     * Tries to find missing version from dependancy list and dependency management.
+     * If found, the artifact is updated with the correct version.
+     */
+    private void fillMissingArtifactVersion(final ArtifactItem item) {
+        log.debug("Attempting to find missing version in " + item.getGroupId() + ":" + item.getArtifactId());
+
+        List list = getProject().getDependencies();
+
+        for (int i = 0; i < list.size(); ++i) {
+            Dependency dependency = (Dependency) list.get(i);
+
+            if (dependency.getGroupId().equals(item.getGroupId())
+                && dependency.getArtifactId().equals(item.getArtifactId())
+                && dependency.getType().equals(item.getType()))
+            {
+                log.debug("Found missing version: " + dependency.getVersion() + " in dependency
list.");
+
+                item.setVersion(dependency.getVersion());
+
+                return;
+            }
+        }
+
+        list = getProject().getDependencyManagement().getDependencies();
+
+        for (int i = 0; i < list.size(); i++) {
+            Dependency dependency = (Dependency) list.get(i);
+
+            if (dependency.getGroupId().equals(item.getGroupId())
+                && dependency.getArtifactId().equals(item.getArtifactId())
+                && dependency.getType().equals(item.getType()))
+            {
+                log.debug("Found missing version: " + dependency.getVersion() + " in dependency
management list");
+
+                item.setVersion(dependency.getVersion());
+            }
+        }
     }
 }

Modified: geronimo/genesis/trunk/plugins/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/pom.xml?rev=433508&r1=433507&r2=433508&view=diff
==============================================================================
--- geronimo/genesis/trunk/plugins/pom.xml (original)
+++ geronimo/genesis/trunk/plugins/pom.xml Mon Aug 21 21:46:19 2006
@@ -100,6 +100,7 @@
     <modules>
         <module>plugin-support</module>
         <module>tools-maven-plugin</module>
+        <module>script-maven-plugin</module>
     </modules>
     
     <reporting>

Added: geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml?rev=433508&view=auto
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml (added)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml Mon Aug 21 21:46:19 2006
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Copyright 2006 The Apache Software Foundation
+    
+    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.
+-->
+
+<!-- $Rev$ $Date$ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.geronimo.genesis.plugins</groupId>
+        <artifactId>plugins</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>script-maven-plugin</artifactId>
+    <name>Genesis Plugins :: Script</name>
+    <packaging>maven-plugin</packaging>
+    
+    <dependencies>
+        
+        <dependency>
+            <groupId>org.apache.geronimo.genesis.plugins</groupId>
+            <artifactId>plugin-support</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
+        
+        <dependency>
+            <groupId>groovy</groupId>
+            <artifactId>groovy-all-1.0-jsr</artifactId>
+            <version>06</version>
+        </dependency>
+
+        <!--
+        NOTE: Included for AntBuilder support.  Needs to be in the same classloader as groovy-all-1.0-jsr
+        -->
+        <dependency>
+            <groupId>ant</groupId>
+            <artifactId>ant</artifactId>
+            <version>1.6.5</version>
+        </dependency>
+
+    </dependencies>
+    
+</project>
+

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: 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?rev=433508&view=auto
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
(added)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
Mon Aug 21 21:46:19 2006
@@ -0,0 +1,180 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  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.apache.geronimo.genesis.plugins.tools;
+
+import org.apache.geronimo.plugin.MojoSupport;
+import org.apache.geronimo.plugin.ArtifactItem;
+
+import java.io.File;
+import java.io.InputStream;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyObject;
+
+/**
+ * Executes a <a href="http://groovy.codehaus.org">Groovy</a> script.
+ *
+ * @goal groovy
+ * @requiresDependencyResolution
+ *
+ * @version $Rev$ $Date$
+ */
+public class GroovyMojo
+    extends MojoSupport
+{
+    /**
+     * The project to create a build for.
+     *
+     * @parameter expression="${project}"
+     * @required
+     */
+    private MavenProject project = null;
+    
+    /**
+     * The code code of the script to execute.
+     *
+     * @parameter
+     */
+    private String code = null;
+    
+    /**
+     * The URL to use as the code of the script to execute.
+     *
+     * @parameter
+     */
+    private URL codeUrl = null;
+
+    /**
+     * Additional artifacts to add to the scripts classpath.
+     *
+     * @parameter
+     */
+    private ArtifactItem[] classpath;
+
+    //
+    // MojoSupport Hooks
+    //
+
+    protected MavenProject getProject() {
+        return project;
+    }
+
+    /**
+     * @component
+     * @required
+     * @readonly
+     */
+    private ArtifactFactory artifactFactory = null;
+
+    protected ArtifactFactory getArtifactFactory() {
+        return artifactFactory;
+    }
+
+    /**
+     * @component
+     * @required
+     * @readonly
+     */
+    private ArtifactResolver artifactResolver = null;
+
+    protected ArtifactResolver getArtifactResolver() {
+        return artifactResolver;
+    }
+
+    /**
+     * @parameter expression="${localRepository}"
+     * @readonly
+     * @required
+     */
+    protected ArtifactRepository artifactRepository = null;
+
+    protected ArtifactRepository getArtifactRepository() {
+        return artifactRepository;
+    }
+
+    //
+    // Mojo
+    //
+    
+    protected void doExecute() throws Exception {
+        if (code == null && codeUrl == null) {
+            throw new MojoExecutionException("Need to specify code or codeUrl");
+        }
+        else if (code != null && codeUrl != null) {
+            throw new MojoExecutionException("Can only specify code or codeUrl, not both");
+        }
+        
+        ClassLoader parent = getClass().getClassLoader();
+        List urls = new ArrayList();
+
+        // Add the plugins dependencies
+        List classpathFiles = project.getCompileClasspathElements();
+        for (int i = 0; i < classpathFiles.size(); ++i) {
+            urls.add(new File((String)classpathFiles.get(i)).toURL());
+        }
+
+        // Add custom dependencies
+        if (classpath != null) {
+            for (int i=0; i < classpath.length; i++) {
+                Artifact artifact = getArtifact(classpath[i]);
+                urls.add(artifact.getFile().toURL());
+            }
+        }
+
+        URL[] _urls = (URL[])urls.toArray(new URL[urls.size()]);
+        if (log.isDebugEnabled()) {
+            for (int i=0; i < _urls.length; i++) {
+                log.debug("URL[" + i + "]: " + _urls[i]);
+            }
+        }
+
+        URLClassLoader cl = new URLClassLoader(_urls, parent);
+        GroovyClassLoader loader = new GroovyClassLoader(cl);
+        
+        Class groovyClass;
+        
+        if (code != null) {
+            groovyClass = loader.parseClass(code);
+        }
+        else {
+            log.debug("Loading source from: " + codeUrl);
+            
+            InputStream input = codeUrl.openConnection().getInputStream();
+            groovyClass = loader.parseClass(input);
+            input.close();
+        }
+        
+        GroovyObject groovyObject = (GroovyObject)groovyClass.newInstance();
+        
+        // Set some properties
+        groovyObject.setProperty("project", project);
+        
+        groovyObject.invokeMethod("run", new Object[0]);
+    }
+}

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 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?rev=433508&view=auto
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt (added)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/apt/usage.apt Mon Aug 21 21:46:19
2006
@@ -0,0 +1,47 @@
+ ------
+ Genesis Script Plugin for Maven 2
+ ------
+ ???
+ ------
+ ???
+
+Basic Usage
+
+* Execute a Groovy Script
+
+ It is easy to execute a Groocy script inline
+ 
+ NOTE: This plugin includes the Ant runtime, so you can use the AntBuilder.
+
++----------+
+<plugin>
+    <groupId>org.apache.geronimo.genesis.plugins</groupId>
+    <artifactId>script-maven-plugin</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+    <executions>
+        <execution>
+            <phase>generate-resources</phase>
+            <goals>
+                <goal>groovy</goal>
+            </goals>
+            <configuration>
+                <code>
+                    if (project.packaging != "pom") {
+                        def ant = new AntBuilder()
+                        
+                        def dir = "${project.basedir}/target/classes/META-INF"
+                        ant.mkdir(dir: dir)
+                        ant.copy(todir: dir) {
+                            fileset(dir: "${project.basedir}") {
+                                include(name: "LICENSE.txt")
+                                include(name: "NOTICE.txt")
+                            }
+                        }
+                    }
+                </code>
+            </configuration>
+        </execution>
+    </executions>
+</plugin>
++----------+
+

Added: geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml?rev=433508&view=auto
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml (added)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml Mon Aug 21 21:46:19
2006
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Copyright 2006 The Apache Software Foundation
+
+    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.
+-->
+
+<!-- $Id$ -->
+
+<project name="${project.name}">
+    
+    <body>
+        
+        ${parentProject}
+        
+        ${modules}
+        
+        <menu name="${project.name}">
+            <item name="Overview" href="index.html"/>
+            <item name="Usage" href="usage.html"/>
+            <item name="Configuration" href="plugin-info.html"/>
+        </menu>
+        
+        ${reports}
+        
+    </body>
+
+</project>
+
+

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/site/site.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message