-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
CompileOptions.java
586 lines (512 loc) · 18.2 KB
/
CompileOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks.compile;
import com.google.common.collect.ImmutableList;
import org.gradle.api.Incubating;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.model.ReplacedBy;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.Console;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.internal.deprecation.DeprecationLogger;
import org.gradle.process.CommandLineArgumentProvider;
import org.gradle.util.internal.CollectionUtils;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Main options for Java compilation.
*/
public abstract class CompileOptions extends AbstractOptions {
private static final long serialVersionUID = 0;
private boolean failOnError = true;
private boolean verbose;
private boolean listFiles;
private boolean deprecation;
private boolean warnings = true;
private String encoding;
private boolean debug = true;
private DebugOptions debugOptions;
private boolean fork;
private ForkOptions forkOptions;
private FileCollection bootstrapClasspath;
private String extensionDirs;
private List<String> compilerArgs = new ArrayList<>();
private final List<CommandLineArgumentProvider> compilerArgumentProviders = new ArrayList<>();
private boolean incremental = true;
private FileCollection sourcepath;
private FileCollection annotationProcessorPath;
private final Property<Boolean> incrementalAfterFailure;
private final Property<String> javaModuleVersion;
private final Property<String> javaModuleMainClass;
private final Property<Integer> release;
private final DirectoryProperty generatedSourceOutputDirectory;
private final DirectoryProperty headerOutputDirectory;
@Inject
public CompileOptions(ObjectFactory objectFactory) {
this.javaModuleVersion = objectFactory.property(String.class);
this.javaModuleMainClass = objectFactory.property(String.class);
this.generatedSourceOutputDirectory = objectFactory.directoryProperty();
this.headerOutputDirectory = objectFactory.directoryProperty();
this.release = objectFactory.property(Integer.class);
this.incrementalAfterFailure = objectFactory.property(Boolean.class);
this.forkOptions = objectFactory.newInstance(ForkOptions.class);
this.debugOptions = new DebugOptions();
}
/**
* Tells whether to fail the build when compilation fails. Defaults to {@code true}.
*/
@Input
public boolean isFailOnError() {
return failOnError;
}
/**
* Sets whether to fail the build when compilation fails. Defaults to {@code true}.
*/
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}
/**
* Tells whether to produce verbose output. Defaults to {@code false}.
*/
@Console
public boolean isVerbose() {
return verbose;
}
/**
* Sets whether to produce verbose output. Defaults to {@code false}.
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/**
* Tells whether to log the files to be compiled. Defaults to {@code false}.
*/
@Console
public boolean isListFiles() {
return listFiles;
}
/**
* Sets whether to log the files to be compiled. Defaults to {@code false}.
*/
public void setListFiles(boolean listFiles) {
this.listFiles = listFiles;
}
/**
* Tells whether to log details of usage of deprecated members or classes. Defaults to {@code false}.
*/
@Console
public boolean isDeprecation() {
return deprecation;
}
/**
* Sets whether to log details of usage of deprecated members or classes. Defaults to {@code false}.
*/
public void setDeprecation(boolean deprecation) {
this.deprecation = deprecation;
}
/**
* Tells whether to log warning messages. The default is {@code true}.
*/
@Console
public boolean isWarnings() {
return warnings;
}
/**
* Sets whether to log warning messages. The default is {@code true}.
*/
public void setWarnings(boolean warnings) {
this.warnings = warnings;
}
/**
* Returns the character encoding to be used when reading source files. Defaults to {@code null}, in which
* case the platform default encoding will be used.
*/
@Nullable
@Optional
@Input
public String getEncoding() {
return encoding;
}
/**
* Sets the character encoding to be used when reading source files. Defaults to {@code null}, in which
* case the platform default encoding will be used.
*/
public void setEncoding(@Nullable String encoding) {
this.encoding = encoding;
}
/**
* Tells whether to include debugging information in the generated class files. Defaults
* to {@code true}. See {@link DebugOptions#getDebugLevel()} for which debugging information will be generated.
*/
@Input
public boolean isDebug() {
return debug;
}
/**
* Sets whether to include debugging information in the generated class files. Defaults
* to {@code true}. See {@link DebugOptions#getDebugLevel()} for which debugging information will be generated.
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/**
* Returns options for generating debugging information.
*/
@Nested
public DebugOptions getDebugOptions() {
return debugOptions;
}
/**
* Sets options for generating debugging information.
*/
public void setDebugOptions(DebugOptions debugOptions) {
this.debugOptions = debugOptions;
}
/**
* Tells whether to run the compiler in its own process. Note that this does
* not necessarily mean that a new process will be created for each compile task.
* Defaults to {@code false}.
*/
@Input
public boolean isFork() {
return fork;
}
/**
* Sets whether to run the compiler in its own process. Note that this does
* not necessarily mean that a new process will be created for each compile task.
* Defaults to {@code false}.
*/
public void setFork(boolean fork) {
this.fork = fork;
}
/**
* Returns options for running the compiler in a child process.
*/
@Nested
public ForkOptions getForkOptions() {
return forkOptions;
}
/**
* Sets options for running the compiler in a child process.
*/
public void setForkOptions(ForkOptions forkOptions) {
this.forkOptions = forkOptions;
}
/**
* Returns the bootstrap classpath to be used for the compiler process. Defaults to {@code null}.
*
* @since 4.3
*/
@Nullable
@Optional
@CompileClasspath
public FileCollection getBootstrapClasspath() {
return bootstrapClasspath;
}
/**
* Sets the bootstrap classpath to be used for the compiler process. Defaults to {@code null}.
*
* @since 4.3
*/
public void setBootstrapClasspath(@Nullable FileCollection bootstrapClasspath) {
this.bootstrapClasspath = bootstrapClasspath;
}
/**
* Returns the extension dirs to be used for the compiler process. Defaults to {@code null}.
*/
@Nullable
@Optional
@Input
public String getExtensionDirs() {
return extensionDirs;
}
/**
* Sets the extension dirs to be used for the compiler process. Defaults to {@code null}.
*/
public void setExtensionDirs(@Nullable String extensionDirs) {
this.extensionDirs = extensionDirs;
}
/**
* Returns any additional arguments to be passed to the compiler.
* Defaults to the empty list.
*
* Compiler arguments not supported by the DSL can be added here.
*
* For example, it is possible to pass the {@code --enable-preview} option that was added in newer Java versions:
* <pre><code>compilerArgs.add("--enable-preview")</code></pre>
*
* Note that if {@code --release} is added then {@code -target} and {@code -source}
* are ignored.
*/
@Input
public List<String> getCompilerArgs() {
return compilerArgs;
}
/**
* Returns all compiler arguments, added to the {@link #getCompilerArgs()} or the {@link #getCompilerArgumentProviders()} property.
*
* @since 4.5
*/
@Internal
public List<String> getAllCompilerArgs() {
ImmutableList.Builder<String> builder = ImmutableList.builder();
builder.addAll(CollectionUtils.stringize(getCompilerArgs()));
for (CommandLineArgumentProvider compilerArgumentProvider : getCompilerArgumentProviders()) {
builder.addAll(compilerArgumentProvider.asArguments());
}
return builder.build();
}
/**
* Compiler argument providers.
*
* @since 4.5
*/
@Nested
public List<CommandLineArgumentProvider> getCompilerArgumentProviders() {
return compilerArgumentProviders;
}
/**
* Sets any additional arguments to be passed to the compiler.
* Defaults to the empty list.
*/
public void setCompilerArgs(List<String> compilerArgs) {
this.compilerArgs = compilerArgs;
}
/**
* Convenience method to set {@link ForkOptions} with named parameter syntax.
* Calling this method will set {@code fork} to {@code true}.
*/
public CompileOptions fork(Map<String, Object> forkArgs) {
fork = true;
forkOptions.define(forkArgs);
return this;
}
/**
* Convenience method to set {@link DebugOptions} with named parameter syntax.
* Calling this method will set {@code debug} to {@code true}.
*/
public CompileOptions debug(Map<String, Object> debugArgs) {
debug = true;
debugOptions.define(debugArgs);
return this;
}
/**
* Configure the java compilation to be incremental (e.g. compiles only those java classes that were changed or that are dependencies to the changed classes).
*/
public CompileOptions setIncremental(boolean incremental) {
this.incremental = incremental;
return this;
}
/**
* informs whether to use incremental compilation feature. See {@link #setIncremental(boolean)}
*/
@Internal
public boolean isIncremental() {
return incremental;
}
/**
* Used to enable or disable incremental compilation after a failure.
* <p>
* By default, incremental compilation after a failure is enabled for Java and Groovy.
* It has no effect for Scala. It has no effect if incremental compilation is not enabled.
* <p>
* When the Java command line compiler is used, i.e. when a custom java home is passed to forkOptions.javaHome or javac is passed to forkOptions.executable,
* this optimization is automatically disabled, since the compiler is not invoked via the compiler API.
*
* @since 7.6
*/
@Input
@Optional
@Incubating
public Property<Boolean> getIncrementalAfterFailure() {
return incrementalAfterFailure;
}
/**
* The source path to use for the compilation.
* <p>
* The source path indicates the location of source files that <i>may</i> be compiled if necessary.
* It is effectively a complement to the class path, where the classes to be compiled against are in source form.
* It does <b>not</b> indicate the actual primary source being compiled.
* <p>
* The source path feature of the Java compiler is rarely needed for modern builds that use dependency management.
* <p>
* The default value for the source path is {@code null}, which indicates an <i>empty</i> source path.
* Note that this is different to the default value for the {@code -sourcepath} option for {@code javac}, which is to use the value specified by {@code -classpath}.
* If you wish to use any source path, it must be explicitly set.
*
* @return the source path
* @see #setSourcepath(FileCollection)
*/
@Optional
@Nullable
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
@InputFiles
public FileCollection getSourcepath() {
return sourcepath;
}
/**
* Sets the source path to use for the compilation.
*
* @param sourcepath the source path
*/
public void setSourcepath(@Nullable FileCollection sourcepath) {
this.sourcepath = sourcepath;
}
/**
* Returns the classpath to use to load annotation processors. This path is also used for annotation processor discovery.
*
* @return The annotation processor path, or {@code null} if annotation processing is disabled.
* @since 3.4
*/
@Nullable
@Optional
@Classpath
public FileCollection getAnnotationProcessorPath() {
return annotationProcessorPath;
}
/**
* Set the classpath to use to load annotation processors. This path is also used for annotation processor discovery.
*
* @param annotationProcessorPath The annotation processor path, or {@code null} to disable annotation processing.
* @since 3.4
*/
public void setAnnotationProcessorPath(@Nullable FileCollection annotationProcessorPath) {
this.annotationProcessorPath = annotationProcessorPath;
}
/**
* Configures the Java language version for this compile task ({@code --release} compiler flag).
* <p>
* If set, it will take precedences over the {@link AbstractCompile#getSourceCompatibility()} and {@link AbstractCompile#getTargetCompatibility()} settings.
* <p>
* This option is only taken into account by the {@link org.gradle.api.tasks.compile.JavaCompile} task.
*
* @since 6.6
*/
@SuppressWarnings("JavadocReference")
@Input
@Optional
public Property<Integer> getRelease() {
return release;
}
/**
* Set the version of the Java module.
*
* @since 6.4
*/
@Optional
@Input
public Property<String> getJavaModuleVersion() {
return javaModuleVersion;
}
/**
* Set the main class of the Java module, if the module is supposed to be executable.
*
* @since 6.4
*/
@Optional
@Input
public Property<String> getJavaModuleMainClass() {
return javaModuleMainClass;
}
/**
* Returns the directory to place source files generated by annotation processors.
*
* @since 6.3
*/
@Optional
@OutputDirectory
public DirectoryProperty getGeneratedSourceOutputDirectory() {
return generatedSourceOutputDirectory;
}
/**
* Returns the directory to place source files generated by annotation processors.
*
* @since 4.3
*
* @deprecated Use {@link #getGeneratedSourceOutputDirectory()} instead. This method will be removed in Gradle 9.0.
*/
@Nullable
@Deprecated
@ReplacedBy("generatedSourceOutputDirectory")
public File getAnnotationProcessorGeneratedSourcesDirectory() {
DeprecationLogger.deprecateProperty(CompileOptions.class, "annotationProcessorGeneratedSourcesDirectory")
.replaceWith("generatedSourceOutputDirectory")
.willBeRemovedInGradle9()
.withDslReference()
.nagUser();
return generatedSourceOutputDirectory.getAsFile().getOrNull();
}
/**
* Sets the directory to place source files generated by annotation processors.
*
* @since 4.3
*
* @deprecated Use {@link #getGeneratedSourceOutputDirectory()}.set() instead. This method will be removed in Gradle 9.0.
*/
@Deprecated
public void setAnnotationProcessorGeneratedSourcesDirectory(@Nullable File file) {
DeprecationLogger.deprecateProperty(CompileOptions.class, "annotationProcessorGeneratedSourcesDirectory")
.replaceWith("generatedSourceOutputDirectory")
.willBeRemovedInGradle9()
.withDslReference()
.nagUser();
this.generatedSourceOutputDirectory.set(file);
}
/**
* Sets the directory to place source files generated by annotation processors.
*
* @since 4.3
*
* @deprecated Use {@link #getGeneratedSourceOutputDirectory()}.set() instead. This method will be removed in Gradle 9.0.
*/
@Deprecated
public void setAnnotationProcessorGeneratedSourcesDirectory(Provider<File> file) {
DeprecationLogger.deprecateProperty(CompileOptions.class, "annotationProcessorGeneratedSourcesDirectory")
.replaceWith("generatedSourceOutputDirectory")
.willBeRemovedInGradle9()
.withDslReference()
.nagUser();
this.generatedSourceOutputDirectory.fileProvider(file);
}
/**
* If this option is set to a non-null directory, it will be passed to the Java compiler's `-h` option, prompting it to generate native headers to that directory.
*
* @since 4.10
*/
@Optional
@OutputDirectory
public DirectoryProperty getHeaderOutputDirectory() {
return headerOutputDirectory;
}
}