Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1890.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* (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<Project> {

// 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 project) {
project.getTasks().withType(JavaCompile.class).configureEach(javaCompileTask -> {
List<String> compilerArgs = javaCompileTask.getOptions().getCompilerArgs();
// Avoid overriding options that have already been set
if (!compilerArgs.contains(MAX_WARNINGS_ARG)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want this to work if people override after applying the plugin too? If so, we'd probably want to put this in a project.afterEvaluate to run after people have had a chance to change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does, but only due to the fact later arguments take precedence over earlier instances. The afterEvaluate will be cleaner :-)
Note that the same potential duplication can occur if commandlineargumentproviders are used because I'm not checking those, we take advantage of knowing they're applied after the fixed args. I figured we don't want to evaluate providers unnecessarily as part of this check due to potential side-effects given the worst case result still has the desired behavior.

compilerArgs.add(MAX_WARNINGS_ARG);
compilerArgs.add(MANY);
}
if (!compilerArgs.contains(MAX_ERRORS_ARG)) {
compilerArgs.add(MAX_ERRORS_ARG);
compilerArgs.add(MANY);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.palantir.baseline.plugins.BaselineJavaCompilerDiagnostics
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> allJvmArgs = compileTask.allJvmArgs
allJvmArgs.stream().filter('-Xmaxerrs'::equals).count() == 1
allJvmArgs.get(allJvmArgs.indexOf("-Xmaxerrs") + 1) == '1000'
allJvmArgs.stream().filter('-Xmaxwarns'::equals).count() == 1
}
}