Skip to content

Commit e0a6b16

Browse files
committed
Merge pull request #9839 from kashike
* gh-9839: Polish "Use -parameters compiler arg by default in Gradle builds" Use -parameters compiler arg by default in Gradle builds
2 parents 8ab12d9 + 7039330 commit e0a6b16

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

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

+1
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

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.gradle.plugin;
1818

1919
import java.util.Collections;
20+
import java.util.List;
2021
import java.util.concurrent.Callable;
2122

2223
import org.gradle.api.Action;
@@ -41,6 +42,7 @@
4142
*/
4243
final class JavaPluginAction implements PluginApplicationAction {
4344

45+
private static final String PARAMETERS_COMPILER_ARG = "-parameters";
4446
private final SinglePublishedArtifact singlePublishedArtifact;
4547

4648
JavaPluginAction(SinglePublishedArtifact singlePublishedArtifact) {
@@ -60,6 +62,7 @@ public void execute(Project project) {
6062
configureArtifactPublication(project, bootJar);
6163
configureBootRunTask(project);
6264
configureUtf8Encoding(project);
65+
configureParametersCompilerArg(project);
6366
}
6467

6568
private void disableJarTask(Project project) {
@@ -121,4 +124,13 @@ private void configureUtf8Encoding(Project project) {
121124
}));
122125
}
123126

127+
private void configureParametersCompilerArg(Project project) {
128+
project.getTasks().withType(JavaCompile.class, (compile) -> {
129+
List<String> compilerArgs = compile.getOptions().getCompilerArgs();
130+
if (!compilerArgs.contains(PARAMETERS_COMPILER_ARG)) {
131+
compilerArgs.add(PARAMETERS_COMPILER_ARG);
132+
}
133+
});
134+
}
135+
124136
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ public void javaCompileTasksUseUtf8Encoding() {
7373
.contains("compileTestJava = UTF-8");
7474
}
7575

76+
@Test
77+
public void javaCompileTasksUseParametersCompilerFlagByDefault() {
78+
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
79+
.contains("compileJava compiler args: [-parameters]")
80+
.contains("compileTestJava compiler args: [-parameters]");
81+
}
82+
83+
@Test
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]");
88+
}
89+
90+
@Test
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]");
95+
}
96+
7697
@Test
7798
public void assembleRunsBootJarAndJarIsSkipped() {
7899
BuildResult result = this.gradleBuild.build("assemble");
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+
}

0 commit comments

Comments
 (0)