下面列出了怎么用org.gradle.api.tasks.compile.AbstractCompile的API类实例代码及写法,或者点击链接到github查看源代码。
private List<String> buildCompilerArgs(JavaCompile javaCompile) {
var patchModuleContainer = PatchModuleContainer.copyOf(
helper().modularityExtension().optionContainer().getPatchModuleContainer());
String moduleName = helper().moduleName();
new MergeClassesHelper(project).otherCompileTaskStream()
.map(AbstractCompile::getDestinationDir)
.forEach(dir -> patchModuleContainer.addDir(moduleName, dir.getAbsolutePath()));
var compilerArgs = new ArrayList<>(javaCompile.getOptions().getCompilerArgs());
patchModuleContainer.buildModulePathOption(compileJavaClasspath)
.ifPresent(option -> option.mutateArgs(compilerArgs));
patchModuleContainer.mutator(compileJavaClasspath).mutateArgs(compilerArgs);
moduleOptions.mutateArgs(compilerArgs);
MutatorHelper.configureModuleVersion(helper(), compilerArgs);
return compilerArgs;
}
/**
* Preconfigures the specified compile task based on the specified source set and class directory binary.
*
* @param compile the compile task to be preconfigured
* @param sourceSet the source set for the compile task
* @param binary the binary for the compile task
*/
public void configureCompileTask(AbstractCompile compile, final JavaSourceSet sourceSet, final ClassDirectoryBinarySpec binary) {
compile.setDescription(String.format("Compiles %s.", sourceSet));
compile.setSource(sourceSet.getSource());
compile.dependsOn(sourceSet);
ConventionMapping conventionMapping = compile.getConventionMapping();
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath().getFiles();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return binary.getClassesDir();
}
});
}
public void configureForSourceSet(final SourceSet sourceSet, AbstractCompile compile) {
ConventionMapping conventionMapping;
compile.setDescription(String.format("Compiles the %s.", sourceSet.getJava()));
conventionMapping = compile.getConventionMapping();
compile.setSource(sourceSet.getJava());
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
}
/**
* Preconfigures the specified compile task based on the specified source set and class directory binary.
*
* @param compile the compile task to be preconfigured
* @param sourceSet the source set for the compile task
* @param binary the binary for the compile task
*/
public void configureCompileTask(AbstractCompile compile, final JavaSourceSet sourceSet, final ClassDirectoryBinary binary) {
compile.setDescription(String.format("Compiles %s.", sourceSet));
compile.setSource(sourceSet.getSource());
compile.dependsOn(sourceSet);
ConventionMapping conventionMapping = compile.getConventionMapping();
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath().getFiles();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return binary.getClassesDir();
}
});
}
public void configureForSourceSet(final SourceSet sourceSet, AbstractCompile compile) {
ConventionMapping conventionMapping;
compile.setDescription(String.format("Compiles the %s.", sourceSet.getJava()));
conventionMapping = compile.getConventionMapping();
compile.setSource(sourceSet.getJava());
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
}
public void configureForSourceSet(final SourceSet sourceSet, AbstractCompile compile) {
ConventionMapping conventionMapping;
compile.setDescription("Compiles the " + sourceSet.getJava() + ".");
conventionMapping = compile.getConventionMapping();
compile.setSource(sourceSet.getJava());
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
}
/**
* Makes the given task the one used by top-level "compile" task.
*/
public static void setJavaCompilerTask(
@NonNull AndroidTask<? extends AbstractCompile> javaCompilerTask,
@NonNull TaskFactory tasks,
@NonNull VariantScope scope) {
scope.getCompileTask().dependsOn(tasks, javaCompilerTask);
scope.setJavaCompilerTask(javaCompilerTask);
// TODO: Get rid of it once we stop keeping tasks in variant data.
//noinspection VariableNotUsedInsideIf
if (scope.getVariantData().javacTask != null) {
// This is not the experimental plugin, let's update variant data, so Variants API
// keeps working.
scope.getVariantData().javaCompilerTask = (AbstractCompile) tasks.named(javaCompilerTask.getName());
}
}
private AspectJCompileSpec createSpec(AbstractCompile compile) {
AspectJCompileSpec spec = new AspectJCompileSpec();
spec.setDestinationDir(compile.getDestinationDir());
spec.setWorkingDir(compile.getProject().getProjectDir());
spec.setTempDir(compile.getTemporaryDir());
spec.setCompileClasspath(new ArrayList<>(compile.getClasspath().filter(File::exists).getFiles()));
spec.setTargetCompatibility(compile.getTargetCompatibility());
spec.setSourceCompatibility(compile.getSourceCompatibility());
spec.setAspectJClasspath(getClasspath());
spec.setAspectJCompileOptions(getOptions());
spec.setAdditionalInpath(getAdditionalInpath());
return spec;
}
private AspectJCompileSpec createSpec(AbstractCompile compile) {
AspectJCompileSpec spec = new AspectJCompileSpec();
spec.setDestinationDir(compile.getDestinationDir());
spec.setWorkingDir(compile.getProject().getProjectDir());
spec.setTempDir(compile.getTemporaryDir());
spec.setCompileClasspath(new ArrayList<>(compile.getClasspath().filter(File::exists).getFiles()));
spec.setTargetCompatibility(compile.getTargetCompatibility());
spec.setSourceCompatibility(compile.getSourceCompatibility());
spec.setAspectJClasspath(getClasspath());
spec.setAspectJCompileOptions(getOptions());
spec.setAdditionalInpath(getAdditionalInpath());
return spec;
}
/**
* Preconfigures the specified compile task based on the specified source set and class directory binary.
*
* @param compile the compile task to be preconfigured
* @param sourceSet the source set for the compile task
* @param binary the binary for the compile task
*/
public void configureCompileTask(AbstractCompile compile, final JavaSourceSet sourceSet, final ClassDirectoryBinarySpec binary) {
compile.setDescription(String.format("Compiles %s.", sourceSet));
compile.setSource(sourceSet.getSource());
compile.dependsOn(sourceSet);
ConventionMapping conventionMapping = compile.getConventionMapping();
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath().getFiles();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return binary.getClassesDir();
}
});
}
public void configureForSourceSet(final SourceSet sourceSet, AbstractCompile compile) {
ConventionMapping conventionMapping;
compile.setDescription(String.format("Compiles the %s.", sourceSet.getJava()));
conventionMapping = compile.getConventionMapping();
compile.setSource(sourceSet.getJava());
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
}
/**
* Preconfigures the specified compile task based on the specified source set and class directory binary.
*
* @param compile the compile task to be preconfigured
* @param sourceSet the source set for the compile task
* @param binary the binary for the compile task
*/
public void configureCompileTask(AbstractCompile compile, final JavaSourceSet sourceSet, final ClassDirectoryBinary binary) {
compile.setDescription(String.format("Compiles %s.", sourceSet));
compile.setSource(sourceSet.getSource());
compile.dependsOn(sourceSet);
ConventionMapping conventionMapping = compile.getConventionMapping();
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath().getFiles();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return binary.getClassesDir();
}
});
}
public void configureForSourceSet(final SourceSet sourceSet, AbstractCompile compile) {
ConventionMapping conventionMapping;
compile.setDescription(String.format("Compiles the %s.", sourceSet.getJava()));
conventionMapping = compile.getConventionMapping();
compile.setSource(sourceSet.getJava());
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getCompileClasspath();
}
});
conventionMapping.map("destinationDir", new Callable<Object>() {
public Object call() throws Exception {
return sourceSet.getOutput().getClassesDir();
}
});
}
public FileCollection getMergeAdjustedClasspath(FileCollection classpath) {
if (!isMergeRequired()) {
return classpath;
}
Set<File> files = new HashSet<>(classpath.getFiles());
allCompileTaskStream().map(AbstractCompile::getDestinationDir).forEach(files::remove);
files.add(helper().getMergedDir());
return project.files(files.toArray());
}
private void configureCompileJava(JavaCompile compileJava) {
project.getConfigurations().stream()
.flatMap(configuration -> configuration.getDependencies().stream())
.filter(dependency -> dependency instanceof ProjectDependency)
.map(dependency -> ((ProjectDependency) dependency).getDependencyProject().getTasks())
.map(tasks -> tasks.findByName(CompileModuleOptions.COMPILE_MODULE_INFO_TASK_NAME))
.filter(Objects::nonNull);
var moduleOptions = compileJava.getExtensions().create("moduleOptions", CompileModuleOptions.class, project);
project.afterEvaluate(p -> {
adjustMainClass(compileJava);
MergeClassesHelper.POST_JAVA_COMPILE_TASK_NAMES.stream()
.map(name -> helper().findTask(name, AbstractCompile.class))
.flatMap(Optional::stream)
.filter(task -> !task.getSource().isEmpty())
.findAny()
.ifPresent(task -> moduleOptions.setCompileModuleInfoSeparately(true));
if (moduleOptions.getCompileModuleInfoSeparately()) {
compileJava.exclude("module-info.java");
} else {
configureModularityForCompileJava(compileJava, moduleOptions);
}
});
}
/**
* Determines the java language level to use and sets it on the given task and
* {@link CompileOptions}. The latter is to propagate the information to Studio.
*/
public static void configureLanguageLevel(
AbstractCompile compileTask,
final CompileOptions compileOptions,
String compileSdkVersion) {
final AndroidVersion hash = AndroidTargetHash.getVersionFromHash(compileSdkVersion);
Integer compileSdkLevel = (hash == null ? null : hash.getApiLevel());
JavaVersion javaVersionToUse;
if (compileSdkLevel == null || (0 <= compileSdkLevel && compileSdkLevel <= 20)) {
javaVersionToUse = JavaVersion.VERSION_1_6;
} else {
javaVersionToUse = JavaVersion.VERSION_1_7;
}
JavaVersion jdkVersion =
JavaVersion.toVersion(System.getProperty("java.specification.version"));
if (jdkVersion.compareTo(javaVersionToUse) < 0) {
compileTask.getLogger().warn(
"Default language level for compileSdkVersion '{}' is " +
"{}, but the JDK used is {}, so the JDK language level will be used.",
compileSdkVersion,
javaVersionToUse,
jdkVersion);
javaVersionToUse = jdkVersion;
}
compileOptions.setDefaultJavaVersion(javaVersionToUse);
compileTask.setSourceCompatibility(compileOptions.getSourceCompatibility().toString());
compileTask.setTargetCompatibility(compileOptions.getTargetCompatibility().toString());
}
@Override
public void execute(Task task) {
if (!enabled.getOrElse(true)) {
return;
}
AspectJCompileSpec spec = createSpec((AbstractCompile) task);
new AspectJCompiler(javaExecHandleFactory).execute(spec);
}
private AjcAction enhanceWithWeavingAction(AbstractCompile abstractCompile, Configuration aspectpath, Configuration inpath, Configuration aspectjConfiguration) {
AjcAction action = project.getObjects().newInstance(AjcAction.class);
action.getOptions().getAspectpath().from(aspectpath);
action.getOptions().getInpath().from(inpath);
action.getAdditionalInpath().from(abstractCompile.getDestinationDir());
action.getClasspath().from(aspectjConfiguration);
action.addToTask(abstractCompile);
return action;
}
@Override
public void execute(Task task) {
if (!enabled.getOrElse(true)) {
return;
}
AspectJCompileSpec spec = createSpec((AbstractCompile) task);
new AspectJCompiler(javaExecHandleFactory).execute(spec);
}
private AjcAction enhanceWithWeavingAction(AbstractCompile abstractCompile, Configuration aspectpath, Configuration inpath, Configuration aspectjConfiguration) {
AjcAction action = project.getObjects().newInstance(AjcAction.class);
action.getOptions().getAspectpath().from(aspectpath);
action.getOptions().getInpath().from(inpath);
action.getAdditionalInpath().from(abstractCompile.getDestinationDir());
action.getClasspath().from(aspectjConfiguration);
action.addToTask(abstractCompile);
return action;
}
/**
* {@inheritDoc}
*/
public void execute(AbstractCompile task) {
if (byteBuddyExtension.implies(task)) {
task.doLast(new TransformationAction(project, byteBuddyExtension, task));
} else {
project.getLogger().info("Skipping non-specified task {}", task.getName());
}
}
public Stream<AbstractCompile> otherCompileTaskStream() {
return otherCompileTaskNameStream()
.map(name -> helper().findTask(name, AbstractCompile.class))
.flatMap(Optional::stream);
}
public Stream<AbstractCompile> allCompileTaskStream() {
return Stream.concat(Stream.of(javaCompileTask()), otherCompileTaskStream());
}
@NonNull
@Override
public AbstractCompile getJavaCompiler() {
return getVariantData().javaCompilerTask;
}
@Nullable
public AndroidTask<? extends AbstractCompile> getJavaCompilerTask() {
return javaCompilerTask;
}
public void setJavaCompilerTask(
@NonNull AndroidTask<? extends AbstractCompile> javaCompileTask) {
this.javaCompilerTask = javaCompileTask;
}
/**
* {@inheritDoc}
*/
public void apply(Project project) {
project.getTasks().withType(AbstractCompile.class, PostCompilationAction.of(project));
}
/**
* Returns the Java Compiler task which can be either javac or jack depending on the project
* configuration.
*/
@NonNull
AbstractCompile getJavaCompiler();
/**
* Creates a post compilation action.
*
* @param project The project to apply the action upon.
* @return An appropriate action.
*/
public static Action<AbstractCompile> of(Project project) {
return new PostCompilationAction(project, project.getExtensions().create("byteBuddy", ByteBuddyExtension.class, project));
}
/**
* Creates a new transformation action.
*
* @param project The current project.
* @param extension The current project's Byte Buddy extension.
* @param task The task to which this transformation was appended.
*/
public TransformationAction(Project project, ByteBuddyExtension extension, AbstractCompile task) {
this.project = project;
this.byteBuddyExtension = extension;
this.task = task;
}