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