View Javadoc

1   package org.apache.maven.plugins.site;
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 java.io.File;
23  import java.io.IOException;
24  import java.io.StringWriter;
25  import java.io.Writer;
26  
27  import java.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  
30  import java.util.Date;
31  import java.util.List;
32  import java.util.Locale;
33  
34  import org.apache.maven.doxia.site.decoration.DecorationModel;
35  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
36  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.MojoFailureException;
39  
40  import org.apache.maven.plugins.annotations.Mojo;
41  import org.apache.maven.plugins.annotations.Parameter;
42  import org.codehaus.plexus.util.IOUtil;
43  import org.codehaus.plexus.util.StringUtils;
44  import org.codehaus.plexus.util.WriterFactory;
45  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
46  import org.codehaus.plexus.util.xml.XMLWriter;
47  import org.codehaus.plexus.util.xml.XmlWriterUtil;
48  
49  /**
50   * Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of
51   * <code>site.xml</code>.
52   *
53   * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
54   * @version $Id: EffectiveSiteMojo.html 861484 2013-05-09 23:10:16Z hboutemy $
55   * @since 2.2
56   */
57  @Mojo( name = "effective-site", requiresReports = true )
58  public class EffectiveSiteMojo
59      extends AbstractSiteRenderingMojo
60  {
61      /**
62       * Optional parameter to write the output of this help in a given file, instead of writing to the console.
63       * <br/>
64       * <b>Note</b>: Could be a relative path.
65       */
66      @Parameter( property = "output" )
67      protected File output;
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void execute()
73          throws MojoExecutionException, MojoFailureException
74      {
75          String effectiveSite;
76  
77          try
78          {
79              List<Locale> localesList = siteTool.getAvailableLocales( locales );
80  
81              SiteRenderingContext context = createSiteRenderingContext( localesList.get( 0 ) );
82  
83              DecorationModel decorationModel = context.getDecoration();
84  
85              StringWriter w = new StringWriter();
86              XMLWriter writer =
87                  new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
88                                            decorationModel.getModelEncoding(), null );
89  
90              writeHeader( writer );
91  
92              writeEffectiveSite( decorationModel, writer );
93  
94              effectiveSite = w.toString();
95          }
96          catch ( IOException e )
97          {
98              throw new MojoExecutionException( "Error during site descriptor calculation", e );
99          }
100 
101         if ( output != null )
102         {
103             try
104             {
105                 writeXmlFile( output, effectiveSite );
106             }
107             catch ( IOException e )
108             {
109                 throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
110             }
111 
112             if ( getLog().isInfoEnabled() )
113             {
114                 getLog().info( "Effective site descriptor written to: " + output );
115             }
116         }
117         else
118         {
119             StringBuilder message = new StringBuilder();
120 
121             message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
122             message.append( effectiveSite );
123             message.append( "\n" );
124 
125             if ( getLog().isInfoEnabled() )
126             {
127                 getLog().info( message.toString() );
128             }
129         }
130     }
131 
132     /**
133      * Write comments in the Effective POM/settings header.
134      *
135      * @param writer not null
136      */
137     protected static void writeHeader( XMLWriter writer )
138     {
139         XmlWriterUtil.writeCommentLineBreak( writer );
140         XmlWriterUtil.writeComment( writer, " " );
141         // Use ISO8601-format for date and time
142         DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
143         XmlWriterUtil.writeComment( writer,
144                                     "Generated by Maven Site Plugin on "
145                                         + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
146         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
147         XmlWriterUtil.writeComment( writer, " " );
148         XmlWriterUtil.writeCommentLineBreak( writer );
149 
150         XmlWriterUtil.writeLineBreak( writer );
151     }
152 
153     /**
154      * Write comments in a normalize way.
155      *
156      * @param writer not null
157      * @param comment not null
158      */
159     protected static void writeComment( XMLWriter writer, String comment )
160     {
161         XmlWriterUtil.writeCommentLineBreak( writer );
162         XmlWriterUtil.writeComment( writer, " " );
163         XmlWriterUtil.writeComment( writer, comment );
164         XmlWriterUtil.writeComment( writer, " " );
165         XmlWriterUtil.writeCommentLineBreak( writer );
166 
167         XmlWriterUtil.writeLineBreak( writer );
168     }
169 
170     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
171         throws MojoExecutionException
172     {
173         String effectiveSite;
174 
175         StringWriter sWriter = new StringWriter();
176         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
177         try
178         {
179             siteWriter.write( sWriter, decorationModel );
180         }
181         catch ( IOException e )
182         {
183             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
184         }
185 
186         effectiveSite = sWriter.toString();
187         effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) ); // remove "<?xml" header
188 
189         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
190 
191         writer.writeMarkup( effectiveSite );
192     }
193 
194     protected static void writeXmlFile( File output, String content )
195         throws IOException
196     {
197         Writer out = null;
198         try
199         {
200             output.getParentFile().mkdirs();
201 
202             out = WriterFactory.newXmlWriter( output );
203 
204             out.write( content );
205 
206             out.flush();
207         }
208         finally
209         {
210             IOUtil.close( out );
211         }
212     }
213 }