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       * <p>
55       * <b>Note</b>: Could be a relative path.
56       * </p>
57       */
58      @Parameter( property = "output" )
59      protected File output;
60  
61      /**
62       * {@inheritDoc}
63       */
64      public void execute()
65          throws MojoExecutionException, MojoFailureException
66      {
67          DecorationModel decorationModel = prepareDecorationModel( getLocales().get( 0 ) );
68  
69          StringWriter w = new StringWriter();
70          XMLWriter writer =
71              new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
72                                        decorationModel.getModelEncoding(), null );
73  
74          writeHeader( writer );
75  
76          writeEffectiveSite( decorationModel, writer );
77  
78          String effectiveSite = w.toString();
79  
80          if ( output != null )
81          {
82              try
83              {
84                  writeXmlFile( output, effectiveSite );
85              }
86              catch ( IOException e )
87              {
88                  throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
89              }
90  
91              if ( getLog().isInfoEnabled() )
92              {
93                  getLog().info( "Effective site descriptor written to: " + output );
94              }
95          }
96          else
97          {
98              StringBuilder message = new StringBuilder();
99  
100             message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
101             message.append( effectiveSite );
102             message.append( "\n" );
103 
104             if ( getLog().isInfoEnabled() )
105             {
106                 getLog().info( message.toString() );
107             }
108         }
109     }
110 
111     /**
112      * Write comments in the Effective POM/settings header.
113      *
114      * @param writer not null
115      */
116     protected static void writeHeader( XMLWriter writer )
117     {
118         XmlWriterUtil.writeCommentLineBreak( writer );
119         XmlWriterUtil.writeComment( writer, " " );
120         // Use ISO 8601 format for date and time
121         String formattedDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format( System.currentTimeMillis() );
122         XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin on " + formattedDateTime );
123         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
124         XmlWriterUtil.writeComment( writer, " " );
125         XmlWriterUtil.writeCommentLineBreak( writer );
126 
127         XmlWriterUtil.writeLineBreak( writer );
128     }
129 
130     /**
131      * Write comments in a normalize way.
132      *
133      * @param writer not null
134      * @param comment not null
135      */
136     protected static void writeComment( XMLWriter writer, String comment )
137     {
138         XmlWriterUtil.writeCommentLineBreak( writer );
139         XmlWriterUtil.writeComment( writer, " " );
140         XmlWriterUtil.writeComment( writer, comment );
141         XmlWriterUtil.writeComment( writer, " " );
142         XmlWriterUtil.writeCommentLineBreak( writer );
143 
144         XmlWriterUtil.writeLineBreak( writer );
145     }
146 
147     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
148         throws MojoExecutionException
149     {
150         String effectiveSite;
151 
152         StringWriter sWriter = new StringWriter();
153         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
154         try
155         {
156             siteWriter.write( sWriter, decorationModel );
157         }
158         catch ( IOException e )
159         {
160             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
161         }
162 
163         effectiveSite = sWriter.toString();
164         effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) ); // remove "<?xml" header
165 
166         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
167 
168         writer.writeMarkup( effectiveSite );
169     }
170 
171     protected static void writeXmlFile( File output, String content )
172         throws IOException
173     {
174         try ( Writer out = WriterFactory.newXmlWriter( output ) )
175         {
176             output.getParentFile().mkdirs();
177 
178             out.write( content );
179         }
180     }
181 }