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 java.io.File;
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.io.Writer;
26
27 import org.apache.commons.lang3.time.DateFormatUtils;
28 import org.apache.maven.doxia.site.decoration.DecorationModel;
29 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.codehaus.plexus.util.IOUtil;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.codehaus.plexus.util.WriterFactory;
37 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
38 import org.codehaus.plexus.util.xml.XMLWriter;
39 import org.codehaus.plexus.util.xml.XmlWriterUtil;
40
41
42
43
44
45
46
47
48
49 @Mojo( name = "effective-site", requiresReports = true )
50 public class EffectiveSiteMojo
51 extends AbstractSiteDescriptorMojo
52 {
53
54
55
56
57
58 @Parameter( property = "output" )
59 protected File output;
60
61
62
63
64 public void execute()
65 throws MojoExecutionException, MojoFailureException
66 {
67 DecorationModel decorationModel = prepareDecorationModel( getLocales().get( 0 ) );
68
69 StringWriter w = new StringWriter();
70 XMLWriter writer =
71 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
72 decorationModel.getModelEncoding(), null );
73
74 writeHeader( writer );
75
76 writeEffectiveSite( decorationModel, writer );
77
78 String effectiveSite = w.toString();
79
80 if ( output != null )
81 {
82 try
83 {
84 writeXmlFile( output, effectiveSite );
85 }
86 catch ( IOException e )
87 {
88 throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
89 }
90
91 if ( getLog().isInfoEnabled() )
92 {
93 getLog().info( "Effective site descriptor written to: " + output );
94 }
95 }
96 else
97 {
98 StringBuilder message = new StringBuilder();
99
100 message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
101 message.append( effectiveSite );
102 message.append( "\n" );
103
104 if ( getLog().isInfoEnabled() )
105 {
106 getLog().info( message.toString() );
107 }
108 }
109 }
110
111
112
113
114
115
116 protected static void writeHeader( XMLWriter writer )
117 {
118 XmlWriterUtil.writeCommentLineBreak( writer );
119 XmlWriterUtil.writeComment( writer, " " );
120
121 String formattedDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format( System.currentTimeMillis() );
122 XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin on " + formattedDateTime );
123 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
124 XmlWriterUtil.writeComment( writer, " " );
125 XmlWriterUtil.writeCommentLineBreak( writer );
126
127 XmlWriterUtil.writeLineBreak( writer );
128 }
129
130
131
132
133
134
135
136 protected static void writeComment( XMLWriter writer, String comment )
137 {
138 XmlWriterUtil.writeCommentLineBreak( writer );
139 XmlWriterUtil.writeComment( writer, " " );
140 XmlWriterUtil.writeComment( writer, comment );
141 XmlWriterUtil.writeComment( writer, " " );
142 XmlWriterUtil.writeCommentLineBreak( writer );
143
144 XmlWriterUtil.writeLineBreak( writer );
145 }
146
147 private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
148 throws MojoExecutionException
149 {
150 String effectiveSite;
151
152 StringWriter sWriter = new StringWriter();
153 DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
154 try
155 {
156 siteWriter.write( sWriter, decorationModel );
157 }
158 catch ( IOException e )
159 {
160 throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
161 }
162
163 effectiveSite = sWriter.toString();
164 effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) );
165
166 writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
167
168 writer.writeMarkup( effectiveSite );
169 }
170
171 protected static void writeXmlFile( File output, String content )
172 throws IOException
173 {
174 Writer out = null;
175 try
176 {
177 output.getParentFile().mkdirs();
178
179 out = WriterFactory.newXmlWriter( output );
180
181 out.write( content );
182
183 out.close();
184 out = null;
185 }
186 finally
187 {
188 IOUtil.close( out );
189 }
190 }
191 }