Remove assumption that working directory will be the project’s root
The plugin for building the samples assumed that the current working directory would be the directory containing the project’s build.gradle. While this is generally true on the command line (it isn’t if -project-dir is used), it isn’t true in Gradle’s IDE integration. This was leading to an NPE as the sampleBuild task was being configured to depend on a null task as a result of not finding the sample’s pom.xml or build.grade file. This commit corrects the main build.gradle to configure each sample’s working directory relative to the root project’s directory, rather than relying on the working directory. It also improves SampleBuildConfigurer so that it will throw an exception if it fails to find a sample’s build.gradle or pom.xml rather than continuing and triggering an NPE inside Gradle. Closes gh-185
This commit is contained in:
@@ -156,11 +156,11 @@ samples {
|
||||
dependOn 'spring-restdocs-mockmvc:install'
|
||||
|
||||
restNotesSpringHateoas {
|
||||
workingDir 'samples/rest-notes-spring-hateoas'
|
||||
workingDir "$projectDir/samples/rest-notes-spring-hateoas"
|
||||
}
|
||||
|
||||
restNotesSpringDataRest {
|
||||
workingDir 'samples/rest-notes-spring-data-rest'
|
||||
workingDir "$projectDir/samples/rest-notes-spring-data-rest"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,17 +38,21 @@ public class SampleBuildConfigurer {
|
||||
}
|
||||
|
||||
Task createTask(Project project, Object... dependencies) {
|
||||
File sampleDir = new File(this.workingDir).absoluteFile
|
||||
Task verifyIncludes
|
||||
if (new File(this.workingDir, 'build.gradle').isFile()) {
|
||||
if (new File(sampleDir, 'build.gradle').isFile()) {
|
||||
Task gradleBuild = createGradleBuild(project, dependencies)
|
||||
verifyIncludes = createVerifyIncludes(project, new File(this.workingDir, 'build/asciidoc'))
|
||||
verifyIncludes = createVerifyIncludes(project, new File(sampleDir, 'build/asciidoc'))
|
||||
verifyIncludes.dependsOn gradleBuild
|
||||
}
|
||||
if (new File(this.workingDir, 'pom.xml').isFile()) {
|
||||
Task mavenBuild = createMavenBuild(project, dependencies)
|
||||
verifyIncludes = createVerifyIncludes(project, new File(this.workingDir, 'target/generated-docs'))
|
||||
else if (new File(sampleDir, 'pom.xml').isFile()) {
|
||||
Task mavenBuild = createMavenBuild(project, sampleDir, dependencies)
|
||||
verifyIncludes = createVerifyIncludes(project, new File(sampleDir, 'target/generated-docs'))
|
||||
verifyIncludes.dependsOn(mavenBuild)
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("No pom.xml or build.gradle was found in $sampleDir")
|
||||
}
|
||||
Task sampleBuild = project.tasks.create name
|
||||
sampleBuild.description = "Builds the ${name} sample"
|
||||
sampleBuild.group = "Build"
|
||||
@@ -56,12 +60,12 @@ public class SampleBuildConfigurer {
|
||||
return sampleBuild
|
||||
}
|
||||
|
||||
private Task createMavenBuild(Project project, Object... dependencies) {
|
||||
private Task createMavenBuild(Project project, File sampleDir, Object... dependencies) {
|
||||
Task mavenBuild = project.tasks.create("${name}Maven", Exec)
|
||||
mavenBuild.description = "Builds the ${name} sample with Maven"
|
||||
mavenBuild.group = "Build"
|
||||
mavenBuild.workingDir = this.workingDir
|
||||
mavenBuild.commandLine = [isWindows() ? "${new File(this.workingDir).absolutePath}/mvnw.cmd" : './mvnw', 'clean', 'package']
|
||||
mavenBuild.commandLine = [isWindows() ? "${sampleDir.absolutePath}/mvnw.cmd" : './mvnw', 'clean', 'package']
|
||||
mavenBuild.dependsOn dependencies
|
||||
|
||||
mavenBuild.doFirst {
|
||||
|
||||
Reference in New Issue
Block a user