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.Profile;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.apache.maven.plugins.annotations.Parameter;
27 import org.apache.maven.project.MavenProject;
28 import org.apache.maven.settings.SettingsUtils;
29
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40
41
42
43
44
45
46 @Mojo( name = "all-profiles", requiresProject = false )
47 public class AllProfilesMojo
48 extends AbstractHelpMojo
49 {
50
51
52
53
54
55
56
57 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
58 private List<MavenProject> projects;
59
60
61
62
63 @Parameter( defaultValue = "${settings.profiles}", readonly = true, required = true )
64 private List<org.apache.maven.settings.Profile> settingsProfiles;
65
66
67
68
69
70
71 public void execute()
72 throws MojoExecutionException, MojoFailureException
73 {
74 StringBuilder descriptionBuffer = new StringBuilder();
75
76 for ( MavenProject project : projects )
77 {
78 descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( LS );
79
80 Map<String, Profile> allProfilesByIds = new HashMap<String, Profile>();
81 Map<String, Profile> activeProfilesByIds = new HashMap<String, Profile>();
82 addSettingsProfiles( allProfilesByIds );
83 addProjectPomProfiles( project, allProfilesByIds, activeProfilesByIds );
84
85
86 if ( allProfilesByIds.isEmpty() )
87 {
88 getLog().warn( "No profiles detected!" );
89 }
90 else
91 {
92
93 allProfilesByIds.keySet().removeAll( activeProfilesByIds.keySet() );
94
95 writeProfilesDescription( descriptionBuffer, activeProfilesByIds, true );
96 writeProfilesDescription( descriptionBuffer, allProfilesByIds, false );
97 }
98 }
99
100 if ( output != null )
101 {
102 try
103 {
104 writeFile( output, descriptionBuffer );
105 }
106 catch ( IOException e )
107 {
108 throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e );
109 }
110
111 getLog().info( "Wrote descriptions to: " + output );
112 }
113 else
114 {
115 getLog().info( descriptionBuffer.toString() );
116 }
117 }
118
119
120
121
122
123 private void writeProfilesDescription( StringBuilder sb, Map<String, Profile> profilesByIds, boolean active )
124 {
125 for ( Profile p : profilesByIds.values() )
126 {
127 sb.append( " Profile Id: " ).append( p.getId() );
128 sb.append( " (Active: " + active + " , Source: " ).append( p.getSource() ).append( ")" );
129 sb.append( LS );
130 }
131 }
132
133
134
135
136
137
138
139
140 private void addProjectPomProfiles( MavenProject project, Map<String, Profile> allProfiles,
141 Map<String, Profile> activeProfiles )
142 {
143 if ( project == null )
144 {
145
146 getLog().debug( "No pom.xml found to read Profiles from." );
147 return;
148 }
149
150 getLog().debug( "Attempting to read profiles from pom.xml..." );
151
152 while ( project != null )
153 {
154 for ( Profile profile : project.getModel().getProfiles() )
155 {
156 allProfiles.put( profile.getId(), profile );
157 }
158 if ( project.getActiveProfiles() != null )
159 {
160 for ( Profile profile : project.getActiveProfiles() )
161 {
162 activeProfiles.put( profile.getId(), profile );
163 }
164 }
165 project = project.getParent();
166 }
167 }
168
169
170
171
172
173
174 private void addSettingsProfiles( Map<String, Profile> allProfiles )
175 {
176 getLog().debug( "Attempting to read profiles from settings.xml..." );
177 for ( org.apache.maven.settings.Profile settingsProfile : settingsProfiles )
178 {
179 Profile profile = SettingsUtils.convertFromSettingsProfile( settingsProfile );
180 allProfiles.put( profile.getId(), profile );
181 }
182 }
183 }