Make discovery of additional config metdata more robust with Gradle

Previously, the configuration metadata annotation processor relied
upon an additional metadata file have been copied to an output
location. When building with Gradle, it's the processResources task
that performs this copy and there is no guarantee that it will have
run before the compileJava task unless an explicit dependency betwee
the two tasks has been configured. If a project is built using
Gradle's parallel build support, the likelihood of this required
ordering not occurring increases.

This commit updates the configuration metadata annotation processor to
consider a new annotation processor option when looking for the
additional config metadata file. The Gradle plugin has been updated
to provide this option as a compiler argument. The option is only
provided when the annotation processor is found on the compilation
classpath to avoid a warning from javac's annotation processing about
the use of an option that is not supported by any of the available
annotation processors.

Closes gh-9755
This commit is contained in:
Andy Wilkinson
2017-10-30 22:04:39 +00:00
parent de080165ec
commit 6f55b57855
7 changed files with 170 additions and 6 deletions

View File

@@ -17,6 +17,9 @@
package org.springframework.boot.gradle.plugin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarOutputStream;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
@@ -113,4 +116,39 @@ public class JavaPluginActionIntegrationTests {
this.gradleBuild.getProjectDir().getName() + "-boot.jar"));
}
@Test
public void additionalMetadataLocationsCompilerArgumentIsAddedWhenAnnotationProcessorIsOnTheClasspath()
throws IOException {
createMinimalMainSource();
File libs = new File(this.gradleBuild.getProjectDir(), "libs");
libs.mkdirs();
new JarOutputStream(new FileOutputStream(
new File(libs, "spring-boot-configuration-processor-1.2.3.jar"))).close();
BuildResult result = this.gradleBuild.build("compileJava");
assertThat(result.task(":compileJava").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains(
"compileJava compiler args: [-parameters, -Aorg.springframework.boot.configurationprocessor.additionalMetadataLocations="
+ this.gradleBuild.getProjectDir().getCanonicalPath()
+ "/src/main/resources]");
}
@Test
public void additionalMetadataLocationsCompilerArgumentIsNotAddedWhenAnnotationProcessorIsNotOnTheClasspath()
throws IOException {
createMinimalMainSource();
BuildResult result = this.gradleBuild.build("compileJava");
assertThat(result.task(":compileJava").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput())
.contains("compileJava compiler args: [-parameters]");
}
private void createMinimalMainSource() throws IOException {
File examplePackage = new File(this.gradleBuild.getProjectDir(),
"src/main/java/com/example");
examplePackage.mkdirs();
new File(examplePackage, "Application.java").createNewFile();
}
}