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 org.apache.maven.model.Model;
23 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
24 import org.apache.maven.plugin.MojoExecution;
25 import org.apache.maven.plugin.MojoExecution.Source;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.project.MavenProject;
30 import org.codehaus.plexus.util.StringUtils;
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.IOException;
36 import java.io.StringWriter;
37 import java.util.List;
38 import java.util.Properties;
39
40
41
42
43
44
45
46 @Mojo( name = "effective-pom", aggregator = true )
47 public class EffectivePomMojo
48 extends AbstractEffectiveMojo
49 {
50
51
52
53
54
55
56
57
58
59 @Parameter( defaultValue = "${project}", readonly = true, required = true )
60 private MavenProject project;
61
62
63
64
65
66 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
67 private List<MavenProject> projects;
68
69
70
71
72 @Parameter( defaultValue = "${mojo}", required = true, readonly = true )
73 private MojoExecution mojoExecution;
74
75
76
77
78
79
80
81
82
83 @Parameter( property = "artifact" )
84 private String artifact;
85
86
87
88
89
90
91 public void execute()
92 throws MojoExecutionException
93 {
94 if ( StringUtils.isNotEmpty( artifact ) )
95 {
96 project = getMavenProject( artifact );
97 }
98
99 StringWriter w = new StringWriter();
100 String encoding = output != null ? project.getModel().getModelEncoding()
101 : System.getProperty( "file.encoding" );
102 XMLWriter writer =
103 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
104 encoding, null );
105
106 writeHeader( writer );
107
108 if ( shouldWriteAllEffectivePOMsInReactor() )
109 {
110
111 writer.startElement( "projects" );
112 for ( MavenProject subProject : projects )
113 {
114 writeEffectivePom( subProject, writer );
115 }
116 writer.endElement();
117 }
118 else
119 {
120 writeEffectivePom( project, writer );
121 }
122
123 String effectivePom = prettyFormat( w.toString(), encoding, false );
124
125 if ( output != null )
126 {
127 try
128 {
129 writeXmlFile( output, effectivePom );
130 }
131 catch ( IOException e )
132 {
133 throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
134 }
135
136 getLog().info( "Effective-POM written to: " + output );
137 }
138 else
139 {
140 StringBuilder message = new StringBuilder();
141
142 message.append( LS );
143 message.append( "Effective POMs, after inheritance, interpolation, and profiles are applied:" );
144 message.append( LS ).append( LS );
145 message.append( effectivePom );
146 message.append( LS );
147
148 getLog().info( message.toString() );
149 }
150 }
151
152
153
154
155
156
157
158
159 private boolean shouldWriteAllEffectivePOMsInReactor()
160 {
161 Source source = mojoExecution.getSource();
162
163 return projects.size() > 1
164 && ( source == Source.CLI || source != Source.CLI && projects.get( 0 ).equals( project ) );
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 private static void writeEffectivePom( MavenProject project, XMLWriter writer )
179 throws MojoExecutionException
180 {
181 Model pom = project.getModel();
182 cleanModel( pom );
183
184 StringWriter sWriter = new StringWriter();
185 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
186 try
187 {
188 pomWriter.write( sWriter, pom );
189 }
190 catch ( IOException e )
191 {
192 throw new MojoExecutionException( "Cannot serialize POM to XML.", e );
193 }
194
195
196 String effectivePom = prettyFormat( sWriter.toString(), null, true );
197
198 writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" );
199
200 writer.writeMarkup( effectivePom );
201 }
202
203
204
205
206
207
208 private static void cleanModel( Model pom )
209 {
210 Properties properties = new SortedProperties();
211 properties.putAll( pom.getProperties() );
212 pom.setProperties( properties );
213 }
214 }