mvn ${goalPrefix}:help -Ddetail=true -Dgoal=<goal-name>
to display parameter details.
* @author maven-plugin-tools
#if ( !$useAnnotations )
* @goal help
* @requiresProject false
* @threadSafe
#end
*/
#if ( $useAnnotations )
@Mojo( name = "help", requiresProject = false, threadSafe = true )
#end
public class HelpMojo
extends AbstractMojo
{
/**
* If true
, display all settable properties for each goal.
*
#if ( !$useAnnotations )
* @parameter property="detail" default-value="false"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "detail", defaultValue = "false" )
#end
private boolean detail;
/**
* The name of the goal for which to show help. If unspecified, all goals will be displayed.
*
#if ( !$useAnnotations )
* @parameter property="goal"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "goal" )
#end
private java.lang.String goal;
/**
* The maximum length of a display line, should be positive.
*
#if ( !$useAnnotations )
* @parameter property="lineLength" default-value="80"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "lineLength", defaultValue = "80" )
#end
private int lineLength;
/**
* The number of spaces per indentation level, should be positive.
*
#if ( !$useAnnotations )
* @parameter property="indentSize" default-value="2"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "indentSize", defaultValue = "2" )
#end
private int indentSize;
// groupId/artifactId/plugin-help.xml
private static final String PLUGIN_HELP_PATH =
"/${pluginHelpPath}";
private static final int DEFAULT_LINE_LENGTH = 80;
private Document build()
throws MojoExecutionException
{
getLog().debug( "load plugin-help.xml: " + PLUGIN_HELP_PATH );
InputStream is = null;
try
{
is = getClass().getResourceAsStream( PLUGIN_HELP_PATH );
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return dBuilder.parse( is );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( ParserConfigurationException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( SAXException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
finally
{
if ( is != null )
{
try
{
is.close();
}
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 = DEFAULT_LINE_LENGTH;
}
if ( indentSize <= 0 )
{
getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
indentSize = 2;
}
Document doc = build();
StringBuilder sb = new StringBuilder();
Node plugin = getSingleChild( doc, "plugin" );
String name = getValue( plugin, "name" );
String version = getValue( plugin, "version" );
String id = getValue( plugin, "groupId" ) + ":" + getValue( plugin, "artifactId" ) + ":" + version;
if ( isNotEmpty( name ) && !name.contains( id ) )
{
append( sb, name + " " + version, 0 );
}
else
{
if ( isNotEmpty( name ) )
{
append( sb, name, 0 );
}
else
{
append( sb, id, 0 );
}
}
append( sb, getValue( plugin, "description" ), 1 );
append( sb, "", 0 );
//Repeat a String n
times to form a new string.
repeat < 0
* @throws NullPointerException if str is null
*/
private static String repeat( String str, int repeat )
{
StringBuilder buffer = new StringBuilder( 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.
* Note: The last character is always a new line.
*
* @param sb The buffer to append the description, not null
.
* @param description The description, not null
.
* @param indent The base indentation level of each line, must not be negative.
*/
private void append( StringBuilder sb, String description, int indent )
{
for ( String line : toLines( description, indent, indentSize, lineLength ) )
{
sb.append( line ).append( '\n' );
}
}
/**
* Splits the specified text into lines of convenient display length.
*
* @param text The text to split into lines, must not be null
.
* @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 null
.
* @throws NegativeArraySizeException if indent < 0
*/
private static Listnull
.
* @param line The line to add, must not be null
.
* @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( Listnull
.
* @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;
}
private String getPropertyFromExpression( String expression )
{
if ( expression != null && expression.startsWith( "${" ) && expression.endsWith( "}" )
&& !expression.substring( 2 ).contains( "${" ) )
{
// expression="${xxx}" -> property="xxx"
return expression.substring( 2, expression.length() - 1 );
}
// no property can be extracted
return null;
}
}