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