Polish "Use -parameters compiler arg by default in Gradle builds"

Closes gh-9839
This commit is contained in:
Andy Wilkinson 2017-09-20 12:29:25 +01:00
parent 238ef98f8b
commit 70393300c2
7 changed files with 72 additions and 58 deletions

View File

@ -21,6 +21,7 @@ plugin:
5. Creates a configuration named `bootArchives` that contains the artifact produced by 5. Creates a configuration named `bootArchives` that contains the artifact produced by
the `bootJar` task. the `bootJar` task.
6. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`. 6. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`.
7. Configures any `JavaCompile` tasks to use the `-parameters` compiler argument.

View File

@ -125,11 +125,12 @@ final class JavaPluginAction implements PluginApplicationAction {
} }
private void configureParametersCompilerArg(Project project) { private void configureParametersCompilerArg(Project project) {
project.getTasks().withType(JavaCompile.class, compile -> { project.getTasks().withType(JavaCompile.class, (compile) -> {
final List<String> compilerArgs = compile.getOptions().getCompilerArgs(); List<String> compilerArgs = compile.getOptions().getCompilerArgs();
if (!compilerArgs.contains(PARAMETERS_COMPILER_ARG)) { if (!compilerArgs.contains(PARAMETERS_COMPILER_ARG)) {
compilerArgs.add(PARAMETERS_COMPILER_ARG); compilerArgs.add(PARAMETERS_COMPILER_ARG);
} }
}); });
} }
} }

View File

@ -75,33 +75,23 @@ public class JavaPluginActionIntegrationTests {
@Test @Test
public void javaCompileTasksUseParametersCompilerFlagByDefault() { public void javaCompileTasksUseParametersCompilerFlagByDefault() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin") assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.getOutput()).contains("compileJava has -parameters by default = true") .contains("compileJava compiler args: [-parameters]")
.contains("compileTestJava has -parameters by default = true"); .contains("compileTestJava compiler args: [-parameters]");
} }
// -parameters and an additional compiler arg
@Test @Test
public void javaCompileTasksUseParametersCompilerFlagWhenOtherAdded() { public void javaCompileTasksUseParametersAndAdditionalCompilerFlags() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersAddOther") assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.getOutput()).contains("compileJava has -parameters when another arg has been added = true") .contains("compileJava compiler args: [-parameters, -Xlint:all]")
.contains("compileTestJava has -parameters when another arg has been added = true"); .contains("compileTestJava compiler args: [-parameters, -Xlint:all]");
} }
// -parameters removed
@Test @Test
public void javaCompileTasksDoesNotUseParametersWhenParametersRemoved() { public void javaCompileTasksCanOverrideDefaultParametersCompilerFlag() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersRemove") assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.getOutput()).contains("compileJava has -parameters when removed = false") .contains("compileJava compiler args: [-Xlint:all]")
.contains("compileTestJava has -parameters when removed = false"); .contains("compileTestJava compiler args: [-Xlint:all]");
}
// compiler args cleared
@Test
public void javaCompileTasksDoesNotUseParametersWhenArgsCleared() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersClear")
.getOutput()).contains("compileJava has -parameters when cleared = false")
.contains("compileTestJava has -parameters when cleared = false");
} }
@Test @Test

View File

@ -0,0 +1,21 @@
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
tasks.withType(JavaCompile) {
options.compilerArgs = ['-Xlint:all']
}
task('javaCompileTasksCompilerArgs') {
doFirst {
tasks.withType(JavaCompile) {
println "$name compiler args: ${options.compilerArgs}"
}
}
}

View File

@ -0,0 +1,20 @@
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
tasks.withType(JavaCompile) {
options.compilerArgs << '-Xlint:all'
}
task('javaCompileTasksCompilerArgs') {
doFirst {
tasks.withType(JavaCompile) {
println "$name compiler args: ${options.compilerArgs}"
}
}
}

View File

@ -0,0 +1,16 @@
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
task('javaCompileTasksCompilerArgs') {
doFirst {
tasks.withType(JavaCompile) {
println "$name compiler args: ${options.compilerArgs}"
}
}
}

View File

@ -10,24 +10,6 @@ if (project.hasProperty('applyJavaPlugin')) {
apply plugin: 'java' apply plugin: 'java'
} }
if (project.hasProperty('parametersRemove')) {
tasks.withType(JavaCompile) {
options.compilerArgs.remove('-parameters')
}
}
if (project.hasProperty('parametersClear')) {
tasks.withType(JavaCompile) {
options.compilerArgs.clear()
}
}
if (project.hasProperty('parametersAddOther')) {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xlint:all')
}
}
task('taskExists') { task('taskExists') {
doFirst { doFirst {
println "$taskName exists = ${tasks.findByName(taskName) != null}" println "$taskName exists = ${tasks.findByName(taskName) != null}"
@ -41,20 +23,3 @@ task('javaCompileEncoding') {
} }
} }
} }
task('javaParametersCompilerArg') {
doFirst {
tasks.withType(JavaCompile) {
def contains = options.compilerArgs.contains('-parameters')
if (project.hasProperty('parametersRemove')) {
println "$name has -parameters when removed = ${contains}"
} else if (project.hasProperty('parametersClear')) {
println "$name has -parameters when cleared = ${contains}"
} else if (project.hasProperty('parametersAddOther')) {
println "$name has -parameters when another arg has been added = ${contains}"
} else {
println "$name has -parameters by default = ${contains}"
}
}
}
}