Provide a default output directory for snippets based on build tool
Rather than requiring an output directory to be explcitly configured, a default is now automatically configured based on the build tool that's being used. When using Gradle, snippets will be generated in build/generated-snippets. When using Maven, snippets will be generated in target/generated-snippets. See gh-297
This commit is contained in:
committed by
Andy Wilkinson
parent
c1540838a3
commit
3ac4a1acad
@@ -32,6 +32,14 @@ public class JUnitRestDocumentation
|
||||
|
||||
private final ManualRestDocumentation delegate;
|
||||
|
||||
/**
|
||||
* Creates a new {@code JUnitRestDocumentation} instance that will generate snippets
|
||||
* to <gradle/maven build path>/generated-snippet.
|
||||
*/
|
||||
public JUnitRestDocumentation() {
|
||||
this.delegate = new ManualRestDocumentation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code JUnitRestDocumentation} instance that will generate snippets
|
||||
* to the given {@code outputDirectory}.
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.restdocs;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* {@code ManualRestDocumentation} is used to manually manage the
|
||||
@@ -31,10 +33,23 @@ import java.io.File;
|
||||
*/
|
||||
public final class ManualRestDocumentation implements RestDocumentationContextProvider {
|
||||
|
||||
private static final String GENERATED_SNIPPETS_PATH = "generated-snippets";
|
||||
private static final String MAVEN_TARGET_PATH = "target" + File.separator + GENERATED_SNIPPETS_PATH;
|
||||
private static final String GRADLE_BUILD_PATH = "build" + File.separator + GENERATED_SNIPPETS_PATH;
|
||||
private static final String MAVEN_POM = "pom.xml";
|
||||
|
||||
private final File outputDirectory;
|
||||
|
||||
private RestDocumentationContext context;
|
||||
|
||||
/**
|
||||
* Creates a new {@code ManualRestDocumentation} instance that will generate snippets
|
||||
* to <gradle/maven build path>/generated-snippet.
|
||||
*/
|
||||
public ManualRestDocumentation() {
|
||||
this(getDefaultOutputDirectory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code ManualRestDocumentation} instance that will generate snippets
|
||||
* to the given {@code outputDirectory}.
|
||||
@@ -79,4 +94,12 @@ public final class ManualRestDocumentation implements RestDocumentationContextPr
|
||||
return this.context;
|
||||
}
|
||||
|
||||
private static String getDefaultOutputDirectory() {
|
||||
String executingDirectory = Paths.get(".").toFile().getAbsolutePath();
|
||||
|
||||
if (Files.exists(Paths.get(MAVEN_POM))) {
|
||||
return executingDirectory + File.separator + MAVEN_TARGET_PATH;
|
||||
}
|
||||
return executingDirectory + File.separator + GRADLE_BUILD_PATH;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,14 @@ public class RestDocumentation implements TestRule, RestDocumentationContextProv
|
||||
|
||||
private final JUnitRestDocumentation delegate;
|
||||
|
||||
/**
|
||||
* Creates a new {@code RestDocumentation} instance that will generate snippets to the
|
||||
* to <gradle/maven build path>/generated-snippet.
|
||||
*/
|
||||
public RestDocumentation() {
|
||||
this.delegate = new JUnitRestDocumentation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RestDocumentation} instance that will generate snippets to the
|
||||
* given {@code outputDirectory}.
|
||||
|
||||
@@ -195,8 +195,7 @@ public class RestDocumentationConfigurerTests {
|
||||
}
|
||||
|
||||
private RestDocumentationContext createContext() {
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
|
||||
"build");
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation("build");
|
||||
manualRestDocumentation.beforeTest(null, null);
|
||||
RestDocumentationContext context = manualRestDocumentation.beforeOperation();
|
||||
return context;
|
||||
|
||||
@@ -83,8 +83,7 @@ public class RestDocumentationContextPlaceholderResolverTests {
|
||||
}
|
||||
|
||||
private RestDocumentationContext createContext(String methodName) {
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
|
||||
"build");
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation("build");
|
||||
manualRestDocumentation.beforeTest(getClass(), methodName);
|
||||
RestDocumentationContext context = manualRestDocumentation.beforeOperation();
|
||||
return context;
|
||||
|
||||
@@ -70,8 +70,7 @@ public class StandardWriterResolverTests {
|
||||
}
|
||||
|
||||
private RestDocumentationContext createContext(String outputDir) {
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
|
||||
outputDir);
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(outputDir);
|
||||
manualRestDocumentation.beforeTest(getClass(), null);
|
||||
RestDocumentationContext context = manualRestDocumentation.beforeOperation();
|
||||
return context;
|
||||
|
||||
@@ -123,8 +123,7 @@ public class OperationBuilder extends OperationTestRule {
|
||||
}
|
||||
|
||||
private RestDocumentationContext createContext() {
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
|
||||
this.outputDirectory.getAbsolutePath());
|
||||
ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(this.outputDirectory.getAbsolutePath());
|
||||
manualRestDocumentation.beforeTest(null, null);
|
||||
RestDocumentationContext context = manualRestDocumentation.beforeOperation();
|
||||
return context;
|
||||
|
||||
Reference in New Issue
Block a user