1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
42
43
44
45
46 @Mojo(name = "effective-site", requiresReports = true)
47 public class EffectiveSiteMojo extends AbstractSiteDescriptorMojo {
48
49
50
51
52
53
54 @Parameter(property = "output")
55 protected File output;
56
57
58
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
101
102
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
117
118
119
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 "));
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 }