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 | 0 | 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 | 0 | StringBuffer descriptionBuffer = new StringBuffer(); |
90 | |
|
91 | 0 | for ( Iterator iter = projects.iterator(); iter.hasNext(); ) |
92 | |
{ |
93 | 0 | MavenProject project = (MavenProject) iter.next(); |
94 | |
|
95 | 0 | descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( "\n" ); |
96 | |
|
97 | 0 | DefaultProfileManager pm = |
98 | |
new DefaultProfileManager( session.getContainer(), session.getExecutionProperties() ); |
99 | |
|
100 | |
|
101 | |
try |
102 | |
{ |
103 | 0 | loadProjectExternalProfiles( pm, project.getBasedir() ); |
104 | |
} |
105 | 0 | catch ( ProfileActivationException e ) |
106 | |
{ |
107 | 0 | throw new MojoExecutionException( "Error obtaining external Profiles:" + e.getMessage(), e ); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | 0 | loadSettingsProfiles( pm, session.getSettings() ); |
112 | |
|
113 | |
|
114 | 0 | loadProjectPomProfiles( pm, project ); |
115 | |
|
116 | |
|
117 | 0 | if ( null == pm.getExplicitlyActivatedIds() || pm.getExplicitlyActivatedIds().size() == 0 ) |
118 | |
{ |
119 | 0 | if ( getLog().isWarnEnabled() ) |
120 | |
{ |
121 | 0 | getLog().warn( "No profiles detected!" ); |
122 | |
} |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | |
|
127 | |
|
128 | 0 | Map allProfilesByIds = pm.getProfilesById(); |
129 | |
|
130 | |
|
131 | 0 | List activeProfiles = project.getActiveProfiles(); |
132 | 0 | for ( Iterator itr = activeProfiles.iterator(); itr.hasNext(); ) |
133 | |
{ |
134 | 0 | Profile activeProfile = (Profile) itr.next(); |
135 | |
|
136 | |
|
137 | |
|
138 | 0 | allProfilesByIds.remove( activeProfile.getId() ); |
139 | 0 | } |
140 | |
|
141 | |
|
142 | 0 | for ( Iterator it = activeProfiles.iterator(); it.hasNext(); ) |
143 | |
{ |
144 | 0 | Profile p = (Profile) it.next(); |
145 | |
|
146 | 0 | descriptionBuffer.append( " Profile Id: " ).append( p.getId() ); |
147 | 0 | descriptionBuffer.append( " (Active: true , Source: " ).append( p.getSource() ).append( ")\n" ); |
148 | 0 | } |
149 | |
|
150 | |
|
151 | 0 | Iterator it = allProfilesByIds.keySet().iterator(); |
152 | 0 | while ( it.hasNext() ) |
153 | |
{ |
154 | 0 | Profile p = (Profile) allProfilesByIds.get( (String) it.next() ); |
155 | |
|
156 | 0 | descriptionBuffer.append( " Profile Id: " ).append( p.getId() ); |
157 | 0 | descriptionBuffer.append( " (Active: false , Source: " ).append( p.getSource() ).append( ")\n" ); |
158 | 0 | } |
159 | |
} |
160 | 0 | } |
161 | |
|
162 | 0 | if ( output != null ) |
163 | |
{ |
164 | |
try |
165 | |
{ |
166 | 0 | writeFile( output, descriptionBuffer ); |
167 | |
} |
168 | 0 | catch ( IOException e ) |
169 | |
{ |
170 | 0 | throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e ); |
171 | 0 | } |
172 | |
|
173 | 0 | if ( getLog().isInfoEnabled() ) |
174 | |
{ |
175 | 0 | getLog().info( "Wrote descriptions to: " + output ); |
176 | |
} |
177 | |
} |
178 | |
else |
179 | |
{ |
180 | 0 | if ( getLog().isInfoEnabled() ) |
181 | |
{ |
182 | 0 | getLog().info( descriptionBuffer.toString() ); |
183 | |
} |
184 | |
} |
185 | 0 | } |
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 | 0 | if ( projectDir == null ) |
203 | |
{ |
204 | 0 | return; |
205 | |
} |
206 | |
|
207 | 0 | if ( getLog().isDebugEnabled() ) |
208 | |
{ |
209 | 0 | getLog().debug( "Attempting to read profiles from external profiles.xml..." ); |
210 | |
} |
211 | |
|
212 | |
try |
213 | |
{ |
214 | 0 | DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder(); |
215 | 0 | ProfilesRoot root = profilesBuilder.buildProfiles( projectDir ); |
216 | 0 | if ( root != null ) |
217 | |
{ |
218 | 0 | for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); ) |
219 | |
{ |
220 | 0 | org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next(); |
221 | |
|
222 | 0 | Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ); |
223 | 0 | profileManager.addProfile( converted ); |
224 | 0 | profileManager.explicitlyActivate( converted.getId() ); |
225 | 0 | } |
226 | |
} |
227 | 0 | else if ( getLog().isDebugEnabled() ) |
228 | |
{ |
229 | 0 | getLog().debug( "ProfilesRoot was found to be NULL" ); |
230 | |
} |
231 | |
} |
232 | 0 | catch ( IOException e ) |
233 | |
{ |
234 | 0 | throw new ProfileActivationException( "Cannot read profiles.xml resource from directory: " |
235 | |
+ projectDir, e ); |
236 | |
} |
237 | 0 | catch ( XmlPullParserException e ) |
238 | |
{ |
239 | 0 | throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: " |
240 | |
+ projectDir, e ); |
241 | 0 | } |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
private void loadProjectPomProfiles( ProfileManager profilesManager, MavenProject project ) |
251 | |
{ |
252 | 0 | if ( project == null ) |
253 | |
{ |
254 | |
|
255 | 0 | if ( getLog().isDebugEnabled() ) |
256 | |
{ |
257 | 0 | getLog().debug( "No pom.xml found to read Profiles from." ); |
258 | |
} |
259 | |
|
260 | 0 | return; |
261 | |
} |
262 | |
|
263 | 0 | if ( getLog().isDebugEnabled() ) |
264 | |
{ |
265 | 0 | getLog().debug( "Attempting to read profiles from pom.xml..." ); |
266 | |
} |
267 | |
|
268 | |
|
269 | 0 | Iterator it = project.getModel().getProfiles().iterator(); |
270 | 0 | while ( it.hasNext() ) |
271 | |
{ |
272 | 0 | Profile profile = (Profile) it.next(); |
273 | |
|
274 | 0 | profilesManager.addProfile( profile ); |
275 | 0 | profilesManager.explicitlyActivate( profile.getId() ); |
276 | 0 | } |
277 | |
|
278 | 0 | MavenProject parent = project.getParent(); |
279 | 0 | while( parent != null ) |
280 | |
{ |
281 | 0 | Iterator it2 = parent.getModel().getProfiles().iterator(); |
282 | 0 | while ( it2.hasNext() ) |
283 | |
{ |
284 | 0 | Profile profile = (Profile) it2.next(); |
285 | |
|
286 | 0 | profilesManager.addProfile( profile ); |
287 | 0 | profilesManager.explicitlyActivate( profile.getId() ); |
288 | 0 | } |
289 | |
|
290 | 0 | parent = parent.getParent(); |
291 | 0 | } |
292 | 0 | } |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
private void loadSettingsProfiles( ProfileManager profileManager, Settings settings ) |
301 | |
{ |
302 | 0 | if ( settings == null ) |
303 | |
{ |
304 | 0 | if ( getLog().isDebugEnabled() ) |
305 | |
{ |
306 | 0 | getLog().debug( "No settings.xml detected." ); |
307 | |
} |
308 | |
|
309 | 0 | return; |
310 | |
} |
311 | |
|
312 | 0 | if ( getLog().isDebugEnabled() ) |
313 | |
{ |
314 | 0 | getLog().debug( "Attempting to read profiles from settings.xml..." ); |
315 | |
} |
316 | |
|
317 | 0 | Iterator it = settings.getProfiles().iterator(); |
318 | 0 | while ( it.hasNext() ) |
319 | |
{ |
320 | 0 | org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next(); |
321 | |
|
322 | 0 | Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); |
323 | 0 | profileManager.addProfile( profile ); |
324 | 0 | profileManager.explicitlyActivate( profile.getId() ); |
325 | 0 | } |
326 | 0 | } |
327 | |
} |