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.maven.doxia.site.decoration.DecorationModel;
23 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.codehaus.plexus.util.StringUtils;
29 import org.codehaus.plexus.util.WriterFactory;
30 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
31 import org.codehaus.plexus.util.xml.XMLWriter;
32 import org.codehaus.plexus.util.xml.XmlWriterUtil;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.StringWriter;
37 import java.io.Writer;
38
39
40
41
42
43
44
45
46
47 @Mojo( name = "effective-site", requiresReports = true )
48 public class EffectiveSiteMojo
49 extends AbstractSiteDescriptorMojo
50 {
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 XmlWriterUtil.writeComment( writer, "Generated by Maven Site Plugin" );
120 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
121 XmlWriterUtil.writeComment( writer, " " );
122 XmlWriterUtil.writeCommentLineBreak( writer );
123
124 XmlWriterUtil.writeLineBreak( writer );
125 }
126
127
128
129
130
131
132
133 protected static void writeComment( XMLWriter writer, String comment )
134 {
135 XmlWriterUtil.writeCommentLineBreak( writer );
136 XmlWriterUtil.writeComment( writer, " " );
137 XmlWriterUtil.writeComment( writer, comment );
138 XmlWriterUtil.writeComment( writer, " " );
139 XmlWriterUtil.writeCommentLineBreak( writer );
140
141 XmlWriterUtil.writeLineBreak( writer );
142 }
143
144 private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
145 throws MojoExecutionException
146 {
147 String effectiveSite;
148
149 StringWriter sWriter = new StringWriter();
150 DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
151 try
152 {
153 siteWriter.write( sWriter, decorationModel );
154 }
155 catch ( IOException e )
156 {
157 throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
158 }
159
160 effectiveSite = sWriter.toString();
161 effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) );
162
163 writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
164
165 writer.writeMarkup( effectiveSite );
166 }
167
168 protected static void writeXmlFile( File output, String content )
169 throws IOException
170 {
171 try ( Writer out = WriterFactory.newXmlWriter( output ) )
172 {
173 output.getParentFile().mkdirs();
174
175 out.write( content );
176 }
177 }
178 }