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.maven.doxia.site.decoration.DecorationModel;
27  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.codehaus.plexus.util.StringUtils;
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          DecorationModel decorationModel = prepareDecorationModel(getLocales().get(0));
62  
63          StringWriter w = new StringWriter();
64          XMLWriter writer = new PrettyPrintXMLWriter(
65                  w,
66                  StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE),
67                  decorationModel.getModelEncoding(),
68                  null);
69  
70          writeHeader(writer);
71  
72          writeEffectiveSite(decorationModel, writer);
73  
74          String effectiveSite = w.toString();
75  
76          if (output != null) {
77              try {
78                  writeXmlFile(output, effectiveSite);
79              } catch (IOException e) {
80                  throw new MojoExecutionException("Cannot write effective site descriptor to output: " + output, e);
81              }
82  
83              if (getLog().isInfoEnabled()) {
84                  getLog().info("Effective site descriptor written to: " + output);
85              }
86          } else {
87              StringBuilder message = new StringBuilder();
88  
89              message.append("\nEffective site descriptor, after inheritance and interpolation:\n\n");
90              message.append(effectiveSite);
91              message.append("\n");
92  
93              if (getLog().isInfoEnabled()) {
94                  getLog().info(message.toString());
95              }
96          }
97      }
98  
99      /**
100      * Write comments in the Effective POM/settings header.
101      *
102      * @param writer not null
103      */
104     protected static void writeHeader(XMLWriter writer) {
105         XmlWriterUtil.writeCommentLineBreak(writer);
106         XmlWriterUtil.writeComment(writer, " ");
107         XmlWriterUtil.writeComment(writer, "Generated by Maven Site Plugin");
108         XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-site-plugin/");
109         XmlWriterUtil.writeComment(writer, " ");
110         XmlWriterUtil.writeCommentLineBreak(writer);
111 
112         XmlWriterUtil.writeLineBreak(writer);
113     }
114 
115     /**
116      * Write comments in a normalize way.
117      *
118      * @param writer not null
119      * @param comment not null
120      */
121     protected static void writeComment(XMLWriter writer, String comment) {
122         XmlWriterUtil.writeCommentLineBreak(writer);
123         XmlWriterUtil.writeComment(writer, " ");
124         XmlWriterUtil.writeComment(writer, comment);
125         XmlWriterUtil.writeComment(writer, " ");
126         XmlWriterUtil.writeCommentLineBreak(writer);
127 
128         XmlWriterUtil.writeLineBreak(writer);
129     }
130 
131     private void writeEffectiveSite(DecorationModel decorationModel, XMLWriter writer) throws MojoExecutionException {
132         String effectiveSite;
133 
134         StringWriter sWriter = new StringWriter();
135         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
136         try {
137             siteWriter.write(sWriter, decorationModel);
138         } catch (IOException e) {
139             throw new MojoExecutionException("Cannot serialize site descriptor to XML.", e);
140         }
141 
142         effectiveSite = sWriter.toString();
143         effectiveSite = effectiveSite.substring(effectiveSite.indexOf("<project ")); // remove "<?xml" header
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 }