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.maven.doxia.site.decoration.DecorationModel;
23  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.WriterFactory;
30  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
31  import org.codehaus.plexus.util.xml.XMLWriter;
32  import org.codehaus.plexus.util.xml.XmlWriterUtil;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.io.StringWriter;
37  import java.io.Writer;
38  
39  /**
40   * Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of
41   * <code>site.xml</code>, for the first locale.
42   *
43   * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
44   *
45   * @since 2.2
46   */
47  @Mojo( name = "effective-site", requiresReports = true )
48  public class EffectiveSiteMojo
49      extends AbstractSiteDescriptorMojo
50  {
51      /**
52       * Optional parameter to write the output of this help in a given file, instead of writing to the console.
53       * <p>
54       * <b>Note</b>: Could be a relative path.
55       * </p>
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         XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin" );
120         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
121         XmlWriterUtil.writeComment( writer, " " );
122         XmlWriterUtil.writeCommentLineBreak( writer );
123 
124         XmlWriterUtil.writeLineBreak( writer );
125     }
126 
127     /**
128      * Write comments in a normalize way.
129      *
130      * @param writer not null
131      * @param comment not null
132      */
133     protected static void writeComment( XMLWriter writer, String comment )
134     {
135         XmlWriterUtil.writeCommentLineBreak( writer );
136         XmlWriterUtil.writeComment( writer, " " );
137         XmlWriterUtil.writeComment( writer, comment );
138         XmlWriterUtil.writeComment( writer, " " );
139         XmlWriterUtil.writeCommentLineBreak( writer );
140 
141         XmlWriterUtil.writeLineBreak( writer );
142     }
143 
144     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
145         throws MojoExecutionException
146     {
147         String effectiveSite;
148 
149         StringWriter sWriter = new StringWriter();
150         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
151         try
152         {
153             siteWriter.write( sWriter, decorationModel );
154         }
155         catch ( IOException e )
156         {
157             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
158         }
159 
160         effectiveSite = sWriter.toString();
161         effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) ); // remove "<?xml" header
162 
163         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
164 
165         writer.writeMarkup( effectiveSite );
166     }
167 
168     protected static void writeXmlFile( File output, String content )
169         throws IOException
170     {
171         try ( Writer out = WriterFactory.newXmlWriter( output ) )
172         {
173             output.getParentFile().mkdirs();
174 
175             out.write( content );
176         }
177     }
178 }