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.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.maven.model.Profile;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.project.MavenProject;
36
37
38
39
40
41
42
43 @Mojo( name = "active-profiles", aggregator = true )
44 public class ActiveProfilesMojo
45 extends AbstractHelpMojo
46 {
47
48
49
50
51
52
53
54 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
55 private List<MavenProject> projects;
56
57
58
59
60
61
62 public void execute()
63 throws MojoExecutionException
64 {
65 StringBuilder message = new StringBuilder();
66
67 for ( MavenProject project : projects )
68 {
69 getActiveProfileStatement( project, message );
70
71 message.append( "\n\n" );
72 }
73
74 if ( output != null )
75 {
76 StringBuilder sb = new StringBuilder();
77 sb.append( "Created by: " + getClass().getName() ).append( "\n" );
78 sb.append( "Created on: " + new Date() ).append( "\n" ).append( "\n" );
79 sb.append( message.toString() );
80
81 try
82 {
83 writeFile( output, sb );
84 }
85 catch ( IOException e )
86 {
87 throw new MojoExecutionException( "Cannot write active profiles to output: " + output, e );
88 }
89
90 if ( getLog().isInfoEnabled() )
91 {
92 getLog().info( "Active profile report written to: " + output );
93 }
94 }
95 else
96 {
97 if ( getLog().isInfoEnabled() )
98 {
99 getLog().info( message );
100 }
101 }
102 }
103
104
105
106
107
108
109
110
111
112
113
114 private void getActiveProfileStatement( MavenProject project, StringBuilder message )
115 {
116 Map<String, List<String>> activeProfileIds = new LinkedHashMap<String, List<String>>();
117 try
118 {
119 activeProfileIds.putAll( getInjectedProfileIds( project ) );
120 }
121 catch ( UnsupportedOperationException uoe )
122 {
123
124 @SuppressWarnings( "unchecked" )
125 List<Profile> profiles = new ArrayList<Profile>( project.getActiveProfiles() );
126
127 for ( Profile profile : profiles )
128 {
129 List<String> profileIds = activeProfileIds.get( profile.getSource() );
130 if ( profileIds == null )
131 {
132 profileIds = new ArrayList<String>();
133 activeProfileIds.put( profile.getSource(), profileIds );
134 }
135 profileIds.add( profile.getId() );
136 }
137 }
138
139
140 message.append( "\n" );
141
142 message.append( "Active Profiles for Project \'" + project.getId() + "\': \n\n" );
143
144 if ( activeProfileIds.isEmpty() )
145 {
146 message.append( "There are no active profiles." );
147 }
148 else
149 {
150 message.append( "The following profiles are active:\n" );
151
152 for ( Map.Entry<String, List<String>> entry : activeProfileIds.entrySet() )
153 {
154 for ( String profileId : entry.getValue() )
155 {
156 message.append( "\n - " ).append( profileId );
157 message.append( " (source: " ).append( entry.getKey() ).append( ")" );
158 }
159 }
160 }
161
162 message.append( "\n" );
163 }
164
165 @SuppressWarnings( "unchecked" )
166 private Map<String, List<String>> getInjectedProfileIds( MavenProject project ) throws UnsupportedOperationException
167 {
168 try
169 {
170
171 Method getInjectedProfileIdsMethod = MavenProject.class.getMethod( "getInjectedProfileIds" );
172 return (Map<String, List<String>>) getInjectedProfileIdsMethod.invoke( project );
173 }
174 catch ( SecurityException e )
175 {
176 throw new UnsupportedOperationException( e.getMessage(), e );
177 }
178 catch ( NoSuchMethodException e )
179 {
180 throw new UnsupportedOperationException( e.getMessage(), e );
181 }
182 catch ( IllegalArgumentException e )
183 {
184 throw new UnsupportedOperationException( e.getMessage(), e );
185 }
186 catch ( IllegalAccessException e )
187 {
188 throw new UnsupportedOperationException( e.getMessage(), e );
189 }
190 catch ( InvocationTargetException e )
191 {
192 throw new UnsupportedOperationException( e.getMessage(), e );
193 }
194 }
195 }