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 | 0 | 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 | 0 | StringWriter w = new StringWriter(); |
87 | 0 | XMLWriter writer = |
88 | |
new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), |
89 | |
project.getModel().getModelEncoding(), null ); |
90 | |
|
91 | 0 | writeHeader( writer ); |
92 | |
|
93 | |
String effectivePom; |
94 | 0 | if ( projects.get( 0 ).equals( project ) && projects.size() > 1 ) |
95 | |
{ |
96 | |
|
97 | 0 | writer.startElement( "projects" ); |
98 | 0 | for ( Iterator it = projects.iterator(); it.hasNext(); ) |
99 | |
{ |
100 | 0 | MavenProject subProject = (MavenProject) it.next(); |
101 | |
|
102 | 0 | writeEffectivePom( subProject, writer ); |
103 | 0 | } |
104 | 0 | writer.endElement(); |
105 | |
|
106 | 0 | effectivePom = w.toString(); |
107 | 0 | effectivePom = prettyFormat( effectivePom ); |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | writeEffectivePom( project, writer ); |
112 | |
|
113 | 0 | effectivePom = w.toString(); |
114 | |
} |
115 | |
|
116 | 0 | if ( output != null ) |
117 | |
{ |
118 | |
try |
119 | |
{ |
120 | 0 | writeXmlFile( output, effectivePom, project.getModel().getModelEncoding() ); |
121 | |
} |
122 | 0 | catch ( IOException e ) |
123 | |
{ |
124 | 0 | throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e ); |
125 | 0 | } |
126 | |
|
127 | 0 | if ( getLog().isInfoEnabled() ) |
128 | |
{ |
129 | 0 | getLog().info( "Effective-POM written to: " + output ); |
130 | |
} |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | StringBuffer message = new StringBuffer(); |
135 | |
|
136 | 0 | message.append( "\nEffective POMs, after inheritance, interpolation, and profiles are applied:\n\n" ); |
137 | 0 | message.append( effectivePom ); |
138 | 0 | message.append( "\n" ); |
139 | |
|
140 | 0 | if ( getLog().isInfoEnabled() ) |
141 | |
{ |
142 | 0 | getLog().info( message.toString() ); |
143 | |
} |
144 | |
} |
145 | 0 | } |
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 | 0 | Model pom = project.getModel(); |
162 | 0 | cleanModel( pom ); |
163 | |
|
164 | |
String effectivePom; |
165 | |
|
166 | 0 | StringWriter sWriter = new StringWriter(); |
167 | 0 | MavenXpp3Writer pomWriter = new MavenXpp3Writer(); |
168 | |
try |
169 | |
{ |
170 | 0 | pomWriter.write( sWriter, pom ); |
171 | |
} |
172 | 0 | catch ( IOException e ) |
173 | |
{ |
174 | 0 | throw new MojoExecutionException( "Cannot serialize POM to XML.", e ); |
175 | 0 | } |
176 | |
|
177 | 0 | effectivePom = addMavenNamespace( sWriter.toString(), true ); |
178 | |
|
179 | 0 | writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" ); |
180 | |
|
181 | 0 | writer.writeMarkup( effectivePom ); |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
private static void cleanModel( Model pom ) |
190 | |
{ |
191 | 0 | Properties properties = new SortedProperties(); |
192 | 0 | properties.putAll( pom.getProperties() ); |
193 | 0 | pom.setProperties( properties ); |
194 | 0 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
private static String prettyFormat( String effectivePom ) |
201 | |
{ |
202 | 0 | SAXBuilder builder = new SAXBuilder(); |
203 | |
|
204 | |
try |
205 | |
{ |
206 | 0 | Document effectiveDocument = builder.build( new StringReader( effectivePom ) ); |
207 | |
|
208 | 0 | StringWriter w = new StringWriter(); |
209 | 0 | Format format = Format.getPrettyFormat(); |
210 | 0 | XMLOutputter out = new XMLOutputter( format ); |
211 | 0 | out.output( effectiveDocument, w ); |
212 | |
|
213 | 0 | return w.toString(); |
214 | |
} |
215 | 0 | catch ( JDOMException e ) |
216 | |
{ |
217 | 0 | return effectivePom; |
218 | |
} |
219 | 0 | catch ( IOException e ) |
220 | |
{ |
221 | 0 | return effectivePom; |
222 | |
} |
223 | |
} |
224 | |
} |