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.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang3.time.DateFormatUtils;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.project.MavenProject;
31
32
33
34
35
36
37
38 @Mojo( name = "active-profiles", aggregator = true )
39 public class ActiveProfilesMojo
40 extends AbstractHelpMojo
41 {
42
43
44
45
46
47
48
49 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
50 private List<MavenProject> projects;
51
52
53
54
55
56
57 public void execute()
58 throws MojoExecutionException
59 {
60 StringBuilder message = new StringBuilder();
61
62 for ( MavenProject project : projects )
63 {
64 getActiveProfileStatement( project, message );
65
66 message.append( LS ).append( LS );
67 }
68
69 if ( output != null )
70 {
71 String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
72 .format( System.currentTimeMillis() );
73 StringBuilder sb = new StringBuilder();
74 sb.append( "Created by: " ).append( getClass().getName() ).append( LS );
75 sb.append( "Created on: " ).append( formattedDateTime ).append( LS ).append( LS );
76 sb.append( message.toString() );
77
78 try
79 {
80 writeFile( output, sb );
81 }
82 catch ( IOException e )
83 {
84 throw new MojoExecutionException( "Cannot write active profiles to output: " + output, e );
85 }
86
87 getLog().info( "Active profile report written to: " + output );
88 }
89 else
90 {
91 getLog().info( message );
92 }
93 }
94
95
96
97
98
99
100
101
102
103
104
105 private void getActiveProfileStatement( MavenProject project, StringBuilder message )
106 {
107 Map<String, List<String>> activeProfileIds = project.getInjectedProfileIds();
108
109 message.append( LS );
110 message.append( "Active Profiles for Project \'" ).append( project.getId() ).append( "\':" );
111 message.append( LS ).append( LS );
112
113 if ( activeProfileIds.isEmpty() )
114 {
115 message.append( "There are no active profiles." );
116 }
117 else
118 {
119 message.append( "The following profiles are active:" ).append( LS );
120
121 for ( Map.Entry<String, List<String>> entry : activeProfileIds.entrySet() )
122 {
123 for ( String profileId : entry.getValue() )
124 {
125 message.append( LS ).append( " - " ).append( profileId );
126 message.append( " (source: " ).append( entry.getKey() ).append( ")" );
127 }
128 }
129 }
130
131 message.append( LS );
132 }
133
134 }