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