Initial revision

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@162918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2004-08-09 18:43:08 +00:00
parent ea1009973e
commit c4719462e4
19 changed files with 1900 additions and 0 deletions

View File

@ -0,0 +1,6 @@
*~
*.log
target
*.ipr
*.iws
*.iml

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven</groupId>
<artifactId>maven-component</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<groupId>maven</groupId>
<artifactId>maven-plugin-tools</artifactId>
<name>Maven Plugin Tools</name>
<version>2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>qdox</groupId>
<artifactId>qdox</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>modello</groupId>
<artifactId>modello</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,4 @@
maven.log
target
.classpath
.project

View File

@ -0,0 +1,296 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;
import com.thoughtworks.xstream.xml.xpp3.Xpp3Dom;
import com.thoughtworks.xstream.xml.xpp3.Xpp3DomBuilder;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
/**
* @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 abstract class AbstractGenerator
implements Generator
{
public static final String MAVEN_PLUGIN_ID = "maven.plugin.id";
public static final String MAVEN_PLUGIN_DESCRIPTION = "maven.plugin.description";
public static final String MAVEN_PLUGIN_INSTANTIATION = "maven.plugin.instantiation";
public static final String MAVEN_PLUGIN_MODE = "maven.plugin.mode";
public static final String PARAMETER = "parameter";
public static final String GOAL = "goal";
public static final String DISPATCH = "dispatch";
public static final String GOAL_DESCRIPTION = "description";
public static final String GOAL_PREREQ = "prereq";
public static final String GOAL_REQUIRES_DEPENDENCY_RESOLUTION = "requiresDependencyResolution";
public static final String GOAL_MULTI_EXECUTION_STRATEGY = "attainAlways";
protected abstract void processPluginDescriptors( MojoDescriptor[] pluginDescriptors, String destinationDirectory, Xpp3Dom pomDom )
throws Exception;
protected Xpp3Dom readModel( String pom )
throws Exception
{
return Xpp3DomBuilder.build( new FileReader( pom ) );
}
public void execute( String sourceDirectory, String destinationDirectory, String pom )
throws Exception
{
Xpp3Dom pomDom = readModel( pom );
JavaDocBuilder builder = new JavaDocBuilder();
File sourceDirectoryFile = new File( sourceDirectory );
builder.addSourceTree( sourceDirectoryFile );
JavaSource[] javaSources = builder.getSources();
List mojoDescriptors = new ArrayList();
for ( int i = 0; i < javaSources.length; i++ )
{
DocletTag tag = getJavaClass( javaSources[i] ).getTagByName( GOAL );
if ( tag != null )
{
MojoDescriptor mojoDescriptor = createMojoDescriptor( javaSources[i], pomDom );
// ----------------------------------------------------------------------
// Validate the descriptor as best we can before allowing it
// to be processed.
// ----------------------------------------------------------------------
List parameters = mojoDescriptor.getParameters();
for ( int j = 0; j < parameters.size(); j++ )
{
validateParameter( (Parameter) parameters.get( j ), j );
}
mojoDescriptors.add( mojoDescriptor );
}
}
MojoDescriptor[] mojos = (MojoDescriptor[]) mojoDescriptors.toArray( new MojoDescriptor[mojoDescriptors.size()] );
processPluginDescriptors( mojos, destinationDirectory, pomDom );
}
protected void validateParameter( Parameter parameter, int i )
throws InvalidParameterException
{
String name = parameter.getName();
if ( name == null )
{
throw new InvalidParameterException( "name", i );
}
String type = parameter.getType();
if ( type == null )
{
throw new InvalidParameterException( "type", i );
}
boolean required = parameter.isRequired();
String validator = parameter.getValidator();
if ( validator == null )
{
throw new InvalidParameterException( "validator", i );
}
String expression = parameter.getExpression();
if ( expression == null )
{
throw new InvalidParameterException( "expression", i );
}
String description = parameter.getDescription();
if ( description == null )
{
throw new InvalidParameterException( "description", i );
}
}
protected String pluginId( Xpp3Dom pomDom )
{
// ----------------------------------------------------------------------
// We will take the id from the artifactId of the POM. The artifactId is
// always of the form maven-<pluginId>-plugin so we can extract the
// pluginId from the artifactId.
// ----------------------------------------------------------------------
String artifactId = pomDom.getChild( "artifactId" ).getValue();
int firstHyphen = artifactId.indexOf( "-" );
int lastHyphen = artifactId.lastIndexOf( "-" );
String pluginId = artifactId.substring( firstHyphen + 1, lastHyphen );
return pluginId;
}
// ----------------------------------------------------------------------
// Plugin descriptor creation from @tags
// ----------------------------------------------------------------------
private MojoDescriptor createMojoDescriptor( JavaSource javaSource, Xpp3Dom pomDom )
{
MojoDescriptor mojoDescriptor = new MojoDescriptor();
JavaClass javaClass = getJavaClass( javaSource );
mojoDescriptor.setImplementation( javaClass.getFullyQualifiedName() );
DocletTag tag;
String pluginId = pluginId( pomDom );
mojoDescriptor.setId( pluginId );
tag = javaClass.getTagByName( MAVEN_PLUGIN_DESCRIPTION );
if ( tag != null )
{
mojoDescriptor.setDescription( tag.getValue() );
}
tag = javaClass.getTagByName( MAVEN_PLUGIN_INSTANTIATION );
if ( tag != null )
{
mojoDescriptor.setInstantiationStrategy( tag.getValue() );
}
tag = javaClass.getTagByName( GOAL_MULTI_EXECUTION_STRATEGY );
if ( tag != null )
{
mojoDescriptor.setExecutionStrategy( MojoDescriptor.MULTI_PASS_EXEC_STRATEGY );
}
else
{
mojoDescriptor.setExecutionStrategy( MojoDescriptor.SINGLE_PASS_EXEC_STRATEGY );
}
// ----------------------------------------------------------------------
// Goal name
// ----------------------------------------------------------------------
DocletTag goal = javaClass.getTagByName( GOAL );
if ( goal != null )
{
mojoDescriptor.setGoal( goal.getValue() );
}
// ----------------------------------------------------------------------
// Dependency resolution flag
// ----------------------------------------------------------------------
DocletTag requiresDependencyResolution = javaClass.getTagByName( GOAL_REQUIRES_DEPENDENCY_RESOLUTION );
if ( requiresDependencyResolution != null )
{
mojoDescriptor.setRequiresDependencyResolution( true );
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
DocletTag[] parameterTags = javaClass.getTagsByName( PARAMETER );
List parameters = new ArrayList();
for ( int j = 0; j < parameterTags.length; j++ )
{
DocletTag parameter = parameterTags[j];
Parameter pd = new Parameter();
pd.setName( parameter.getNamedParameter( "name" ) );
pd.setType( parameter.getNamedParameter( "type" ) );
pd.setRequired( Boolean.getBoolean( parameter.getNamedParameter( "required" ) ) );
pd.setValidator( parameter.getNamedParameter( "validator" ) );
pd.setExpression( parameter.getNamedParameter( "expression" ) );
pd.setDescription( parameter.getNamedParameter( "description" ) );
parameters.add( pd );
}
mojoDescriptor.setParameters( parameters );
// ----------------------------------------------------------------------
// Prereqs must in the form pluginId:goalName
// ----------------------------------------------------------------------
DocletTag[] goalPrereqTags = javaClass.getTagsByName( GOAL_PREREQ );
List goalPrereqs = new ArrayList();
for ( int j = 0; j < goalPrereqTags.length; j++ )
{
DocletTag goalPrereq = goalPrereqTags[j];
goalPrereqs.add( goalPrereq.getValue() );
}
mojoDescriptor.setPrereqs( goalPrereqs );
return mojoDescriptor;
}
private JavaClass getJavaClass( JavaSource javaSource )
{
return javaSource.getClasses()[0];
}
}

View File

@ -0,0 +1,186 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 com.thoughtworks.xstream.xml.xpp3.Xpp3Dom;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.codehaus.modello.generator.java.javasource.JClass;
import org.codehaus.modello.generator.java.javasource.JConstructor;
import org.codehaus.modello.generator.java.javasource.JMethod;
import org.codehaus.modello.generator.java.javasource.JParameter;
import org.codehaus.modello.generator.java.javasource.JSourceWriter;
import org.codehaus.modello.generator.java.javasource.JType;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
/**
* @todo use the descriptions in the descriptor for the javadoc pushed into
* the source code.
*/
public class BeanGenerator
extends AbstractGenerator
{
protected void processPluginDescriptors( MojoDescriptor[] pluginDescriptors, String destinationDirectory, Xpp3Dom pomDom )
throws Exception
{
for ( int i = 0; i < pluginDescriptors.length; i++ )
{
processPluginDescriptor( pluginDescriptors[i], destinationDirectory );
}
}
protected void processPluginDescriptor( MojoDescriptor pluginDescriptor, String destinationDirectory )
throws Exception
{
String implementation = pluginDescriptor.getImplementation();
String className = implementation.substring( implementation.lastIndexOf( "." ) + 1 ) + "Bean";
JClass jClass = new JClass( className );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
jClass.setSuperClass( "org.apache.maven.plugin.BeanPluginAdapter" );
jClass.addImport( "java.util.*" );
// ----------------------------------------------------------------------
// Use the same package as the plugin we are wrapping.
// ----------------------------------------------------------------------
String packageName = implementation.substring( 0, implementation.lastIndexOf( "." ) );
jClass.setPackageName( packageName );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
JConstructor constructor = new JConstructor( jClass );
constructor.getSourceCode().add( "super( new " + implementation + "() );" );
jClass.addConstructor( constructor );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
List parameters = pluginDescriptor.getParameters();
for ( int i = 0; i < parameters.size(); i++ )
{
Parameter parameter = (Parameter) parameters.get( i );
jClass.addMethod( createSetter( parameter, jClass ) );
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
String packageDirectory = replace( packageName, ".", "/", -1 );
File destination = new File( destinationDirectory, packageDirectory + "/" + className + ".java" );
if ( !destination.getParentFile().exists() )
{
destination.getParentFile().mkdirs();
}
FileWriter writer = new FileWriter( destination );
JSourceWriter sourceWriter = new JSourceWriter( writer );
jClass.print( sourceWriter );
writer.flush();
writer.close();
}
private JMethod createSetter( Parameter parameter, JClass jClass )
{
String propertyName = capitalise( parameter.getName() );
JMethod setter = new JMethod( null, "set" + propertyName );
String type = parameter.getType();
JType parameterType;
int arrayLocation = type.indexOf( "[]" );
if ( arrayLocation > 0 )
{
type = type.substring( 0, arrayLocation );
parameterType = new JClass( type ).createArray();
}
else
{
parameterType = new JClass( type );
}
setter.addParameter( new JParameter( parameterType, parameter.getName() ) );
setter.getSourceCode().add( "addParameter( " + "\"" + parameter.getName() + "\", " + parameter.getName() + " );" );
return setter;
}
protected String capitalise( String str )
{
if ( str == null || str.length() == 0 )
{
return str;
}
return new StringBuffer( str.length() )
.append( Character.toTitleCase( str.charAt( 0 ) ) )
.append( str.substring( 1 ) )
.toString();
}
protected String replace( String text, String repl, String with, int max )
{
if ( text == null || repl == null || with == null || repl.length() == 0 )
{
return text;
}
StringBuffer buf = new StringBuffer( text.length() );
int start = 0, end = 0;
while ( ( end = text.indexOf( repl, start ) ) != -1 )
{
buf.append( text.substring( start, end ) ).append( with );
start = end + repl.length();
if ( --max == 0 )
{
break;
}
}
buf.append( text.substring( start ) );
return buf.toString();
}
}

View File

@ -0,0 +1,27 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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.
*/
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public interface Generator
{
void execute( String sourceDirectory, String destinationDirectory, String pom )
throws Exception;
}

View File

@ -0,0 +1,44 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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.
*/
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class InvalidParameterException
extends Exception
{
public InvalidParameterException()
{
}
public InvalidParameterException( String element, int i )
{
super( "The " + element + " element in parameter # " + i + " is invalid. It cannot be null." );
}
public InvalidParameterException( Throwable cause )
{
super( cause );
}
public InvalidParameterException( String message, Throwable cause )
{
super( message, cause );
}
}

View File

@ -0,0 +1,66 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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.generator.jelly.JellyHarnessGenerator;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class Main
{
public static void main( String[] args )
throws Exception
{
if ( args.length != 4 )
{
System.err.println( "Usage: pluggy <mode> <source directory> <output directory> <pom>" );
System.exit( 1 );
}
String mode = args[0];
String sourceDirectory = args[1];
String outputDirectory = args[2];
String pom = args[3];
AbstractGenerator generator = null;
if ( mode.equals( "descriptor" ) )
{
generator = new PluginDescriptorGenerator();
}
else if ( mode.equals( "xdoc" ) )
{
generator = new PluginXdocGenerator();
}
else if ( mode.equals( "jelly" ) )
{
generator = new JellyHarnessGenerator();
}
else if ( mode.equals( "bean" ) )
{
generator = new BeanGenerator();
}
generator.execute( sourceDirectory, outputDirectory, pom );
}
}

View File

@ -0,0 +1,288 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 com.thoughtworks.xstream.xml.XMLWriter;
import com.thoughtworks.xstream.xml.text.PrettyPrintXMLWriter;
import com.thoughtworks.xstream.xml.xpp3.Xpp3Dom;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
/**
* @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 PluginDescriptorGenerator
extends AbstractGenerator
{
protected void processPluginDescriptors( MojoDescriptor[] pluginDescriptors, String destinationDirectory, Xpp3Dom pomDom )
throws Exception
{
File f = new File( destinationDirectory, "plugin.xml" );
if ( !f.getParentFile().exists() )
{
f.getParentFile().mkdirs();
}
FileWriter writer = new FileWriter( f );
XMLWriter w = new PrettyPrintXMLWriter( writer );
w.startElement( "plugin" );
element( w, "id", pluginId( pomDom ) );
element( w, "isolatedRealm", "true" );
w.startElement( "mojos" );
for ( int i = 0; i < pluginDescriptors.length; i++ )
{
processPluginDescriptor( pluginDescriptors[i], w, pomDom );
}
w.endElement();
writeDependencies( w, pomDom );
w.endElement();
writer.flush();
writer.close();
}
protected void processPluginDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w, Xpp3Dom pomDom )
throws Exception
{
w.startElement( "mojo" );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "id" );
w.writeText( mojoDescriptor.getId() + ":" + mojoDescriptor.getGoal() );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
if ( mojoDescriptor.requiresDependencyResolution() )
{
element( w, "requiresDependencyResolution", "true" );
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "implementation" );
w.writeText( mojoDescriptor.getImplementation() );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "instantiationStrategy" );
w.writeText( mojoDescriptor.getInstantiationStrategy() );
w.endElement();
// ----------------------------------------------------------------------
// Strategy for handling repeated reference to mojo in
// the calculated (decorated, resolved) execution stack
// ----------------------------------------------------------------------
w.startElement( "executionStrategy" );
w.writeText( mojoDescriptor.getExecutionStrategy() );
w.endElement();
// ----------------------------------------------------------------------
// Parameters
// ----------------------------------------------------------------------
List parameters = mojoDescriptor.getParameters();
w.startElement( "parameters" );
for ( int j = 0; j < parameters.size(); j++ )
{
Parameter parameter = (Parameter) parameters.get( j );
w.startElement( "parameter" );
element( w, "name", parameter.getName() );
element( w, "type", parameter.getType() );
element( w, "required", Boolean.toString( parameter.isRequired() ) );
element( w, "validator", parameter.getValidator() );
element( w, "expression", parameter.getExpression() );
element( w, "description", parameter.getDescription() );
w.endElement();
}
w.endElement();
// ----------------------------------------------------------------------
// Prereqs
// ----------------------------------------------------------------------
List prereqs = mojoDescriptor.getPrereqs();
if ( prereqs.size() > 0 )
{
w.startElement( "prereqs" );
for ( int j = 0; j < prereqs.size(); j++ )
{
element( w, "prereq", (String) prereqs.get( j ) );
}
w.endElement();
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.endElement();
}
public void writeDependencies( XMLWriter w, Xpp3Dom pomDom )
throws Exception
{
w.startElement( "dependencies" );
Xpp3Dom deps = pomDom.getChild( "dependencies" );
if ( deps != null )
{
Xpp3Dom dependencies[] = deps.getChildren( "dependency" );
for ( int i = 0; i < dependencies.length; i++ )
{
writeDependencyElement( dependencies[i], w );
}
}
/*
// ----------------------------------------------------------------------
Xpp3Dom parent = pomDom.getChild( "parent" );
if ( parent != null )
{
String groupId = parent.getChild( "groupId" ).getValue();
System.out.println( "groupId = " + groupId );
String artifactId = parent.getChild( "artifactId" ).getValue();
System.out.println( "artifactId = " + artifactId );
String version = parent.getChild( "version" ).getValue();
System.out.println( "version = " + version );
writeDependencyElement( parent, w );
}
// ----------------------------------------------------------------------
*/
w.endElement();
}
private void writeDependencyElement( Xpp3Dom dependency, XMLWriter w )
throws Exception
{
w.startElement( "dependency" );
Xpp3Dom groupId = dependency.getChild( "groupId" );
if ( groupId == null )
{
throw new Exception( "Missing dependency: 'groupId'." );
}
element( w, "groupId", groupId.getValue() );
Xpp3Dom artifactId = dependency.getChild( "artifactId" );
if ( artifactId == null )
{
throw new Exception( "Missing dependency: 'artifactId'." );
}
element( w, "artifactId", artifactId.getValue() );
Xpp3Dom type = dependency.getChild( "type" );
if ( type != null )
{
element( w, "type", type.getValue() );
}
Xpp3Dom version = dependency.getChild( "version" );
if ( version == null )
{
throw new Exception( "Missing dependency: 'version'." );
}
element( w, "version", version.getValue() );
w.endElement();
}
public void element( XMLWriter w, String name, String value )
{
w.startElement( name );
if ( value == null )
{
value = "";
}
w.writeText( value );
w.endElement();
}
}

View File

@ -0,0 +1,216 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 com.thoughtworks.xstream.xml.XMLWriter;
import com.thoughtworks.xstream.xml.xpp3.Xpp3Dom;
import com.thoughtworks.xstream.xml.text.PrettyPrintXMLWriter;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Map;
/**
* @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 PluginXdocGenerator
extends AbstractGenerator
{
protected void processPluginDescriptors( MojoDescriptor[] mojoDescriptors, String destinationDirectory, Xpp3Dom pomDom )
throws Exception
{
for ( int i = 0; i < mojoDescriptors.length; i++ )
{
processPluginDescriptor( mojoDescriptors[i], destinationDirectory );
}
}
protected void processPluginDescriptor( MojoDescriptor mojoDescriptor, String destinationDirectory )
throws Exception
{
String id = mojoDescriptor.getId();
FileWriter writer = new FileWriter( new File( destinationDirectory, id + "-plugin.xml" ) );
XMLWriter w = new PrettyPrintXMLWriter( writer );
w.startElement( "document" );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "properties" );
w.startElement( "title" );
w.writeText( "Documentation for the " + mojoDescriptor.getId() + " plugin." );
w.endElement();
w.startElement( "author" );
w.addAttribute( "email", "dev@maven.apache.org" );
w.writeText( "Maven developement team." );
w.endElement();
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "section" );
w.addAttribute( "name", "Goals" );
w.startElement( "p" );
w.writeText( "The goals for the " + mojoDescriptor.getId() + " are as follows:" );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "subsection" );
w.addAttribute( "name", mojoDescriptor.getGoal() );
if ( mojoDescriptor.getDescription() != null )
{
w.startElement( "p" );
w.writeText( mojoDescriptor.getDescription() );
w.endElement();
}
w.startElement( "p" );
w.writeText( "These parameters for this goal: " );
w.endElement();
writeGoalParameterTable( mojoDescriptor, w );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.endElement();
writer.flush();
writer.close();
}
private void writeGoalParameterTable( MojoDescriptor mojoDescriptor, XMLWriter w )
throws Exception
{
w.startElement( "p" );
w.startElement( "table" );
w.startElement( "tr" );
w.startElement( "th" );
w.writeText( "Parameter" );
w.endElement();
w.startElement( "th" );
w.writeText( "Expression" );
w.endElement();
w.startElement( "th" );
w.writeText( "Description" );
w.endElement();
w.endElement();
List parameters = mojoDescriptor.getParameters();
Map parameterMap = mojoDescriptor.getParameterMap();
for ( int i = 0; i < parameters.size(); i++ )
{
Parameter parameter = (Parameter) parameters.get( i );
w.startElement( "tr" );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "td" );
w.writeText( parameter.getName() );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "td" );
w.writeText( parameter.getExpression() );
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "td" );
Parameter p = (Parameter) parameterMap.get( parameter.getName() );
w.writeText( p.getDescription() );
w.endElement();
w.endElement();
}
w.endElement();
w.endElement();
}
}

View File

@ -0,0 +1,327 @@
package org.apache.maven.plugin.generator.jelly;
/*
* Copyright 2001-2004 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 com.thoughtworks.xstream.xml.text.PrettyPrintXMLWriter;
import com.thoughtworks.xstream.xml.XMLWriter;
import com.thoughtworks.xstream.xml.xpp3.Xpp3Dom;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.generator.AbstractGenerator;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
/**
* @todo use the descriptions in the descriptor for the javadoc pushed into the source code.
* @todo write plugin.properties (as a place holder, we don't technially need it)
* @todo convert POM or just strip out the dependencies to create a project.xml that
* will serve as the trigger to download dependencies.
*/
public class JellyHarnessGenerator
extends AbstractGenerator
{
protected String getClassName( MojoDescriptor pluginDescriptor )
{
return pluginDescriptor.getImplementation() + "Bean";
}
protected void processPluginDescriptors( MojoDescriptor[] mojoDescriptors, String destinationDirectory, Xpp3Dom pomDom )
throws Exception
{
FileWriter writer = new FileWriter( new File( destinationDirectory, "plugin.jelly" ) );
PrettyPrintXMLWriter w = new PrettyPrintXMLWriter( writer );
String pluginId = pluginId( pomDom );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "project" );
w.addAttribute( "xmlns:j", "jelly:core" );
w.addAttribute( "xmlns:d", "jelly:define" );
w.addAttribute( "xmlns:" + pluginId, pluginId );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( "d:taglib" );
w.addAttribute( "uri", pluginId );
for ( int i = 0; i < mojoDescriptors.length; i++ )
{
processPluginDescriptor( mojoDescriptors[i], w, pomDom );
}
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
for ( int i = 0; i < mojoDescriptors.length; i++ )
{
writeGoals( mojoDescriptors[i], w );
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
writer.flush();
writer.close();
// ----------------------------------------------------------------------
// project.xml
// ----------------------------------------------------------------------
writer = new FileWriter( new File( destinationDirectory, "project.xml" ) );
w = new PrettyPrintXMLWriter( writer );
w.startElement( "project" );
w.startElement( "dependencies" );
writeDependencies( w, pomDom );
w.endElement();
w.endElement();
writer.flush();
writer.close();
}
protected void processPluginDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w, Xpp3Dom pomDom )
throws Exception
{
String pluginId = pluginId( pomDom );
String goalName = mojoDescriptor.getGoal();
// ----------------------------------------------------------------------
// jellybean
// ----------------------------------------------------------------------
//
// <define:jellybean
// name="vdocletBean"
// className="org.apache.maven.vdoclet.VDocletBean"
// method="execute">
// </define:jellybean>
//
// ----------------------------------------------------------------------
w.startElement( "d:jellybean" );
w.addAttribute( "name", goalName + "Bean" );
w.addAttribute( "className", getClassName( mojoDescriptor ) );
w.addAttribute( "method", "execute" );
w.endElement();
// ----------------------------------------------------------------------
// tag
// ----------------------------------------------------------------------
//
// <define:tag name="vdoclet">
// <vdoclet:vdocletBean
// srcDir="${srcDir}"
// destDir="${destDir}"
// template="${template}"
// outputFile="${outputFile}"
// encoding="${encoding}"
// jellyContext="${context}"
// />
// </define:tag>
//
// ----------------------------------------------------------------------
w.startElement( "d:tag" );
w.addAttribute( "name", goalName );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.startElement( pluginId + ":" + goalName + "Bean" );
List parameters = mojoDescriptor.getParameters();
for ( int i = 0; i < parameters.size(); i++ )
{
Parameter parameter = (Parameter) parameters.get( i );
w.addAttribute( parameter.getName(), "${" + parameter.getName() + "}" );
}
w.endElement();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
w.endElement();
}
private void writeGoals( MojoDescriptor mojoDescriptor, XMLWriter w )
{
String id = mojoDescriptor.getId();
w.startElement( "goal" );
w.addAttribute( "name", id + ":" + mojoDescriptor.getGoal() );
List goalPrereqs = mojoDescriptor.getPrereqs();
if ( goalPrereqs.size() > 0 )
{
StringBuffer prereqs = new StringBuffer();
for ( int j = 0; j < goalPrereqs.size(); j++ )
{
String prereq = (String) goalPrereqs.get( j );
prereqs.append( prereq );
if ( j < goalPrereqs.size() - 1 )
{
prereqs.append( "," );
}
}
w.addAttribute( "prereqs", prereqs.toString() );
}
if ( mojoDescriptor.getDescription() != null )
{
w.addAttribute( "description", mojoDescriptor.getDescription() );
}
w.startElement( id + ":" + mojoDescriptor.getGoal() + "Bean" );
List goalParameters = mojoDescriptor.getParameters();
for ( int j = 0; j < goalParameters.size(); j++ )
{
Parameter p = (Parameter) goalParameters.get( j );
String expression = p.getExpression();
int projectIndex = expression.indexOf( "project" );
if ( projectIndex > 0 )
{
expression = expression.substring( 0, projectIndex ) + "pom" + expression.substring( projectIndex + 7 );
}
if ( expression.startsWith( "#" ) )
{
expression = "${" + expression.substring( 1 ) + "}";
}
w.addAttribute( p.getName(), expression );
}
w.endElement();
w.endElement();
}
protected void writeDependencies( XMLWriter w, Xpp3Dom pomDom )
{
writeDependency( w, "maven", "maven-plugin", "2.0-SNAPSHOT" );
Xpp3Dom depElement = pomDom.getChild( "dependencies" );
if ( depElement != null )
{
Xpp3Dom[] deps = depElement.getChildren( "dependency" );
for ( int i = 0; i < deps.length; i++ )
{
Xpp3Dom dep = deps[i];
String groupId = dep.getChild( "artifactId" ).getValue();
String artifactId = dep.getChild( "groupId" ).getValue();
String version = dep.getChild( "version" ).getValue();
writeDependency( w, groupId, artifactId, version );
}
}
}
protected void writeDependency( XMLWriter w, String groupId, String artifactId, String version )
{
w.startElement( "dependency" );
w.startElement( "groupId" );
w.writeText( groupId );
w.endElement();
w.startElement( "artifactId" );
w.writeText( artifactId );
w.endElement();
w.startElement( "version" );
w.writeText( version );
w.endElement();
w.endElement();
}
protected String capitalise( String str )
{
if ( str == null || str.length() == 0 )
{
return str;
}
return new StringBuffer( str.length() )
.append( Character.toTitleCase( str.charAt( 0 ) ) )
.append( str.substring( 1 ) )
.toString();
}
}

View File

@ -0,0 +1,90 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 junit.framework.TestCase;
import java.io.File;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public abstract class AbstractGeneratorTestCase
extends TestCase
{
protected Generator generator;
protected String basedir;
protected void setUp()
throws Exception
{
basedir = System.getProperty( "basedir" );
}
public void testGenerator()
throws Exception
{
setupGenerator();
String sourceDirectory = new File( basedir, "src/test/resources/source" ).getPath();
String destinationDirectory = new File( basedir, "target" ).getPath();
String pom = new File( basedir, "src/test/resources/source/pom.xml" ).getPath();
generator.execute( sourceDirectory, destinationDirectory, pom );
validate();
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
protected void setupGenerator()
throws Exception
{
String generatorClassName = getClass().getName();
generatorClassName = generatorClassName.substring( 0, generatorClassName.length() - 4 );
try
{
Class generatorClass = Thread.currentThread().getContextClassLoader().loadClass( generatorClassName );
generator = (Generator) generatorClass.newInstance();
}
catch ( Exception e )
{
throw new Exception(
"Cannot find " + generatorClassName + "! Make sure your test case is named in the form ${generatorClassName}Test " +
"or override the setupPlugin() method to instantiate the mojo yourself." );
}
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
protected void validate()
throws Exception
{
// empty
}
}

View File

@ -0,0 +1,30 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 junit.framework.TestCase;
import java.io.File;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class BeanGeneratorTest
extends AbstractGeneratorTestCase
{
}

View File

@ -0,0 +1,121 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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 java.io.File;
import java.io.FileReader;
import java.util.List;
import org.apache.maven.plugin.descriptor.Dependency;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class PluginDescriptorGeneratorTest
extends AbstractGeneratorTestCase
{
protected void validate()
throws Exception
{
PluginDescriptorBuilder pdb = new PluginDescriptorBuilder();
File pluginDescriptorFile = new File( basedir, "target/plugin.xml" );
PluginDescriptor pluginDescriptor = pdb.build( new FileReader( pluginDescriptorFile ) );
MojoDescriptor mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 0 );
if ( mojoDescriptor.getId().equals( "idea:ideaOne" ) )
{
checkMojoOne( mojoDescriptor );
mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 1 );
checkMojoTwo( mojoDescriptor );
}
else
{
checkMojoTwo( mojoDescriptor );
mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 1 );
checkMojoOne( mojoDescriptor );
}
// ----------------------------------------------------------------------
// Parameters
// ----------------------------------------------------------------------
List parameters = mojoDescriptor.getParameters();
assertEquals( 1, parameters.size() );
Parameter pd = (Parameter) mojoDescriptor.getParameters().get( 0 );
assertEquals( "project", pd.getName() );
assertEquals( "#project", pd.getExpression() );
// ----------------------------------------------------------------------
// Dependencies
// ----------------------------------------------------------------------
List dependencies = pluginDescriptor.getDependencies();
checkDependency( "maven", "maven-core", "2.0-SNAPSHOT", (Dependency) dependencies.get( 0 ) );
assertEquals( 3, dependencies.size() );
}
private void checkMojoOne( MojoDescriptor mojoDescriptor )
{
assertEquals( "idea:ideaOne", mojoDescriptor.getId() );
assertEquals( "org.apache.maven.plugin.idea.IdeaMojoOne", mojoDescriptor.getImplementation() );
assertEquals( "singleton", mojoDescriptor.getInstantiationStrategy() );
assertTrue( mojoDescriptor.requiresDependencyResolution() );
}
private void checkMojoTwo( MojoDescriptor mojoDescriptor )
{
assertEquals( "idea:ideaTwo", mojoDescriptor.getId() );
assertEquals( "org.apache.maven.plugin.idea.IdeaMojoTwo", mojoDescriptor.getImplementation() );
assertEquals( "singleton", mojoDescriptor.getInstantiationStrategy() );
assertFalse( mojoDescriptor.requiresDependencyResolution() );
}
private void checkDependency( String groupId, String artifactId, String version, Dependency dependency )
{
assertNotNull( dependency );
assertEquals( groupId, dependency.getGroupId() );
assertEquals( artifactId, dependency.getArtifactId() );
assertEquals( version, dependency.getVersion() );
}
}

View File

@ -0,0 +1,27 @@
package org.apache.maven.plugin.generator;
/*
* Copyright 2001-2004 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.
*/
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class PluginXdocGeneratorTest
extends AbstractGeneratorTestCase
{
}

View File

@ -0,0 +1,28 @@
package org.apache.maven.plugin.generator.jelly;
/*
* Copyright 2001-2004 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.generator.AbstractGeneratorTestCase;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class JellyHarnessGeneratorTest
extends AbstractGeneratorTestCase
{
}

View File

@ -0,0 +1,43 @@
package org.apache.maven.plugin.idea;
import org.apache.maven.project.MavenProject;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
/**
* @goal ideaOne
*
* @description Create an IDEA project file from a Maven project.
*
* @requiresDependencyResolution
*
* @prereq foo
* @prereq bar
*
* @parameter
* name="project"
* type="String[]"
* required="true"
* validator="org.foo.validator"
* expression="#project"
* description="Maven project used to generate IDEA project files."
*/
public class IdeaMojoOne
extends AbstractPlugin
{
protected IdeaWriter ideaWriter;
public IdeaPlugin()
{
ideaWriter = new IdeaWriter();
}
public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
throws Exception
{
MavenProject project = (MavenProject) request.getParameter( "project" );
ideaWriter.write( project );
}
}

View File

@ -0,0 +1,38 @@
package org.apache.maven.plugin.idea;
import org.apache.maven.project.MavenProject;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
/**
* @goal ideaTwo
*
* @description Create an IDEA project file from a Maven project.
*
* @parameter
* name="project"
* type="MavenProject"
* required="true"
* validator="org.foo.validator"
* expression="#project"
* description="Maven project used to generate IDEA project files."
*/
public class IdeaMojoTwo
extends AbstractPlugin
{
protected IdeaWriter ideaWriter;
public IdeaPlugin()
{
ideaWriter = new IdeaWriter();
}
public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
throws Exception
{
MavenProject project = (MavenProject) request.getParameter( "project" );
ideaWriter.write( project );
}
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven</groupId>
<artifactId>maven-plugin-parent</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<groupId>maven</groupId>
<artifactId>maven-idea-plugin</artifactId>
<name>Maven</name>
<version>1.0-SNAPSHOT</version>
<package>org.apache.maven</package>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>