use of com.palantir.baseline.extensions.BaselineModuleJvmArgsExtension in project gradle-baseline by palantir.
the class BaselineModuleJvmArgs method apply.
@Override
@SuppressWarnings("checkstyle:MethodLength")
public void apply(Project project) {
project.getPluginManager().withPlugin("java", unused -> {
BaselineModuleJvmArgsExtension extension = project.getExtensions().create(EXTENSION_NAME, BaselineModuleJvmArgsExtension.class, project);
// javac isn't provided `--add-exports` args for the time being due to
// https://github.com/gradle/gradle/issues/18824
// However, we set sourceCompatibility in BaselineJavaVersion to opt out of the '--release' flag.
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {
TaskProvider<JavaCompile> javaCompileProvider = project.getTasks().named(sourceSet.getCompileJavaTaskName(), JavaCompile.class);
javaCompileProvider.configure(javaCompile -> {
javaCompile.getOptions().getCompilerArgumentProviders().add(new CommandLineArgumentProvider() {
@Override
public Iterable<String> asArguments() {
// The '--release' flag is set when BaselineJavaVersion is not used.
if (!project.getPlugins().hasPlugin(BaselineJavaVersion.class)) {
project.getLogger().debug("BaselineModuleJvmArgs not applying args to compilation task " + "{} on {} due to lack of BaselineJavaVersion", javaCompile.getName(), project);
return ImmutableList.of();
}
ImmutableList<String> arguments = collectCompilationArgs(project, extension, sourceSet);
project.getLogger().debug("BaselineModuleJvmArgs compiling {} on {} with exports: {}", javaCompile.getName(), project, arguments);
return arguments;
}
});
});
TaskProvider<Task> javadocTaskProvider = null;
try {
javadocTaskProvider = project.getTasks().named(sourceSet.getJavadocTaskName());
} catch (UnknownTaskException e) {
// skip
}
if (javadocTaskProvider != null) {
javadocTaskProvider.configure(javadocTask -> {
javadocTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
// The '--release' flag is set when BaselineJavaVersion is not used.
if (!project.getPlugins().hasPlugin(BaselineJavaVersion.class)) {
project.getLogger().debug("BaselineModuleJvmArgs not applying args to compilation task " + "{} on {} due to lack of BaselineJavaVersion", task.getName(), project);
return;
}
Javadoc javadoc = (Javadoc) task;
MinimalJavadocOptions options = javadoc.getOptions();
if (options instanceof CoreJavadocOptions) {
CoreJavadocOptions coreOptions = (CoreJavadocOptions) options;
ImmutableList<JarManifestModuleInfo> info = collectClasspathInfo(project, sourceSet);
List<String> exportValues = Stream.concat(// Compilation only supports exports, so we union with opens.
Stream.concat(extension.exports().get().stream(), extension.opens().get().stream()), info.stream().flatMap(item -> Stream.concat(item.exports().stream(), item.opens().stream()))).distinct().sorted().map(item -> item + "=ALL-UNNAMED").collect(ImmutableList.toImmutableList());
project.getLogger().debug("BaselineModuleJvmArgs building {} on {} " + "with exports: {}", javadoc.getName(), project, exportValues);
if (!exportValues.isEmpty()) {
coreOptions.addMultilineStringsOption("-add-exports").setValue(exportValues);
}
} else {
project.getLogger().error("MinimalJavadocOptions implementation was " + "not CoreJavadocOptions, rather '{}'", options.getClass().getName());
}
}
});
});
}
});
project.getTasks().withType(Test.class).configureEach(new Action<Test>() {
@Override
public void execute(Test test) {
test.getJvmArgumentProviders().add(new CommandLineArgumentProvider() {
@Override
public Iterable<String> asArguments() {
ImmutableList<String> arguments = collectClasspathArgs(project, extension, test.getClasspath(), OpensMode.RUNTIME);
project.getLogger().debug("BaselineModuleJvmArgs executing {} on {} with exports: {}", test.getName(), project, arguments);
return arguments;
}
});
}
});
project.getTasks().withType(JavaExec.class).configureEach(new Action<JavaExec>() {
@Override
public void execute(JavaExec javaExec) {
javaExec.getJvmArgumentProviders().add(new CommandLineArgumentProvider() {
@Override
public Iterable<String> asArguments() {
ImmutableList<String> arguments = collectClasspathArgs(project, extension, javaExec.getClasspath(), OpensMode.RUNTIME);
project.getLogger().debug("BaselineModuleJvmArgs executing {} on {} with exports: {}", javaExec.getName(), project, arguments);
return arguments;
}
});
}
});
project.getTasks().withType(Jar.class).configureEach(new Action<Jar>() {
@Override
public void execute(Jar jar) {
jar.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
jar.manifest(new Action<Manifest>() {
@Override
public void execute(Manifest manifest) {
addManifestAttribute(jar, manifest, ADD_EXPORTS_ATTRIBUTE, extension.exports());
addManifestAttribute(jar, manifest, ADD_OPENS_ATTRIBUTE, extension.opens());
}
});
}
});
}
});
});
}
Aggregations