View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.descriptor;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.doxia.site.SiteModel;
28  import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
34  import org.codehaus.plexus.util.xml.XMLWriter;
35  import org.codehaus.plexus.util.xml.XmlStreamWriter;
36  import org.codehaus.plexus.util.xml.XmlWriterUtil;
37  
38  /**
39   * Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of
40   * <code>site.xml</code>, for the first locale.
41   *
42   * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
43   *
44   * @since 2.2
45   */
46  @Mojo(name = "effective-site", requiresReports = true)
47  public class EffectiveSiteMojo extends AbstractSiteDescriptorMojo {
48      /**
49       * Optional parameter to write the output of this help in a given file, instead of writing to the console.
50       * <p>
51       * <b>Note</b>: Could be a relative path.
52       * </p>
53       */
54      @Parameter(property = "output")
55      protected File output;
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public void execute() throws MojoExecutionException, MojoFailureException {
62          SiteModel siteModel = prepareSiteModel(getLocales().get(0));
63  
64          StringWriter w = new StringWriter();
65          XMLWriter writer = new PrettyPrintXMLWriter(
66                  w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), siteModel.getModelEncoding(), null);
67  
68          writeHeader(writer);
69  
70          writeEffectiveSite(siteModel, writer);
71  
72          String effectiveSite = w.toString();
73  
74          if (output != null) {
75              try {
76                  writeXmlFile(output, effectiveSite);
77              } catch (IOException e) {
78                  throw new MojoExecutionException("Cannot write effective site descriptor to output: " + output, e);
79              }
80  
81              if (getLog().isInfoEnabled()) {
82                  getLog().info("Effective site descriptor written to: " + output);
83              }
84          } else {
85              StringBuilder message = new StringBuilder();
86  
87              message.append("\nEffective site descriptor, after inheritance and interpolation:\n\n");
88              message.append(effectiveSite);
89              message.append("\n");
90  
91              if (getLog().isInfoEnabled()) {
92                  getLog().info(message.toString());
93              }
94          }
95      }
96  
97      /**
98       * Write comments in the Effective POM/settings header.
99       *
100      * @param writer not null
101      */
102     protected static void writeHeader(XMLWriter writer) {
103         XmlWriterUtil.writeCommentLineBreak(writer);
104         XmlWriterUtil.writeComment(writer, " ");
105         XmlWriterUtil.writeComment(writer, "Generated by Maven Site Plugin");
106         XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-site-plugin/");
107         XmlWriterUtil.writeComment(writer, " ");
108         XmlWriterUtil.writeCommentLineBreak(writer);
109 
110         XmlWriterUtil.writeLineBreak(writer);
111     }
112 
113     /**
114      * Write comments in a normalize way.
115      *
116      * @param writer not null
117      * @param comment not null
118      */
119     protected static void writeComment(XMLWriter writer, String comment) {
120         XmlWriterUtil.writeCommentLineBreak(writer);
121         XmlWriterUtil.writeComment(writer, " ");
122         XmlWriterUtil.writeComment(writer, comment);
123         XmlWriterUtil.writeComment(writer, " ");
124         XmlWriterUtil.writeCommentLineBreak(writer);
125 
126         XmlWriterUtil.writeLineBreak(writer);
127     }
128 
129     private void writeEffectiveSite(SiteModel siteModel, XMLWriter writer) throws MojoExecutionException {
130         String effectiveSite;
131 
132         StringWriter sWriter = new StringWriter();
133         SiteXpp3Writer siteWriter = new SiteXpp3Writer();
134         try {
135             siteWriter.write(sWriter, siteModel);
136         } catch (IOException e) {
137             throw new MojoExecutionException("Cannot serialize site descriptor to XML", e);
138         }
139 
140         effectiveSite = sWriter.toString();
141         // remove XML prolog
142         int xmlPrologStart = effectiveSite.indexOf("<?xml");
143         int xmlPrologEnd = effectiveSite.indexOf("?>", xmlPrologStart);
144         effectiveSite = effectiveSite.substring(xmlPrologEnd + 2).trim();
145 
146         writeComment(writer, "Effective site descriptor for project \'" + project.getId() + "\'");
147 
148         writer.writeMarkup(effectiveSite);
149     }
150 
151     protected static void writeXmlFile(File output, String content) throws IOException {
152         try (Writer out = new XmlStreamWriter(output)) {
153             output.getParentFile().mkdirs();
154 
155             out.write(content);
156         }
157     }
158 }