1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.apache.maven.model.Model;
30 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.project.MavenProject;
33 import org.codehaus.plexus.util.StringUtils;
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 import org.jdom.Document;
38 import org.jdom.JDOMException;
39 import org.jdom.input.SAXBuilder;
40 import org.jdom.output.Format;
41 import org.jdom.output.XMLOutputter;
42
43
44
45
46
47
48
49
50
51 public class EffectivePomMojo
52 extends AbstractEffectiveMojo
53 {
54
55
56
57
58
59
60
61
62
63
64
65
66 private MavenProject project;
67
68
69
70
71
72
73
74
75
76 private List projects;
77
78
79
80
81
82
83 public void execute()
84 throws MojoExecutionException
85 {
86 StringWriter w = new StringWriter();
87 XMLWriter writer =
88 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
89 project.getModel().getModelEncoding(), null );
90
91 writeHeader( writer );
92
93 String effectivePom;
94 if ( projects.get( 0 ).equals( project ) && projects.size() > 1 )
95 {
96
97 writer.startElement( "projects" );
98 for ( Iterator it = projects.iterator(); it.hasNext(); )
99 {
100 MavenProject subProject = (MavenProject) it.next();
101
102 writeEffectivePom( subProject, writer );
103 }
104 writer.endElement();
105
106 effectivePom = w.toString();
107 effectivePom = prettyFormat( effectivePom );
108 }
109 else
110 {
111 writeEffectivePom( project, writer );
112
113 effectivePom = w.toString();
114 }
115
116 if ( output != null )
117 {
118 try
119 {
120 writeXmlFile( output, effectivePom, project.getModel().getModelEncoding() );
121 }
122 catch ( IOException e )
123 {
124 throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
125 }
126
127 if ( getLog().isInfoEnabled() )
128 {
129 getLog().info( "Effective-POM written to: " + output );
130 }
131 }
132 else
133 {
134 StringBuffer message = new StringBuffer();
135
136 message.append( "\nEffective POMs, after inheritance, interpolation, and profiles are applied:\n\n" );
137 message.append( effectivePom );
138 message.append( "\n" );
139
140 if ( getLog().isInfoEnabled() )
141 {
142 getLog().info( message.toString() );
143 }
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 private static void writeEffectivePom( MavenProject project, XMLWriter writer )
159 throws MojoExecutionException
160 {
161 Model pom = project.getModel();
162 cleanModel( pom );
163
164 String effectivePom;
165
166 StringWriter sWriter = new StringWriter();
167 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
168 try
169 {
170 pomWriter.write( sWriter, pom );
171 }
172 catch ( IOException e )
173 {
174 throw new MojoExecutionException( "Cannot serialize POM to XML.", e );
175 }
176
177 effectivePom = addMavenNamespace( sWriter.toString(), true );
178
179 writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" );
180
181 writer.writeMarkup( effectivePom );
182 }
183
184
185
186
187
188
189 private static void cleanModel( Model pom )
190 {
191 Properties properties = new SortedProperties();
192 properties.putAll( pom.getProperties() );
193 pom.setProperties( properties );
194 }
195
196
197
198
199
200 private static String prettyFormat( String effectivePom )
201 {
202 SAXBuilder builder = new SAXBuilder();
203
204 try
205 {
206 Document effectiveDocument = builder.build( new StringReader( effectivePom ) );
207
208 StringWriter w = new StringWriter();
209 Format format = Format.getPrettyFormat();
210 XMLOutputter out = new XMLOutputter( format );
211 out.output( effectiveDocument, w );
212
213 return w.toString();
214 }
215 catch ( JDOMException e )
216 {
217 return effectivePom;
218 }
219 catch ( IOException e )
220 {
221 return effectivePom;
222 }
223 }
224 }