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.WriterFactory;
34  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35  import org.codehaus.plexus.util.xml.XMLWriter;
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      public void execute() throws MojoExecutionException, MojoFailureException {
61          SiteModel siteModel = prepareSiteModel(getLocales().get(0));
62  
63          StringWriter w = new StringWriter();
64          XMLWriter writer = new PrettyPrintXMLWriter(
65                  w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), siteModel.getModelEncoding(), null);
66  
67          writeHeader(writer);
68  
69          writeEffectiveSite(siteModel, writer);
70  
71          String effectiveSite = w.toString();
72  
73          if (output != null) {
74              try {
75                  writeXmlFile(output, effectiveSite);
76              } catch (IOException e) {
77                  throw new MojoExecutionException("Cannot write effective site descriptor to output: " + output, e);
78              }
79  
80              if (getLog().isInfoEnabled()) {
81                  getLog().info("Effective site descriptor written to: " + output);
82              }
83          } else {
84              StringBuilder message = new StringBuilder();
85  
86              message.append("\nEffective site descriptor, after inheritance and interpolation:\n\n");
87              message.append(effectiveSite);
88              message.append("\n");
89  
90              if (getLog().isInfoEnabled()) {
91                  getLog().info(message.toString());
92              }
93          }
94      }
95  
96      /**
97       * Write comments in the Effective POM/settings header.
98       *
99       * @param writer not null
100      */
101     protected static void writeHeader(XMLWriter writer) {
102         XmlWriterUtil.writeCommentLineBreak(writer);
103         XmlWriterUtil.writeComment(writer, " ");
104         XmlWriterUtil.writeComment(writer, "Generated by Maven Site Plugin");
105         XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-site-plugin/");
106         XmlWriterUtil.writeComment(writer, " ");
107         XmlWriterUtil.writeCommentLineBreak(writer);
108 
109         XmlWriterUtil.writeLineBreak(writer);
110     }
111 
112     /**
113      * Write comments in a normalize way.
114      *
115      * @param writer not null
116      * @param comment not null
117      */
118     protected static void writeComment(XMLWriter writer, String comment) {
119         XmlWriterUtil.writeCommentLineBreak(writer);
120         XmlWriterUtil.writeComment(writer, " ");
121         XmlWriterUtil.writeComment(writer, comment);
122         XmlWriterUtil.writeComment(writer, " ");
123         XmlWriterUtil.writeCommentLineBreak(writer);
124 
125         XmlWriterUtil.writeLineBreak(writer);
126     }
127 
128     private void writeEffectiveSite(SiteModel siteModel, XMLWriter writer) throws MojoExecutionException {
129         String effectiveSite;
130 
131         StringWriter sWriter = new StringWriter();
132         SiteXpp3Writer siteWriter = new SiteXpp3Writer();
133         try {
134             siteWriter.write(sWriter, siteModel);
135         } catch (IOException e) {
136             throw new MojoExecutionException("Cannot serialize site descriptor to XML", e);
137         }
138 
139         effectiveSite = sWriter.toString();
140         // remove XML prolog
141         int xmlPrologStart = effectiveSite.indexOf("<?xml");
142         int xmlPrologEnd = effectiveSite.indexOf("?>", xmlPrologStart);
143         effectiveSite = effectiveSite.substring(xmlPrologEnd + 2).trim();
144 
145         writeComment(writer, "Effective site descriptor for project \'" + project.getId() + "\'");
146 
147         writer.writeMarkup(effectiveSite);
148     }
149 
150     protected static void writeXmlFile(File output, String content) throws IOException {
151         try (Writer out = WriterFactory.newXmlWriter(output)) {
152             output.getParentFile().mkdirs();
153 
154             out.write(content);
155         }
156     }
157 }