Skip to content

Ensure that the -parameters compiler flag is included in boot applications built with Gradle #9839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.gradle.plugin;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

import org.gradle.api.Action;
Expand All @@ -41,6 +42,7 @@
*/
final class JavaPluginAction implements PluginApplicationAction {

private static final String PARAMETERS_COMPILER_ARG = "-parameters";
private final SinglePublishedArtifact singlePublishedArtifact;

JavaPluginAction(SinglePublishedArtifact singlePublishedArtifact) {
Expand All @@ -60,6 +62,7 @@ public void execute(Project project) {
configureArtifactPublication(project, bootJar);
configureBootRunTask(project);
configureUtf8Encoding(project);
configureParametersCompilerArg(project);
}

private void disableJarTask(Project project) {
Expand Down Expand Up @@ -121,4 +124,12 @@ private void configureUtf8Encoding(Project project) {
}));
}

private void configureParametersCompilerArg(Project project) {
project.getTasks().withType(JavaCompile.class, compile -> {
final List<String> compilerArgs = compile.getOptions().getCompilerArgs();
if (!compilerArgs.contains(PARAMETERS_COMPILER_ARG)) {
compilerArgs.add(PARAMETERS_COMPILER_ARG);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ public void javaCompileTasksUseUtf8Encoding() {
.contains("compileTestJava = UTF-8");
}

@Test
public void javaCompileTasksUseParametersCompilerFlagByDefault() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin")
.getOutput()).contains("compileJava has -parameters by default = true")
.contains("compileTestJava has -parameters by default = true");
}

// -parameters and an additional compiler arg
@Test
public void javaCompileTasksUseParametersCompilerFlagWhenOtherAdded() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersAddOther")
.getOutput()).contains("compileJava has -parameters when another arg has been added = true")
.contains("compileTestJava has -parameters when another arg has been added = true");
}

// -parameters removed
@Test
public void javaCompileTasksDoesNotUseParametersWhenParametersRemoved() {
assertThat(this.gradleBuild.build("javaParametersCompilerArg", "-PapplyJavaPlugin", "-PparametersRemove")
.getOutput()).contains("compileJava has -parameters when removed = false")
.contains("compileTestJava has -parameters when removed = false");
}

// 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
public void assembleRunsBootJarAndJarIsSkipped() {
BuildResult result = this.gradleBuild.build("assemble");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ if (project.hasProperty('applyJavaPlugin')) {
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') {
doFirst {
println "$taskName exists = ${tasks.findByName(taskName) != null}"
Expand All @@ -23,3 +41,20 @@ 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}"
}
}
}
}