Search in sources :

Example 1 with BaselineModuleJvmArgsExtension

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());
                            }
                        });
                    }
                });
            }
        });
    });
}
Also used : Task(org.gradle.api.Task) Action(org.gradle.api.Action) ImmutableList(com.google.common.collect.ImmutableList) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) CoreJavadocOptions(org.gradle.external.javadoc.CoreJavadocOptions) JavaExec(org.gradle.api.tasks.JavaExec) Test(org.gradle.api.tasks.testing.Test) MinimalJavadocOptions(org.gradle.external.javadoc.MinimalJavadocOptions) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Manifest(org.gradle.api.java.archives.Manifest) UnknownTaskException(org.gradle.api.UnknownTaskException) BaselineModuleJvmArgsExtension(com.palantir.baseline.extensions.BaselineModuleJvmArgsExtension) CommandLineArgumentProvider(org.gradle.process.CommandLineArgumentProvider) Jar(org.gradle.jvm.tasks.Jar) JavaCompile(org.gradle.api.tasks.compile.JavaCompile)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 BaselineModuleJvmArgsExtension (com.palantir.baseline.extensions.BaselineModuleJvmArgsExtension)1 List (java.util.List)1 Action (org.gradle.api.Action)1 Task (org.gradle.api.Task)1 UnknownTaskException (org.gradle.api.UnknownTaskException)1 Manifest (org.gradle.api.java.archives.Manifest)1 JavaExec (org.gradle.api.tasks.JavaExec)1 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)1 JavaCompile (org.gradle.api.tasks.compile.JavaCompile)1 Javadoc (org.gradle.api.tasks.javadoc.Javadoc)1 Test (org.gradle.api.tasks.testing.Test)1 CoreJavadocOptions (org.gradle.external.javadoc.CoreJavadocOptions)1 MinimalJavadocOptions (org.gradle.external.javadoc.MinimalJavadocOptions)1 Jar (org.gradle.jvm.tasks.Jar)1 CommandLineArgumentProvider (org.gradle.process.CommandLineArgumentProvider)1