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