@@ -148,6 +148,82 @@ public class ExecMojo
148
148
@ Parameter ( property = "exec.outputFile" )
149
149
private File outputFile ;
150
150
151
+ /**
152
+ * When enabled, program standard and error output will be redirected to the
153
+ * Maven logger as <i>Info</i> and <i>Error</i> level logs, respectively. If not enabled the
154
+ * traditional behavior of program output being directed to standard System.out
155
+ * and System.err is used.<br>
156
+ * <br>
157
+ * NOTE: When enabled, to log the program standard out as Maven <i>Debug</i> level instead of
158
+ * <i>Info</i> level use {@code exec.quietLogs=true}. <br>
159
+ * <br>
160
+ * This option can be extremely helpful when combined with multithreaded builds
161
+ * for two reasons:<br>
162
+ * <ul>
163
+ * <li>Program output is suffixed with the owning thread name, making it easier
164
+ * to trace execution of a specific projects build thread.</li>
165
+ * <li>Program output will not get jumbled with other maven log messages.</li>
166
+ * </ul>
167
+ *
168
+ * For Example, if using {@code exec:exec} to run a script to echo a count from
169
+ * 1 to 100 as:
170
+ *
171
+ * <pre>
172
+ * for i in {1..100}
173
+ * do
174
+ * echo "${project.artifactId} - $i"
175
+ * done
176
+ * </pre>
177
+ *
178
+ * When this script is run multi-threaded on two modules, {@code module1} and
179
+ * {@code module2}, you might get output such as:
180
+ *
181
+ * <pre>
182
+ * [BuilderThread 1] [INFO] --- exec-maven-plugin:1.6.0:exec (test) @ module1 ---
183
+ * [BuilderThread 2] [INFO] --- exec-maven-plugin:1.6.0:exec (test) @ module2 ---
184
+ * ...
185
+ * module2 - 98
186
+ * modu
187
+ * module1 - 97
188
+ * module1 -
189
+ * le2 - 9899
190
+ * ...
191
+ * </pre>
192
+ *
193
+ * With this flag enabled, the output will instead come something similar to:
194
+ *
195
+ * <pre>
196
+ * ...
197
+ * [Exec Stream Pumper] [INFO] [BuilderThread 2] module2 - 98
198
+ * [Exec Stream Pumper] [INFO] [BuilderThread 1] module1 - 97
199
+ * [Exec Stream Pumper] [INFO] [BuilderThread 1] module1 - 98
200
+ * [Exec Stream Pumper] [INFO] [BuilderThread 2] module2 - 99
201
+ * ...
202
+ * </pre>
203
+ *
204
+ * NOTE 1: To show the thread in the Maven log, configure the Maven
205
+ * installations <i>conf/logging/simplelogger.properties</i> option:
206
+ * {@code org.slf4j.simpleLogger.showThreadName=true}<br>
207
+ *
208
+ * NOTE 2: This option is ignored when {@code exec.outputFile} is specified.
209
+ *
210
+ * @since 3.0.0
211
+ * @see java.lang.System#err
212
+ * @see java.lang.System#in
213
+ */
214
+ @ Parameter ( property = "exec.useMavenLogger" , defaultValue = "false" )
215
+ private boolean useMavenLogger ;
216
+
217
+ /**
218
+ * When combined with {@code exec.useMavenLogger=true}, prints all executed
219
+ * program output at debug level instead of the default info level to the Maven
220
+ * logger.
221
+ *
222
+ * @since 3.0.0
223
+ */
224
+ @ Parameter ( property = "exec.quietLogs" , defaultValue = "false" )
225
+ private boolean quietLogs ;
226
+
151
227
/**
152
228
* <p>
153
229
* A list of arguments passed to the {@code executable}, which should be of type <code><argument></code> or
@@ -340,6 +416,42 @@ else if ( !StringUtils.isEmpty( argsProp ) )
340
416
IOUtil .close ( outputStream );
341
417
}
342
418
}
419
+ else if (useMavenLogger )
420
+ {
421
+ getLog ().debug ("Will redirect program output to Maven logger" );
422
+ final String parentThreadName = Thread .currentThread ().getName ();
423
+ final String logSuffix = "[" + parentThreadName + "] " ;
424
+ Invokable <String > mavenOutRedirect = new Invokable <String >()
425
+ {
426
+
427
+ @ Override
428
+ public void accept (String logMessage )
429
+ {
430
+ if (quietLogs )
431
+ {
432
+ getLog ().debug (logSuffix + logMessage );
433
+ }
434
+ else
435
+ {
436
+ getLog ().info (logSuffix + logMessage );
437
+ }
438
+ }
439
+ };
440
+ Invokable <String > mavenErrRedirect = new Invokable <String >()
441
+ {
442
+
443
+ @ Override
444
+ public void accept (String logMessage )
445
+ {
446
+ getLog ().error (logSuffix + logMessage );
447
+ }
448
+ };
449
+
450
+ try (OutputStream out = new LineRedirectOutputStream (mavenOutRedirect );
451
+ OutputStream err = new LineRedirectOutputStream (mavenErrRedirect )) {
452
+ resultCode = executeCommandLine (exec , commandLine , enviro , out , err );
453
+ }
454
+ }
343
455
else
344
456
{
345
457
resultCode = executeCommandLine ( exec , commandLine , enviro , System .out , System .err );
0 commit comments