diff --git a/changelog/@unreleased/pr-1890.v2.yml b/changelog/@unreleased/pr-1890.v2.yml new file mode 100644 index 000000000..9a5202d71 --- /dev/null +++ b/changelog/@unreleased/pr-1890.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: Java compilation warnings and errors are no longer limited to the first + 100 + links: + - https://github.com/palantir/gradle-baseline/pull/1890 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java index 738af5dee..3e059f4c5 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java @@ -57,6 +57,7 @@ public void apply(Project project) { proj.getPluginManager().apply(BaselineReleaseCompatibility.class); proj.getPluginManager().apply(BaselineTesting.class); proj.getPluginManager().apply(BaselineTestHeap.class); + proj.getPluginManager().apply(BaselineJavaCompilerDiagnostics.class); proj.getPluginManager().apply(BaselineJavaParameters.class); proj.getPluginManager().apply(BaselineImmutables.class); }); diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaCompilerDiagnostics.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaCompilerDiagnostics.java new file mode 100644 index 000000000..922990368 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineJavaCompilerDiagnostics.java @@ -0,0 +1,53 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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 com.palantir.baseline.plugins; + +import java.util.List; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.tasks.compile.JavaCompile; + +/** + * Applies the {@code -Xmaxwarns} and {@code -Xmaxerrs} compiler options with a very large + * limit to avoid truncating failure info. + */ +public final class BaselineJavaCompilerDiagnostics implements Plugin { + + // Default maxwarns/maxerrs is only 100, which often makes it difficult to debug + // annotation processing issues. + private static final String MANY = "10000"; + + private static final String MAX_WARNINGS_ARG = "-Xmaxwarns"; + private static final String MAX_ERRORS_ARG = "-Xmaxerrs"; + + @Override + public void apply(Project proj) { + proj.afterEvaluate( + project -> project.getTasks().withType(JavaCompile.class).configureEach(javaCompileTask -> { + List compilerArgs = javaCompileTask.getOptions().getCompilerArgs(); + // Avoid overriding options that have already been set + if (!compilerArgs.contains(MAX_WARNINGS_ARG)) { + compilerArgs.add(MAX_WARNINGS_ARG); + compilerArgs.add(MANY); + } + if (!compilerArgs.contains(MAX_ERRORS_ARG)) { + compilerArgs.add(MAX_ERRORS_ARG); + compilerArgs.add(MANY); + } + })); + } +} diff --git a/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-java-compiler-diagnostics.properties b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-java-compiler-diagnostics.properties new file mode 100644 index 000000000..18fed0ce7 --- /dev/null +++ b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-java-compiler-diagnostics.properties @@ -0,0 +1 @@ +implementation-class=com.palantir.baseline.plugins.BaselineJavaCompilerDiagnostics diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaCompilerDiagnosticsTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaCompilerDiagnosticsTest.groovy new file mode 100644 index 000000000..999e984bb --- /dev/null +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineJavaCompilerDiagnosticsTest.groovy @@ -0,0 +1,72 @@ +/* + * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. + * + * 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 com.palantir.baseline + +import com.palantir.baseline.plugins.BaselineJavaCompilerDiagnostics +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification + +class BaselineJavaCompilerDiagnosticsTest extends Specification { + + def testDefault() { + when: + def project = ProjectBuilder.builder().build() + project.buildscript { + repositories { + mavenCentral() + } + } + project.plugins.apply 'java' + project.plugins.apply BaselineJavaCompilerDiagnostics + project.evaluate() + + then: + JavaCompile compileTask = project.tasks.getByName('compileJava').asType(JavaCompile.class) + List allJvmArgs = compileTask.allJvmArgs + allJvmArgs.stream().filter('-Xmaxerrs'::equals).count() == 1 + allJvmArgs.get(allJvmArgs.indexOf('-Xmaxerrs') + 1) == '1000' + allJvmArgs.stream().filter('-Xmaxwarns'::equals).count() == 1 + allJvmArgs.get(allJvmArgs.indexOf('-Xmaxerrs') + 1) == '1000' + } + + def testOverridden() { + when: + def project = ProjectBuilder.builder().build() + project.buildscript { + repositories { + mavenCentral() + } + } + project.plugins.apply 'java' + tasks.withType(JavaCompile) { + options.compilerArgs += [ + '-Xmaxerrs', '1000' + ] + } + project.plugins.apply BaselineJavaCompilerDiagnostics + project.evaluate() + + then: + project.tasks.type + JavaCompile compileTask = project.tasks.getByName('compileJava').asType(JavaCompile.class) + List allJvmArgs = compileTask.allJvmArgs + allJvmArgs.stream().filter('-Xmaxerrs'::equals).count() == 1 + allJvmArgs.get(allJvmArgs.indexOf("-Xmaxerrs") + 1) == '1000' + allJvmArgs.stream().filter('-Xmaxwarns'::equals).count() == 1 + } +}