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.File;
23 import java.io.IOException;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Profile;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.profiles.DefaultMavenProfilesBuilder;
33 import org.apache.maven.profiles.DefaultProfileManager;
34 import org.apache.maven.profiles.ProfileManager;
35 import org.apache.maven.profiles.ProfilesConversionUtils;
36 import org.apache.maven.profiles.ProfilesRoot;
37 import org.apache.maven.profiles.activation.ProfileActivationException;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.settings.Settings;
40 import org.apache.maven.settings.SettingsUtils;
41 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class AllProfilesMojo
57 extends AbstractHelpMojo
58 {
59
60
61
62
63
64
65
66
67
68
69
70 private List projects;
71
72
73
74
75
76
77
78
79 private MavenSession session;
80
81
82
83
84
85
86 public void execute()
87 throws MojoExecutionException, MojoFailureException
88 {
89 StringBuffer descriptionBuffer = new StringBuffer();
90
91 for ( Iterator iter = projects.iterator(); iter.hasNext(); )
92 {
93 MavenProject project = (MavenProject) iter.next();
94
95 descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( "\n" );
96
97 DefaultProfileManager pm =
98 new DefaultProfileManager( session.getContainer(), session.getExecutionProperties() );
99
100
101 try
102 {
103 loadProjectExternalProfiles( pm, project.getBasedir() );
104 }
105 catch ( ProfileActivationException e )
106 {
107 throw new MojoExecutionException( "Error obtaining external Profiles:" + e.getMessage(), e );
108 }
109
110
111 loadSettingsProfiles( pm, session.getSettings() );
112
113
114 loadProjectPomProfiles( pm, project );
115
116
117 if ( null == pm.getExplicitlyActivatedIds() || pm.getExplicitlyActivatedIds().size() == 0 )
118 {
119 if ( getLog().isWarnEnabled() )
120 {
121 getLog().warn( "No profiles detected!" );
122 }
123 }
124 else
125 {
126
127
128 Map allProfilesByIds = pm.getProfilesById();
129
130
131 List activeProfiles = project.getActiveProfiles();
132 for ( Iterator itr = activeProfiles.iterator(); itr.hasNext(); )
133 {
134 Profile activeProfile = (Profile) itr.next();
135
136
137
138 allProfilesByIds.remove( activeProfile.getId() );
139 }
140
141
142 for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
143 {
144 Profile p = (Profile) it.next();
145
146 descriptionBuffer.append( " Profile Id: " ).append( p.getId() );
147 descriptionBuffer.append( " (Active: true , Source: " ).append( p.getSource() ).append( ")\n" );
148 }
149
150
151 Iterator it = allProfilesByIds.keySet().iterator();
152 while ( it.hasNext() )
153 {
154 Profile p = (Profile) allProfilesByIds.get( (String) it.next() );
155
156 descriptionBuffer.append( " Profile Id: " ).append( p.getId() );
157 descriptionBuffer.append( " (Active: false , Source: " ).append( p.getSource() ).append( ")\n" );
158 }
159 }
160 }
161
162 if ( output != null )
163 {
164 try
165 {
166 writeFile( output, descriptionBuffer );
167 }
168 catch ( IOException e )
169 {
170 throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e );
171 }
172
173 if ( getLog().isInfoEnabled() )
174 {
175 getLog().info( "Wrote descriptions to: " + output );
176 }
177 }
178 else
179 {
180 if ( getLog().isInfoEnabled() )
181 {
182 getLog().info( descriptionBuffer.toString() );
183 }
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 private void loadProjectExternalProfiles( ProfileManager profileManager, File projectDir )
200 throws ProfileActivationException
201 {
202 if ( projectDir == null )
203 {
204 return;
205 }
206
207 if ( getLog().isDebugEnabled() )
208 {
209 getLog().debug( "Attempting to read profiles from external profiles.xml..." );
210 }
211
212 try
213 {
214 DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder();
215 ProfilesRoot root = profilesBuilder.buildProfiles( projectDir );
216 if ( root != null )
217 {
218 for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
219 {
220 org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
221
222 Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
223 profileManager.addProfile( converted );
224 profileManager.explicitlyActivate( converted.getId() );
225 }
226 }
227 else if ( getLog().isDebugEnabled() )
228 {
229 getLog().debug( "ProfilesRoot was found to be NULL" );
230 }
231 }
232 catch ( IOException e )
233 {
234 throw new ProfileActivationException( "Cannot read profiles.xml resource from directory: "
235 + projectDir, e );
236 }
237 catch ( XmlPullParserException e )
238 {
239 throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: "
240 + projectDir, e );
241 }
242 }
243
244
245
246
247
248
249
250 private void loadProjectPomProfiles( ProfileManager profilesManager, MavenProject project )
251 {
252 if ( project == null )
253 {
254
255 if ( getLog().isDebugEnabled() )
256 {
257 getLog().debug( "No pom.xml found to read Profiles from." );
258 }
259
260 return;
261 }
262
263 if ( getLog().isDebugEnabled() )
264 {
265 getLog().debug( "Attempting to read profiles from pom.xml..." );
266 }
267
268
269 Iterator it = project.getModel().getProfiles().iterator();
270 while ( it.hasNext() )
271 {
272 Profile profile = (Profile) it.next();
273
274 profilesManager.addProfile( profile );
275 profilesManager.explicitlyActivate( profile.getId() );
276 }
277
278 MavenProject parent = project.getParent();
279 while( parent != null )
280 {
281 Iterator it2 = parent.getModel().getProfiles().iterator();
282 while ( it2.hasNext() )
283 {
284 Profile profile = (Profile) it2.next();
285
286 profilesManager.addProfile( profile );
287 profilesManager.explicitlyActivate( profile.getId() );
288 }
289
290 parent = parent.getParent();
291 }
292 }
293
294
295
296
297
298
299
300 private void loadSettingsProfiles( ProfileManager profileManager, Settings settings )
301 {
302 if ( settings == null )
303 {
304 if ( getLog().isDebugEnabled() )
305 {
306 getLog().debug( "No settings.xml detected." );
307 }
308
309 return;
310 }
311
312 if ( getLog().isDebugEnabled() )
313 {
314 getLog().debug( "Attempting to read profiles from settings.xml..." );
315 }
316
317 Iterator it = settings.getProfiles().iterator();
318 while ( it.hasNext() )
319 {
320 org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
321
322 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
323 profileManager.addProfile( profile );
324 profileManager.explicitlyActivate( profile.getId() );
325 }
326 }
327 }