missed to add files

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/branches/MPLUGIN-189@1337274 13f79535-47bb-0310-9956-ffa450edef68
master
Olivier Lamy 2012-05-11 16:45:16 +00:00
parent 4ec2eaf3a5
commit 0aa8ad9c05
2 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package org.apache.maven.tools.plugin.generator;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 Olivier Lamy
* @since 3.0
*/
public class GeneratorException
extends Exception
{
public GeneratorException( String s, Throwable throwable )
{
super( s, throwable );
}
}

View File

@ -0,0 +1,311 @@
#if ($helpPackageName.length>0)
package ${helpPackageName};
#end
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* @author
* @version
*/
@Mojo( name = "help", threadSafe = true, requiresProject = false)
public class HelpMojo
extends AbstractMojo
{
/**
* If <code>true</code>, display all settable properties for each goal.
*
*/
@Parameter( expression = "${detail}", defaultValue = "false" )
private boolean detail;
/**
* The name of the goal for which to show help. If unspecified, all goals will be displayed.
*
*/
@Parameter( expression = "${goal}" )
private java.lang.String goal;
/**
* The maximum length of a display line, should be positive.
*
*/
@Parameter( expression = "${lineLength}", defaultValue = "80" )
private int lineLength;
/**
* The number of spaces per indentation level, should be positive.
*
*/
@Parameter( expression = "${indentSize}", defaultValue = "2" )
private int indentSize;
// groupId/artifactId/version
private String pluginDescriptorPath = "${propertiesFilePath}";
private Xpp3Dom build()
throws MojoExecutionException
{
// olamy more than one pluginDescriptor in the classloader possible ?
InputStream is = getClass().getResourceAsStream( pluginDescriptorPath );
try
{
return Xpp3DomBuilder.build( is, "ISO-8859-1" );
}
catch ( XmlPullParserException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
/**
* {@inheritDoc}
*/
public void execute()
throws MojoExecutionException
{
if ( lineLength <= 0 )
{
getLog().warn( "The parameter 'lineLength' should be positive, using '80' as default." );
lineLength = 80;
}
if ( indentSize <= 0 )
{
getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
indentSize = 2;
}
// FIXME encoding as parameter
Xpp3Dom pluginElement = build();
StringBuffer sb = new StringBuffer();
/**
* <groupId>org.apache.maven.plugins</groupId>
* <artifactId>maven-plugin-plugin</artifactId>
* <version>3.0-SNAPSHOT</version>
*/
//append( sb, "org.apache.maven.plugins:maven-plugin-plugin:3.0-SNAPSHOT", 0 );
append( sb,
pluginElement.getChild( "groupId" ).getValue() + ":" + pluginElement.getChild( "artifactId" ).getValue()
+ ":" + pluginElement.getChild( "version" ).getValue(), 0 );
append( sb, "", 0 );
//append( sb, "Maven Plugin Plugin", 0 );
append( sb, pluginElement.getChild( "name" ).getValue(), 0 );
//append( sb,
// "The Plugin Plugin is used to create a Maven plugin descriptor for any Mojo\'s found in the source tree, to include in the JAR. It is also used to generate Xdoc files for the Mojos as well as for updating the plugin registry, the artifact metadata and a generic help goal.",
// 1 );
append( sb, pluginElement.getChild( "description" ).getValue(), 1 );
append( sb, "", 0 );
//<goalPrefix>plugin</goalPrefix>
String goalPrefix = pluginElement.getChild( "goalPrefix" ).getValue();
Xpp3Dom[] mojos = pluginElement.getChild( "mojos" ).getChildren( "mojo" );
if ( goal == null || goal.length() <= 0 )
{
append( sb, "This plugin has " + mojos.length + " goals:", 0 );
append( sb, "", 0 );
}
for ( Xpp3Dom mojo : mojos )
{
String mojoGoal = mojo.getChild( "goal" ).getValue();
Xpp3Dom configurationElement = mojo.getChild( "configuration" );
if ( goal == null || goal.length() <= 0 || mojoGoal.equals( goal ) )
{
append( sb, goalPrefix + ":" + mojoGoal, 0 );
append( sb, mojo.getChild( "description" ).getValue(), 1 );
append( sb, "", 0 );
if ( detail )
{
Xpp3Dom[] parameters = mojo.getChild( "parameters" ).getChildren( "parameter" );
append( sb, "Available parameters:", 1 );
append( sb, "", 0 );
append( sb, "goalPrefix", 2 );
append( sb, "The prefix for the plugin goal.", 3 );
append( sb, "", 0 );
for ( Xpp3Dom parameter : parameters )
{
Xpp3Dom name = parameter.getChild( "name" );
Xpp3Dom fieldConfigurationElement = configurationElement.getChild( name.getValue() );
if ( fieldConfigurationElement != null && fieldConfigurationElement.getValue() != null )
{
append( sb, name.getValue() + "(Default: " + configurationElement.getChild(
name.getValue() ).getAttribute( "default-value" ) + ")", 2 );
}
append( sb, parameter.getChild( "description" ).getValue(), 3 );
if ( fieldConfigurationElement != null && fieldConfigurationElement.getValue() != null )
{
append( sb, fieldConfigurationElement.getValue(), 3 );
}
append( sb, "", 0 );
}
}
}
}
if ( getLog().isInfoEnabled() )
{
getLog().info( sb.toString() );
}
}
/**
* <p>Repeat a String <code>n</code> times to form a new string.</p>
*
* @param str String to repeat
* @param repeat number of times to repeat str
* @return String with repeated String
* @throws NegativeArraySizeException if <code>repeat < 0</code>
* @throws NullPointerException if str is <code>null</code>
*/
private static String repeat( String str, int repeat )
{
StringBuffer buffer = new StringBuffer( repeat * str.length() );
for ( int i = 0; i < repeat; i++ )
{
buffer.append( str );
}
return buffer.toString();
}
/**
* Append a description to the buffer by respecting the indentSize and lineLength parameters.
* <b>Note</b>: The last character is always a new line.
*
* @param sb The buffer to append the description, not <code>null</code>.
* @param description The description, not <code>null</code>.
* @param indent The base indentation level of each line, must not be negative.
*/
private void append( StringBuffer sb, String description, int indent )
{
for ( Iterator it = toLines( description, indent, indentSize, lineLength ).iterator(); it.hasNext(); )
{
sb.append( it.next().toString() ).append( '\n' );
}
}
/**
* Splits the specified text into lines of convenient display length.
*
* @param text The text to split into lines, must not be <code>null</code>.
* @param indent The base indentation level of each line, must not be negative.
* @param indentSize The size of each indentation, must not be negative.
* @param lineLength The length of the line, must not be negative.
* @return The sequence of display lines, never <code>null</code>.
* @throws NegativeArraySizeException if <code>indent < 0</code>
*/
private static List toLines( String text, int indent, int indentSize, int lineLength )
{
List<String> lines = new ArrayList<String>();
String ind = repeat( "\t", indent );
String[] plainLines = text.split( "(\r\n)|(\r)|(\n)" );
for ( int i = 0; i < plainLines.length; i++ )
{
toLines( lines, ind + plainLines[i], indentSize, lineLength );
}
return lines;
}
/**
* Adds the specified line to the output sequence, performing line wrapping if necessary.
*
* @param lines The sequence of display lines, must not be <code>null</code>.
* @param line The line to add, must not be <code>null</code>.
* @param indentSize The size of each indentation, must not be negative.
* @param lineLength The length of the line, must not be negative.
*/
private static void toLines( List<String> lines, String line, int indentSize, int lineLength )
{
int lineIndent = getIndentLevel( line );
StringBuffer buf = new StringBuffer( 256 );
String[] tokens = line.split( " +" );
for ( int i = 0; i < tokens.length; i++ )
{
String token = tokens[i];
if ( i > 0 )
{
if ( buf.length() + token.length() >= lineLength )
{
lines.add( buf.toString() );
buf.setLength( 0 );
buf.append( repeat( " ", lineIndent * indentSize ) );
}
else
{
buf.append( ' ' );
}
}
for ( int j = 0; j < token.length(); j++ )
{
char c = token.charAt( j );
if ( c == '\t' )
{
buf.append( repeat( " ", indentSize - buf.length() % indentSize ) );
}
else if ( c == '\u00A0' )
{
buf.append( ' ' );
}
else
{
buf.append( c );
}
}
}
lines.add( buf.toString() );
}
/**
* Gets the indentation level of the specified line.
*
* @param line The line whose indentation level should be retrieved, must not be <code>null</code>.
* @return The indentation level of the line.
*/
private static int getIndentLevel( String line )
{
int level = 0;
for ( int i = 0; i < line.length() && line.charAt( i ) == '\t'; i++ )
{
level++;
}
for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
{
if ( line.charAt( i ) == '\t' )
{
level++;
break;
}
}
return level;
}
}