View Javadoc
1   package org.apache.maven.plugins.site.descriptor;
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.commons.lang3.time.DateFormatUtils;
23  import org.apache.maven.doxia.site.decoration.DecorationModel;
24  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.codehaus.plexus.util.StringUtils;
30  import org.codehaus.plexus.util.WriterFactory;
31  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
32  import org.codehaus.plexus.util.xml.XMLWriter;
33  import org.codehaus.plexus.util.xml.XmlWriterUtil;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.io.StringWriter;
38  import java.io.Writer;
39  
40  /**
41   * Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of
42   * <code>site.xml</code>, for the first locale.
43   *
44   * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
45   *
46   * @since 2.2
47   */
48  @Mojo( name = "effective-site", requiresReports = true )
49  public class EffectiveSiteMojo
50      extends AbstractSiteDescriptorMojo
51  {
52      /**
53       * Optional parameter to write the output of this help in a given file, instead of writing to the console.
54       * <br/>
55       * <b>Note</b>: Could be a relative path.
56       */
57      @Parameter( property = "output" )
58      protected File output;
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void execute()
64          throws MojoExecutionException, MojoFailureException
65      {
66          DecorationModel decorationModel = prepareDecorationModel( getLocales().get( 0 ) );
67  
68          StringWriter w = new StringWriter();
69          XMLWriter writer =
70              new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
71                                        decorationModel.getModelEncoding(), null );
72  
73          writeHeader( writer );
74  
75          writeEffectiveSite( decorationModel, writer );
76  
77          String effectiveSite = w.toString();
78  
79          if ( output != null )
80          {
81              try
82              {
83                  writeXmlFile( output, effectiveSite );
84              }
85              catch ( IOException e )
86              {
87                  throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
88              }
89  
90              if ( getLog().isInfoEnabled() )
91              {
92                  getLog().info( "Effective site descriptor written to: " + output );
93              }
94          }
95          else
96          {
97              StringBuilder message = new StringBuilder();
98  
99              message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
100             message.append( effectiveSite );
101             message.append( "\n" );
102 
103             if ( getLog().isInfoEnabled() )
104             {
105                 getLog().info( message.toString() );
106             }
107         }
108     }
109 
110     /**
111      * Write comments in the Effective POM/settings header.
112      *
113      * @param writer not null
114      */
115     protected static void writeHeader( XMLWriter writer )
116     {
117         XmlWriterUtil.writeCommentLineBreak( writer );
118         XmlWriterUtil.writeComment( writer, " " );
119         // Use ISO 8601 format for date and time
120         String formattedDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format( System.currentTimeMillis() );
121         XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin on " + formattedDateTime );
122         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
123         XmlWriterUtil.writeComment( writer, " " );
124         XmlWriterUtil.writeCommentLineBreak( writer );
125 
126         XmlWriterUtil.writeLineBreak( writer );
127     }
128 
129     /**
130      * Write comments in a normalize way.
131      *
132      * @param writer not null
133      * @param comment not null
134      */
135     protected static void writeComment( XMLWriter writer, String comment )
136     {
137         XmlWriterUtil.writeCommentLineBreak( writer );
138         XmlWriterUtil.writeComment( writer, " " );
139         XmlWriterUtil.writeComment( writer, comment );
140         XmlWriterUtil.writeComment( writer, " " );
141         XmlWriterUtil.writeCommentLineBreak( writer );
142 
143         XmlWriterUtil.writeLineBreak( writer );
144     }
145 
146     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
147         throws MojoExecutionException
148     {
149         String effectiveSite;
150 
151         StringWriter sWriter = new StringWriter();
152         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
153         try
154         {
155             siteWriter.write( sWriter, decorationModel );
156         }
157         catch ( IOException e )
158         {
159             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
160         }
161 
162         effectiveSite = sWriter.toString();
163         effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) ); // remove "<?xml" header
164 
165         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
166 
167         writer.writeMarkup( effectiveSite );
168     }
169 
170     protected static void writeXmlFile( File output, String content )
171         throws IOException
172     {
173         try ( Writer out = WriterFactory.newXmlWriter( output ) )
174         {
175             output.getParentFile().mkdirs();
176 
177             out.write( content );
178         }
179     }
180 }