1 package org.apache.maven.plugins.site.descriptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang3.time.DateFormatUtils;
23 import org.apache.maven.doxia.site.decoration.DecorationModel;
24 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.codehaus.plexus.util.StringUtils;
30 import org.codehaus.plexus.util.WriterFactory;
31 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
32 import org.codehaus.plexus.util.xml.XMLWriter;
33 import org.codehaus.plexus.util.xml.XmlWriterUtil;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.StringWriter;
38 import java.io.Writer;
39
40
41
42
43
44
45
46
47
48 @Mojo( name = "effective-site", requiresReports = true )
49 public class EffectiveSiteMojo
50 extends AbstractSiteDescriptorMojo
51 {
52
53
54
55
56
57 @Parameter( property = "output" )
58 protected File output;
59
60
61
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
112
113
114
115 protected static void writeHeader( XMLWriter writer )
116 {
117 XmlWriterUtil.writeCommentLineBreak( writer );
118 XmlWriterUtil.writeComment( writer, " " );
119
120 String formattedDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format( System.currentTimeMillis() );
121 XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin on " + formattedDateTime );
122 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
123 XmlWriterUtil.writeComment( writer, " " );
124 XmlWriterUtil.writeCommentLineBreak( writer );
125
126 XmlWriterUtil.writeLineBreak( writer );
127 }
128
129
130
131
132
133
134
135 protected static void writeComment( XMLWriter writer, String comment )
136 {
137 XmlWriterUtil.writeCommentLineBreak( writer );
138 XmlWriterUtil.writeComment( writer, " " );
139 XmlWriterUtil.writeComment( writer, comment );
140 XmlWriterUtil.writeComment( writer, " " );
141 XmlWriterUtil.writeCommentLineBreak( writer );
142
143 XmlWriterUtil.writeLineBreak( writer );
144 }
145
146 private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
147 throws MojoExecutionException
148 {
149 String effectiveSite;
150
151 StringWriter sWriter = new StringWriter();
152 DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
153 try
154 {
155 siteWriter.write( sWriter, decorationModel );
156 }
157 catch ( IOException e )
158 {
159 throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
160 }
161
162 effectiveSite = sWriter.toString();
163 effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) );
164
165 writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
166
167 writer.writeMarkup( effectiveSite );
168 }
169
170 protected static void writeXmlFile( File output, String content )
171 throws IOException
172 {
173 try ( Writer out = WriterFactory.newXmlWriter( output ) )
174 {
175 output.getParentFile().mkdirs();
176
177 out.write( content );
178 }
179 }
180 }