Skip to content

Commit 7039330

Browse files
committed
Polish "Use -parameters compiler arg by default in Gradle builds"
Closes gh-9839
1 parent 238ef98 commit 7039330

File tree

7 files changed

+72
-58
lines changed

7 files changed

+72
-58
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ plugin:
2121
5. Creates a configuration named `bootArchives` that contains the artifact produced by
2222
the `bootJar` task.
2323
6. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`.
24+
7. Configures any `JavaCompile` tasks to use the `-parameters` compiler argument.
2425

2526

2627

spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ private void configureUtf8Encoding(Project project) {
125125
}
126126

127127
private void configureParametersCompilerArg(Project project) {
128-
project.getTasks().withType(JavaCompile.class, compile -> {
129-
final List<String> compilerArgs = compile.getOptions().getCompilerArgs();
128+
project.getTasks().withType(JavaCompile.class, (compile) -> {
129+
List<String> compilerArgs = compile.getOptions().getCompilerArgs();
130130
if (!compilerArgs.contains(PARAMETERS_COMPILER_ARG)) {
131131
compilerArgs.add(PARAMETERS_COMPILER_ARG);
132132
}
133133
});
134134
}
135+
135136
}

spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,23 @@ public void javaCompileTasksUseUtf8Encoding() {
7575

7676
@Test
7777
public void javaCompileTasksUseParametersCompilerFlagByDefault() {
78-
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin")
79-
.getOutput()).contains("compileJava has -parameters by default = true")
80-
.contains("compileTestJava has -parameters by default = true");
78+
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
79+
.contains("compileJava compiler args: [-parameters]")
80+
.contains("compileTestJava compiler args: [-parameters]");
8181
}
8282

83-
// -parameters and an additional compiler arg
8483
@Test
85-
public void javaCompileTasksUseParametersCompilerFlagWhenOtherAdded() {
86-
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersAddOther")
87-
.getOutput()).contains("compileJava has -parameters when another arg has been added = true")
88-
.contains("compileTestJava has -parameters when another arg has been added = true");
84+
public void javaCompileTasksUseParametersAndAdditionalCompilerFlags() {
85+
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
86+
.contains("compileJava compiler args: [-parameters, -Xlint:all]")
87+
.contains("compileTestJava compiler args: [-parameters, -Xlint:all]");
8988
}
9089

91-
// -parameters removed
9290
@Test
93-
public void javaCompileTasksDoesNotUseParametersWhenParametersRemoved() {
94-
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersRemove")
95-
.getOutput()).contains("compileJava has -parameters when removed = false")
96-
.contains("compileTestJava has -parameters when removed = false");
97-
}
98-
99-
// compiler args cleared
100-
@Test
101-
public void javaCompileTasksDoesNotUseParametersWhenArgsCleared() {
102-
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersClear")
103-
.getOutput()).contains("compileJava has -parameters when cleared = false")
104-
.contains("compileTestJava has -parameters when cleared = false");
91+
public void javaCompileTasksCanOverrideDefaultParametersCompilerFlag() {
92+
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
93+
.contains("compileJava compiler args: [-Xlint:all]")
94+
.contains("compileTestJava compiler args: [-Xlint:all]");
10595
}
10696

10797
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
apply plugin: 'org.springframework.boot'
8+
apply plugin: 'java'
9+
10+
tasks.withType(JavaCompile) {
11+
options.compilerArgs = ['-Xlint:all']
12+
}
13+
14+
15+
task('javaCompileTasksCompilerArgs') {
16+
doFirst {
17+
tasks.withType(JavaCompile) {
18+
println "$name compiler args: ${options.compilerArgs}"
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
apply plugin: 'org.springframework.boot'
8+
apply plugin: 'java'
9+
10+
tasks.withType(JavaCompile) {
11+
options.compilerArgs << '-Xlint:all'
12+
}
13+
14+
task('javaCompileTasksCompilerArgs') {
15+
doFirst {
16+
tasks.withType(JavaCompile) {
17+
println "$name compiler args: ${options.compilerArgs}"
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
apply plugin: 'org.springframework.boot'
8+
apply plugin: 'java'
9+
10+
task('javaCompileTasksCompilerArgs') {
11+
doFirst {
12+
tasks.withType(JavaCompile) {
13+
println "$name compiler args: ${options.compilerArgs}"
14+
}
15+
}
16+
}

spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@ if (project.hasProperty('applyJavaPlugin')) {
1010
apply plugin: 'java'
1111
}
1212

13-
if (project.hasProperty('parametersRemove')) {
14-
tasks.withType(JavaCompile) {
15-
options.compilerArgs.remove('-parameters')
16-
}
17-
}
18-
19-
if (project.hasProperty('parametersClear')) {
20-
tasks.withType(JavaCompile) {
21-
options.compilerArgs.clear()
22-
}
23-
}
24-
25-
if (project.hasProperty('parametersAddOther')) {
26-
tasks.withType(JavaCompile) {
27-
options.compilerArgs.add('-Xlint:all')
28-
}
29-
}
30-
3113
task('taskExists') {
3214
doFirst {
3315
println "$taskName exists = ${tasks.findByName(taskName) != null}"
@@ -41,20 +23,3 @@ task('javaCompileEncoding') {
4123
}
4224
}
4325
}
44-
45-
task('javaParametersCompilerArg') {
46-
doFirst {
47-
tasks.withType(JavaCompile) {
48-
def contains = options.compilerArgs.contains('-parameters')
49-
if (project.hasProperty('parametersRemove')) {
50-
println "$name has -parameters when removed = ${contains}"
51-
} else if (project.hasProperty('parametersClear')) {
52-
println "$name has -parameters when cleared = ${contains}"
53-
} else if (project.hasProperty('parametersAddOther')) {
54-
println "$name has -parameters when another arg has been added = ${contains}"
55-
} else {
56-
println "$name has -parameters by default = ${contains}"
57-
}
58-
}
59-
}
60-
}

0 commit comments

Comments
 (0)