PR: MNG-164

Add Beanshell mojo support

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@170608 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-05-17 15:11:41 +00:00
parent 21c7962649
commit 0253ccc305
21 changed files with 484 additions and 11 deletions

View File

@ -64,6 +64,9 @@ it0018: Ensure that managed dependencies for dependency POMs are calculated
redownloaded.
it0019: Test that a version is managed by pluginManagement in the super POM
it0020: Test beanshell mojo support.
-------------------------------------------------------------------------------
- generated sources

View File

@ -18,3 +18,4 @@ it0016
it0017
it0018
it0019
it0020

View File

@ -0,0 +1 @@
target/out.txt

View File

@ -0,0 +1,2 @@
install
it0020:it0020

View File

@ -0,0 +1,24 @@
<model>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-it0020-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-script-beanshell</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/scripts</directory>
<includes>
<include>**/*.bsh</include>
</includes>
</resource>
</resources>
</build>
</model>

View File

@ -0,0 +1 @@
rm target/out.txt

View File

@ -0,0 +1,28 @@
/**
* Beanshell mojo integration test.
* @goal it0020
*/
import org.apache.maven.plugin.Mojo;
import org.apache.maven.script.beanshell.BeanshellMojoAdapter;
execute()
{
logger.info( "Executing it0020..." );
print( "info level?" );
org.codehaus.plexus.util.FileUtils.fileWrite( outDir.getAbsolutePath() + "/out.txt", "This is a Beanshell test" );
}
/**
* Output directory for files.
*
* @parameter expression="${project.build.directory}" type="java.io.File"
* @required
*/
setOutDir( file )
{
outDir = file;
}
return new BeanshellMojoAdapter( (Mojo) this, this.interpreter );

View File

@ -69,6 +69,13 @@
<artifactId>plexus-i18n</artifactId>
<version>1.0-beta-3</version>
</dependency>
<!-- TODO: only here for classloader issues. It used to work when the script classes were inside the component factory... -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-script-beanshell</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-script-marmalade</artifactId>

View File

@ -536,6 +536,7 @@ public class DefaultPluginManager
{
String configuratorId = mojoDescriptor.getComponentConfigurator();
// TODO: should this be known to the component factory instead? And if so, should configuration be part of lookup?
if ( StringUtils.isNotEmpty( configuratorId ) )
{
configurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE, configuratorId );
@ -670,7 +671,7 @@ public class DefaultPluginManager
" for parameter: \'" + key + "\'" );
// TODO: remove. If there is a default value, required should have been removed by the descriptor generator
if ( value == null && !"map-oriented".equals( goal.getComponentConfigurator() ) )
if ( value == null && goal.getComponentConfigurator() == null )
{
Object defaultValue;
try
@ -791,6 +792,7 @@ public class DefaultPluginManager
"maven-reporting-api", "doxia-core",
"wagon-provider-api", "classworlds", "maven-plugin",
"plexus-marmalade-factory", "maven-script-marmalade",
"maven-script-beanshell", "plexus-bsh-factory", "bsh",
"marmalade-core"} );
}

View File

@ -9,13 +9,6 @@
<component-discoverer implementation="org.apache.maven.plugin.MavenPluginDiscoverer"/>
</component-discoverers>
</component-discoverer-manager>
<!-- component-factory-manager implementation="org.codehaus.plexus.component.factory.DefaultComponentFactoryManager">
<component-factories>
<component-factory implementation="org.codehaus.plexus.component.factory.marmalade.MarmaladeClasspathComponentFactory">
<id>marmalade</id>
</component-factory>
</component-factories>
</component-factory-manager -->
<components>
<component>
<role>org.apache.maven.plugin.PluginManager</role>

View File

@ -37,9 +37,11 @@ public class MBoot
{
String[] builds = new String[]{"maven-model", "maven-settings", "maven-monitor", "maven-plugin-api",
"maven-plugin-descriptor", "maven-artifact", "maven-script/maven-script-marmalade",
"maven-project", "maven-reporting/maven-reporting-api", "maven-core",
"maven-archiver", "maven-plugin-tools/maven-plugin-tools-api",
"maven-script/maven-script-beanshell", "maven-project",
"maven-reporting/maven-reporting-api", "maven-core", "maven-archiver",
"maven-plugin-tools/maven-plugin-tools-api",
"maven-plugin-tools/maven-plugin-tools-java",
"maven-plugin-tools/maven-plugin-tools-beanshell",
"maven-plugin-tools/maven-plugin-tools-pluggy",
"maven-plugin-tools/maven-plugin-tools-marmalade", "maven-core-it-verifier"};

View File

@ -0,0 +1,27 @@
<model>
<parent>
<artifactId>maven-plugin-tools</artifactId>
<groupId>org.apache.maven</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-plugin-tools-beanshell</artifactId>
<name>Maven Plugin Tools for Beanshell</name>
<dependencies>
<dependency>
<groupId>bsh</groupId>
<artifactId>bsh</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-descriptor</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</model>

View File

@ -0,0 +1,109 @@
package org.apache.maven.tools.plugin.extractor.beanshell;
/*
* Copyright 2001-2005 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.
*/
import bsh.EvalError;
import bsh.Interpreter;
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @todo share constants
* @todo add example usage tag that can be shown in the doco
* @todo need to add validation directives so that systems embedding maven2 can
* get validation directives to help users in IDEs.
*/
public class BeanshellMojoDescriptorExtractor
extends AbstractLogEnabled
implements MojoDescriptorExtractor
{
private MojoDescriptor createMojoDescriptor( File basedir, String resource, PluginDescriptor pluginDescriptor )
throws InvalidPluginDescriptorException
{
MojoDescriptor mojoDescriptor = new MojoDescriptor();
mojoDescriptor.setPluginDescriptor( pluginDescriptor );
mojoDescriptor.setLanguage( "bsh" );
mojoDescriptor.setComponentConfigurator( "bsh" );
mojoDescriptor.setImplementation( resource );
Interpreter interpreter = new Interpreter();
try
{
interpreter.set( "file", new File( basedir, resource ) );
interpreter.set( "mojoDescriptor", mojoDescriptor );
interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ) ) );
}
catch ( EvalError evalError )
{
throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
}
return mojoDescriptor;
}
public List execute( MavenProject project, PluginDescriptor pluginDescriptor )
throws InvalidPluginDescriptorException
{
List descriptors = new ArrayList();
for ( Iterator i = project.getScriptSourceRoots().iterator(); i.hasNext(); )
{
try
{
File basedir = new File( (String) i.next() );
if ( basedir.exists() )
{
List files = FileUtils.getFiles( basedir, "**/*.bsh", null, false );
for ( Iterator j = files.iterator(); j.hasNext(); )
{
File resource = (File) j.next();
String resourcePath = "/" + resource.getPath().replace( '\\', '/' );
MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, resourcePath, pluginDescriptor );
descriptors.add( mojoDescriptor );
}
}
}
catch ( IOException e )
{
throw new InvalidPluginDescriptorException( "Unable to locate files to process", e );
}
}
return descriptors;
}
}

View File

@ -0,0 +1,17 @@
<component-set>
<components>
<!--
|
| BeanshellMojoDescriptorExtractor, a MojoDescriptor extractor to read
| descriptors from java sources.
|
-->
<component>
<role>org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor</role>
<role-hint>bsh</role-hint>
<implementation>org.apache.maven.tools.plugin.extractor.beanshell.BeanshellMojoDescriptorExtractor</implementation>
</component>
</components>
</component-set>

View File

@ -0,0 +1,152 @@
import bsh.*;
import java.util.regex.Pattern;
import java.io.FileReader;
import org.codehaus.plexus.util.StringUtils;
import org.apache.maven.plugin.descriptor.Parameter;
// only on start of line
this.tag = "\\r?\\n\\s*\\*?\\s*@(\\w+)";
// zero width lookahead to next tag or end of comment
this.tagOrEndComment = "(?=" + tag + "|\\*/)";
this.commentTextPattern = Pattern.compile( "(?s)/\\*\\*(.*?)" + tagOrEndComment );
this.tagsPattern = Pattern.compile( "(?s)" + tag + "\\s*(.*?)" + tagOrEndComment );
this.descriptionPattern = Pattern.compile( "(?s)\\r?\\n\\s*\\*" );
this.typePattern = Pattern.compile( "type\\s*=\\s*\"(.*?)\"" );
this.expressionPattern = Pattern.compile( "expression\\s*=\\s*\"(.*?)\"" );
setAccessibility( true );
createParameter( text, method )
{
if ( method.startsWith( "set" ) )
{
this.name = StringUtils.uncapitalise( method.substring( 3 ) );
this.desc = getDescription( text );
this.tags = getTags( text );
this.parameter = new Parameter();
parameter.setName( name );
parameter.setDescription( desc );
parameter.setRequired( tags.containsKey( "required" ) );
parameter.setEditable( !tags.containsKey( "readonly" ) );
this.deprecation = tags.get( "deprecated" );
if ( deprecation != null )
{
parameter.setDeprecated( deprecation );
}
this.alias = tags.get( "alias" );
if ( alias != null )
{
parameter.setAlias( alias );
}
this.value = tags.get( "parameter" );
if ( value != null )
{
m = typePattern.matcher( value );
if ( m.find() )
{
parameter.setType( m.group( 1 ) );
}
else
{
parameter.setType( "java.lang.Object" );
}
m = expressionPattern.matcher( value );
if ( m.find() )
{
parameter.setExpression( m.group( 1 ) );
}
}
return parameter;
}
return null;
}
getTags( text )
{
this.matcher = tagsPattern.matcher( text );
this.tags = new HashMap();
while ( matcher.find() )
{
this.tagname = matcher.group( 1 );
this.tagvalue = matcher.group( 2 );
tags.put( tagname, tagvalue.trim() );
}
return tags;
}
getDescription( text )
{
this.matcher = commentTextPattern.matcher( text );
if ( matcher.find() )
{
this.input = matcher.group( 1 );
return descriptionPattern.matcher( input ).replaceAll( "" ).trim();
}
else
{
return "";
}
}
extract( file, mojoDescriptor )
{
this.parser = new Parser( new FileReader( file ) );
parser.setRetainComments( true );
this.lastNode = null;
this.firstComment = null;
while ( !parser.Line() )
{
this.node = parser.popNode();
if ( node instanceof BSHFormalComment && firstComment == null )
{
firstComment = node;
}
if ( node instanceof BSHMethodDeclaration )
{
if ( lastNode instanceof BSHFormalComment )
{
this.text = lastNode.text;
this.method = node.name;
this.parameter = createParameter( text, method );
if ( parameter != null )
{
mojoDescriptor.addParameter( parameter );
}
if ( firstComment == lastNode )
{
firstComment = null;
}
}
}
lastNode = node;
}
if ( firstComment != null )
{
this.text = firstComment.text;
mojoDescriptor.setDescription( getDescription( text ) );
this.tags = getTags( text );
mojoDescriptor.setGoal( tags.get( "goal" ) );
mojoDescriptor.setPhase( tags.get( "phase" ) );
mojoDescriptor.setExecutePhase( tags.get( "executePhase" ) );
this.value = tags.get( "requiresDependencyResolution" );
// TODO: share with java extractor
if ( value == null || value.length() == 0 )
{
value = "runtime";
}
mojoDescriptor.setDependencyResolutionRequired( value );
mojoDescriptor.setProjectRequired( tags.containsKey( "requiresProject" ) );
mojoDescriptor.setOnlineRequired( tags.containsKey( "requiresOnline" ) );
}
}
extract( file, mojoDescriptor );

View File

@ -273,7 +273,7 @@ public class JavaMojoDescriptorExtractor
String alias = parameter.getNamedParameter( "alias" );
if ( StringUtils.isEmpty( alias ) )
if ( !StringUtils.isEmpty( alias ) )
{
pd.setAlias( alias );
}

View File

@ -12,6 +12,7 @@
<module>maven-plugin-tools-api</module>
<module>maven-plugin-tools-java</module>
<module>maven-plugin-tools-marmalade</module>
<module>maven-plugin-tools-beanshell</module>
<module>maven-plugin-tools-pluggy</module>
</modules>
</project>

View File

@ -16,6 +16,12 @@
<artifactId>maven-plugin-tools-api</artifactId>
<version>2.0-alpha-2</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-beanshell</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-java</artifactId>

View File

@ -0,0 +1,22 @@
<model>
<parent>
<artifactId>maven-script</artifactId>
<groupId>org.apache.maven</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-script-beanshell</artifactId>
<name>Maven Beanshell Mojo Support</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-bsh-factory</artifactId>
<version>1.0-alpha-6-SNAPSHOT</version>
</dependency>
</dependencies>
</model>

View File

@ -0,0 +1,74 @@
package org.apache.maven.script.beanshell;
/*
* Copyright 2001-2005 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.
*/
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.component.factory.bsh.BshComponent;
import bsh.Interpreter;
import bsh.This;
import bsh.UtilEvalError;
import bsh.EvalError;
import java.io.PrintStream;
/**
* Mojo adapter for a Beanshell Mojo.
*
* @todo should log be passed in, or rely on getLog() ?
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class BeanshellMojoAdapter
extends AbstractMojo
implements BshComponent
{
private Mojo mojo;
private Interpreter interpreter;
public BeanshellMojoAdapter( Mojo mojo, Interpreter interpreter )
{
this.mojo = mojo;
this.interpreter = interpreter;
}
public void execute()
throws MojoExecutionException
{
try
{
interpreter.set( "logger", getLog() );
// TODO: set out, err to a print stream that will log at info, error respectively
}
catch ( EvalError evalError )
{
throw new MojoExecutionException( "Unable to establish mojo", evalError );
}
mojo.execute();
}
public Interpreter getInterpreter()
{
return interpreter;
}
}

View File

@ -10,5 +10,6 @@
<name>Maven Script Support Root</name>
<modules>
<module>maven-script-marmalade</module>
<module>maven-script-beanshell</module>
</modules>
</project>