View Javadoc

1   package org.apache.maven.plugins.help;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.codehaus.plexus.util.IOUtil;
25  import org.codehaus.plexus.util.WriterFactory;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.Writer;
30  
31  /**
32   * Base class with some Help Mojo functionalities.
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @version $Id: AbstractHelpMojo.java 1384337 2012-09-13 13:53:19Z olamy $
36   * @since 2.1
37   */
38  public abstract class AbstractHelpMojo
39      extends AbstractMojo
40  {
41      /** The maximum length of a display line. */
42      protected static final int LINE_LENGTH = 79;
43  
44      /**
45       * Optional parameter to write the output of this help in a given file, instead of writing to the console.
46       * <br/>
47       * <b>Note</b>: Could be a relative path.
48       */
49      @Parameter( property = "output" )
50      protected File output;
51  
52      /**
53       * Utility method to write a content in a given file.
54       *
55       * @param output is the wanted output file.
56       * @param content contains the content to be written to the file.
57       * @throws IOException if any
58       * @see #writeFile(File, String)
59       */
60      protected static void writeFile( File output, StringBuilder content )
61          throws IOException
62      {
63          writeFile( output, content.toString() );
64      }
65  
66      /**
67       * Utility method to write a content in a given file.
68       *
69       * @param output is the wanted output file.
70       * @param content contains the content to be written to the file.
71       * @throws IOException if any
72       */
73      protected static void writeFile( File output, String content )
74          throws IOException
75      {
76          if ( output == null )
77          {
78              return;
79          }
80  
81          Writer out = null;
82          try
83          {
84              output.getParentFile().mkdirs();
85  
86              out = WriterFactory.newPlatformWriter( output );
87  
88              out.write( content );
89  
90              out.flush();
91          }
92          finally
93          {
94              IOUtil.close( out );
95          }
96      }
97  }