77import org .codehaus .groovy .runtime .DefaultGroovyMethods ;
88import org .gradle .api .DefaultTask ;
99import org .gradle .api .Transformer ;
10- import org .gradle .api .file .ConfigurableFileCollection ;
11- import org .gradle .api .file .DirectoryProperty ;
12- import org .gradle .api .file .FileSystemLocation ;
13- import org .gradle .api .file .FileSystemLocationProperty ;
10+ import org .gradle .api .file .*;
1411import org .gradle .api .logging .LogLevel ;
1512import org .gradle .api .logging .LoggingManager ;
1613import org .gradle .api .model .ObjectFactory ;
1916import org .gradle .api .provider .Property ;
2017import org .gradle .api .provider .Provider ;
2118import org .gradle .api .provider .ProviderFactory ;
22- import org .gradle .api .tasks .Classpath ;
19+ import org .gradle .api .tasks .* ;
2320import org .gradle .api .tasks .Console ;
24- import org .gradle .api .tasks .Input ;
25- import org .gradle .api .tasks .InputFiles ;
26- import org .gradle .api .tasks .Internal ;
27- import org .gradle .api .tasks .JavaExec ;
28- import org .gradle .api .tasks .Nested ;
29- import org .gradle .api .tasks .Optional ;
30- import org .gradle .api .tasks .TaskAction ;
3121import org .gradle .jvm .toolchain .JavaLanguageVersion ;
3222import org .gradle .jvm .toolchain .JavaLauncher ;
3323import org .gradle .jvm .toolchain .JavaToolchainService ;
3929import org .jetbrains .annotations .UnknownNullability ;
4030
4131import javax .inject .Inject ;
42- import java .io .File ;
32+ import java .io .* ;
4333import java .util .ArrayList ;
4434import java .util .HashMap ;
4535import java .util .List ;
4636import java .util .Locale ;
4737import java .util .Map ;
4838import java .util .concurrent .Callable ;
39+ import java .util .stream .Collectors ;
4940
5041/// This tool execution task is a template on top of [JavaExec] to make executing [tools][Tool] much easier and more
5142/// consistent between plugins.
@@ -79,6 +70,8 @@ public abstract class ToolExecBase<P extends EnhancedProblems> extends DefaultTa
7970 protected abstract @ Nested Property <JavaLauncher > getToolchainLauncher ();
8071
8172 public abstract @ Input @ Optional Property <Boolean > getPreferToolchainJvm ();
73+
74+ public abstract @ Internal DirectoryProperty getWorkingDir ();
8275 //endregion
8376
8477 //region Logging
@@ -90,6 +83,8 @@ public abstract class ToolExecBase<P extends EnhancedProblems> extends DefaultTa
9083 protected abstract @ Console Property <LogLevel > getStandardOutputLogLevel ();
9184
9285 protected abstract @ Console Property <LogLevel > getStandardErrorLogLevel ();
86+
87+ protected abstract @ OutputFile RegularFileProperty getLogFile ();
9388 //endregion
9489
9590 protected abstract @ Inject ObjectFactory getObjects ();
@@ -127,6 +122,9 @@ protected ToolExecBase(Tool tool) {
127122
128123 this .getStandardOutputLogLevel ().convention (LogLevel .LIFECYCLE );
129124 this .getStandardErrorLogLevel ().convention (LogLevel .ERROR );
125+
126+ this .getWorkingDir ().convention (this .getDefaultOutputDirectory ());
127+ this .getLogFile ().convention (this .getDefaultLogFile ());
130128 }
131129
132130 /// The enhanced problems instance to use for this task.
@@ -153,7 +151,7 @@ protected void addArguments() { }
153151 /// @implNote Not invoking this method from an overriding method *will* result in the tool never being executed and
154152 /// [#addArguments()] never being run.
155153 @ TaskAction
156- protected ExecResult exec () {
154+ protected ExecResult exec () throws IOException {
157155 this .args = new ArrayList <>();
158156 this .jvmArgs = new ArrayList <>();
159157 this .environment = new HashMap <>();
@@ -180,20 +178,46 @@ protected ExecResult exec() {
180178 javaLauncher = getJavaLauncher ().get ();
181179 }
182180
183- return this .getExecOperations ().javaexec (spec -> {
184- spec .setIgnoreExitValue (true );
185-
186- spec .setClasspath (this .getClasspath ());
187- spec .getMainClass ().set (this .getMainClass ());
188- spec .setExecutable (javaLauncher .getExecutablePath ().getAsFile ().getAbsolutePath ());
189- spec .setArgs (args );
190- spec .setJvmArgs (jvmArgs );
191- spec .setEnvironment (this .environment );
192- spec .setSystemProperties (this .systemProperties );
193-
194- spec .setStandardOutput (SharedUtil .toLog (this .getLogger (), stdOutLevel ));
195- spec .setErrorOutput (SharedUtil .toLog (this .getLogger (), stdErrLevel ));
196- });
181+ final File workingDirectory = this .getWorkingDir ().get ().getAsFile ();
182+ if (!workingDirectory .exists ())
183+ workingDirectory .mkdirs ();
184+
185+ try (PrintWriter log = new PrintWriter (new FileWriter (this .getLogFile ().get ().getAsFile ()), true )) {
186+ return this .getExecOperations ().javaexec (spec -> {
187+ spec .setIgnoreExitValue (true );
188+
189+ spec .setWorkingDir (this .getWorkingDir ());
190+ spec .setClasspath (this .getClasspath ());
191+ spec .getMainClass ().set (this .getMainClass ());
192+ spec .setExecutable (javaLauncher .getExecutablePath ().getAsFile ().getAbsolutePath ());
193+ spec .setArgs (args );
194+ spec .setJvmArgs (jvmArgs );
195+ spec .setEnvironment (this .environment );
196+ spec .setSystemProperties (this .systemProperties );
197+
198+ spec .setStandardOutput (SharedUtil .toLog (
199+ line -> {
200+ this .getLogger ().log (stdOutLevel , line );
201+ log .println (line );
202+ }
203+ ));
204+ spec .setErrorOutput (SharedUtil .toLog (
205+ line -> {
206+ this .getLogger ().log (stdErrLevel , line );
207+ log .println (line );
208+ }
209+ ));
210+
211+ log .println ("Java Launcher: " + spec .getExecutable ());
212+ log .println ("Working directory: " + workingDirectory .getAbsolutePath ());
213+ log .println ("Main class: " + this .getMainClass ().get ());
214+ log .println ("Arguments: " + args .stream ().collect (Collectors .joining (", " , "'" , "'" )));
215+ log .println ("JVM Arguments: " + jvmArgs .stream ().collect (Collectors .joining (", " , "'" , "'" )));
216+ log .println ("Classpath:" );
217+ getClasspath ().forEach (f -> log .println (" - " + f .getAbsolutePath ()));
218+ log .println ("====================================" );
219+ });
220+ }
197221 }
198222
199223 @ SuppressWarnings ("DataFlowIssue" )
0 commit comments