View Javadoc

1   package org.apache.maven.plugins.enforcer;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.apache.maven.plugin.AbstractMojo;
8   import org.apache.maven.plugin.MojoExecutionException;
9   
10  /**
11   * Display help information on maven-enforcer-plugin.<br/> Call <pre>  mvn enforcer:help -Ddetail=true -Dgoal=&lt;goal-name&gt;</pre> to display parameter details.
12   *
13   * @version generated on Tue May 08 21:10:26 CDT 2012
14   * @author org.apache.maven.tools.plugin.generator.PluginHelpGenerator (version 2.7)
15   * @goal help
16   * @requiresProject false
17   * @threadSafe
18   */
19  @SuppressWarnings( "all" )
20  public class HelpMojo
21      extends AbstractMojo
22  {
23      /**
24       * If <code>true</code>, display all settable properties for each goal.
25       * 
26       * @parameter expression="${detail}" default-value="false"
27       */
28      private boolean detail;
29  
30      /**
31       * The name of the goal for which to show help. If unspecified, all goals will be displayed.
32       * 
33       * @parameter expression="${goal}"
34       */
35      private java.lang.String goal;
36  
37      /**
38       * The maximum length of a display line, should be positive.
39       * 
40       * @parameter expression="${lineLength}" default-value="80"
41       */
42      private int lineLength;
43  
44      /**
45       * The number of spaces per indentation level, should be positive.
46       * 
47       * @parameter expression="${indentSize}" default-value="2"
48       */
49      private int indentSize;
50  
51  
52      /** {@inheritDoc} */
53      public void execute()
54          throws MojoExecutionException
55      {
56          if ( lineLength <= 0 )
57          {
58              getLog().warn( "The parameter 'lineLength' should be positive, using '80' as default." );
59              lineLength = 80;
60          }
61          if ( indentSize <= 0 )
62          {
63              getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
64              indentSize = 2;
65          }
66  
67          StringBuffer sb = new StringBuffer();
68  
69          append( sb, "org.apache.maven.plugins:maven-enforcer-plugin:1.1", 0 );
70          append( sb, "", 0 );
71  
72          append( sb, "Maven Enforcer Plugin", 0 );
73          append( sb, "The Loving Iron Fist of Maven", 1 );
74          append( sb, "", 0 );
75  
76          if ( goal == null || goal.length() <= 0 )
77          {
78              append( sb, "This plugin has 4 goals:", 0 );
79              append( sb, "", 0 );
80          }
81  
82          if ( goal == null || goal.length() <= 0 || "display-info".equals( goal ) )
83          {
84              append( sb, "enforcer:display-info", 0 );
85              append( sb, "This goal displays the current platform information.", 1 );
86              append( sb, "", 0 );
87              if ( detail )
88              {
89                  append( sb, "Available parameters:", 1 );
90                  append( sb, "", 0 );
91              }
92          }
93  
94          if ( goal == null || goal.length() <= 0 || "enforce".equals( goal ) )
95          {
96              append( sb, "enforcer:enforce", 0 );
97              append( sb, "This goal executes the defined enforcer-rules once per module.", 1 );
98              append( sb, "", 0 );
99              if ( detail )
100             {
101                 append( sb, "Available parameters:", 1 );
102                 append( sb, "", 0 );
103 
104                 append( sb, "fail (Default: true)", 2 );
105                 append( sb, "Flag to fail the build if a version check fails.", 3 );
106                 append( sb, "Expression: ${enforcer.fail}", 3 );
107                 append( sb, "", 0 );
108 
109                 append( sb, "failFast (Default: false)", 2 );
110                 append( sb, "Fail on the first rule that doesn\'t pass", 3 );
111                 append( sb, "Expression: ${enforcer.failFast}", 3 );
112                 append( sb, "", 0 );
113 
114                 append( sb, "ignoreCache (Default: false)", 2 );
115                 append( sb, "Use this flag to disable rule result caching. This will cause all rules to execute on each project even if the rule indicates it can safely be cached.", 3 );
116                 append( sb, "Expression: ${enforcer.ignoreCache}", 3 );
117                 append( sb, "", 0 );
118 
119                 append( sb, "rules", 2 );
120                 append( sb, "Array of objects that implement the EnforcerRule interface to execute.", 3 );
121                 append( sb, "Required: Yes", 3 );
122                 append( sb, "", 0 );
123 
124                 append( sb, "skip (Default: false)", 2 );
125                 append( sb, "Flag to easily skip all checks", 3 );
126                 append( sb, "Expression: ${enforcer.skip}", 3 );
127                 append( sb, "", 0 );
128             }
129         }
130 
131         if ( goal == null || goal.length() <= 0 || "enforce-once".equals( goal ) )
132         {
133             append( sb, "enforcer:enforce-once", 0 );
134             append( sb, "This goal has been deprecated.", 1 );
135             append( sb, "", 0 );
136             if ( detail )
137             {
138                 append( sb, "Available parameters:", 1 );
139                 append( sb, "", 0 );
140 
141                 append( sb, "fail (Default: true)", 2 );
142                 append( sb, "Flag to fail the build if a version check fails.", 3 );
143                 append( sb, "Expression: ${enforcer.fail}", 3 );
144                 append( sb, "", 0 );
145 
146                 append( sb, "failFast (Default: false)", 2 );
147                 append( sb, "Fail on the first rule that doesn\'t pass", 3 );
148                 append( sb, "Expression: ${enforcer.failFast}", 3 );
149                 append( sb, "", 0 );
150 
151                 append( sb, "ignoreCache (Default: false)", 2 );
152                 append( sb, "Use this flag to disable rule result caching. This will cause all rules to execute on each project even if the rule indicates it can safely be cached.", 3 );
153                 append( sb, "Expression: ${enforcer.ignoreCache}", 3 );
154                 append( sb, "", 0 );
155 
156                 append( sb, "rules", 2 );
157                 append( sb, "Array of objects that implement the EnforcerRule interface to execute.", 3 );
158                 append( sb, "Required: Yes", 3 );
159                 append( sb, "", 0 );
160 
161                 append( sb, "skip (Default: false)", 2 );
162                 append( sb, "Flag to easily skip all checks", 3 );
163                 append( sb, "Expression: ${enforcer.skip}", 3 );
164                 append( sb, "", 0 );
165             }
166         }
167 
168         if ( goal == null || goal.length() <= 0 || "help".equals( goal ) )
169         {
170             append( sb, "enforcer:help", 0 );
171             append( sb, "Display help information on maven-enforcer-plugin.\nCall\n\u00a0\u00a0mvn\u00a0enforcer:help\u00a0-Ddetail=true\u00a0-Dgoal=<goal-name>\nto display parameter details.", 1 );
172             append( sb, "", 0 );
173             if ( detail )
174             {
175                 append( sb, "Available parameters:", 1 );
176                 append( sb, "", 0 );
177 
178                 append( sb, "detail (Default: false)", 2 );
179                 append( sb, "If true, display all settable properties for each goal.", 3 );
180                 append( sb, "Expression: ${detail}", 3 );
181                 append( sb, "", 0 );
182 
183                 append( sb, "goal", 2 );
184                 append( sb, "The name of the goal for which to show help. If unspecified, all goals will be displayed.", 3 );
185                 append( sb, "Expression: ${goal}", 3 );
186                 append( sb, "", 0 );
187 
188                 append( sb, "indentSize (Default: 2)", 2 );
189                 append( sb, "The number of spaces per indentation level, should be positive.", 3 );
190                 append( sb, "Expression: ${indentSize}", 3 );
191                 append( sb, "", 0 );
192 
193                 append( sb, "lineLength (Default: 80)", 2 );
194                 append( sb, "The maximum length of a display line, should be positive.", 3 );
195                 append( sb, "Expression: ${lineLength}", 3 );
196                 append( sb, "", 0 );
197             }
198         }
199 
200         if ( getLog().isInfoEnabled() )
201         {
202             getLog().info( sb.toString() );
203         }
204     }
205 
206     /**
207      * <p>Repeat a String <code>n</code> times to form a new string.</p>
208      *
209      * @param str String to repeat
210      * @param repeat number of times to repeat str
211      * @return String with repeated String
212      * @throws NegativeArraySizeException if <code>repeat < 0</code>
213      * @throws NullPointerException if str is <code>null</code>
214      */
215     private static String repeat( String str, int repeat )
216     {
217         StringBuffer buffer = new StringBuffer( repeat * str.length() );
218 
219         for ( int i = 0; i < repeat; i++ )
220         {
221             buffer.append( str );
222         }
223 
224         return buffer.toString();
225     }
226 
227     /** 
228      * Append a description to the buffer by respecting the indentSize and lineLength parameters.
229      * <b>Note</b>: The last character is always a new line.
230      * 
231      * @param sb The buffer to append the description, not <code>null</code>.
232      * @param description The description, not <code>null</code>.
233      * @param indent The base indentation level of each line, must not be negative.
234      */
235     private void append( StringBuffer sb, String description, int indent )
236     {
237         for ( Iterator it = toLines( description, indent, indentSize, lineLength ).iterator(); it.hasNext(); )
238         {
239             sb.append( it.next().toString() ).append( '\n' );
240         }
241     }
242 
243     /** 
244      * Splits the specified text into lines of convenient display length.
245      * 
246      * @param text The text to split into lines, must not be <code>null</code>.
247      * @param indent The base indentation level of each line, must not be negative.
248      * @param indentSize The size of each indentation, must not be negative.
249      * @param lineLength The length of the line, must not be negative.
250      * @return The sequence of display lines, never <code>null</code>.
251      * @throws NegativeArraySizeException if <code>indent < 0</code>
252      */
253     private static List toLines( String text, int indent, int indentSize, int lineLength )
254     {
255         List lines = new ArrayList();
256 
257         String ind = repeat( "\t", indent );
258         String[] plainLines = text.split( "(\r\n)|(\r)|(\n)" );
259         for ( int i = 0; i < plainLines.length; i++ )
260         {
261             toLines( lines, ind + plainLines[i], indentSize, lineLength );
262         }
263 
264         return lines;
265     }
266 
267     /** 
268      * Adds the specified line to the output sequence, performing line wrapping if necessary.
269      * 
270      * @param lines The sequence of display lines, must not be <code>null</code>.
271      * @param line The line to add, must not be <code>null</code>.
272      * @param indentSize The size of each indentation, must not be negative.
273      * @param lineLength The length of the line, must not be negative.
274      */
275     private static void toLines( List lines, String line, int indentSize, int lineLength )
276     {
277         int lineIndent = getIndentLevel( line );
278         StringBuffer buf = new StringBuffer( 256 );
279         String[] tokens = line.split( " +" );
280         for ( int i = 0; i < tokens.length; i++ )
281         {
282             String token = tokens[i];
283             if ( i > 0 )
284             {
285                 if ( buf.length() + token.length() >= lineLength )
286                 {
287                     lines.add( buf.toString() );
288                     buf.setLength( 0 );
289                     buf.append( repeat( " ", lineIndent * indentSize ) );
290                 }
291                 else
292                 {
293                     buf.append( ' ' );
294                 }
295             }
296             for ( int j = 0; j < token.length(); j++ )
297             {
298                 char c = token.charAt( j );
299                 if ( c == '\t' )
300                 {
301                     buf.append( repeat( " ", indentSize - buf.length() % indentSize ) );
302                 }
303                 else if ( c == '\u00A0' )
304                 {
305                     buf.append( ' ' );
306                 }
307                 else
308                 {
309                     buf.append( c );
310                 }
311             }
312         }
313         lines.add( buf.toString() );
314     }
315 
316     /** 
317      * Gets the indentation level of the specified line.
318      * 
319      * @param line The line whose indentation level should be retrieved, must not be <code>null</code>.
320      * @return The indentation level of the line.
321      */
322     private static int getIndentLevel( String line )
323     {
324         int level = 0;
325         for ( int i = 0; i < line.length() && line.charAt( i ) == '\t'; i++ )
326         {
327             level++;
328         }
329         for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
330         {
331             if ( line.charAt( i ) == '\t' )
332             {
333                 level++;
334                 break;
335             }
336         }
337         return level;
338     }
339 }