@@ -94,13 +94,9 @@ the configuration are described below.
9494 <scope>test</scope>
9595 </dependency>
9696
97- <properties> <2>
98- <snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
99- </properties>
100-
10197 <build>
10298 <plugins>
103- <plugin> <3 >
99+ <plugin> <2 >
104100 <groupId>org.apache.maven.plugins</groupId>
105101 <artifactId>maven-surefire-plugin</artifactId>
106102 <configuration>
@@ -109,76 +105,81 @@ the configuration are described below.
109105 </includes>
110106 </configuration>
111107 </plugin>
112- <plugin> <4 >
108+ <plugin> <3 >
113109 <groupId>org.asciidoctor</groupId>
114110 <artifactId>asciidoctor-maven-plugin</artifactId>
115- <version>1.5.2 </version>
111+ <version>1.5.3 </version>
116112 <executions>
117113 <execution>
118114 <id>generate-docs</id>
119- <phase>prepare-package</phase> <6 >
115+ <phase>prepare-package</phase> <4 >
120116 <goals>
121117 <goal>process-asciidoc</goal>
122118 </goals>
123119 <configuration>
124120 <backend>html</backend>
125121 <doctype>book</doctype>
126- <attributes>
127- <snippets>${snippetsDirectory}</snippets> <5>
128- </attributes>
129122 </configuration>
130123 </execution>
131124 </executions>
125+ <dependencies>
126+ <dependency> <5>
127+ <groupId>org.springframework.restdocs</groupId>
128+ <artifactId>spring-restdocs-asciidoctor</artifactId>
129+ <version>{project-version}</version>
130+ </dependency>
131+ </dependencies>
132132 </plugin>
133133 </plugins>
134134 </build>
135135----
136136<1> Add a dependency on `spring-restdocs-mockmvc` in the `test` scope. If you want to use
137137 REST Assured rather than MockMvc, add a dependency on `spring-restdocs-restassured`
138138 instead.
139- <2> Configure a property to define the output location for generated snippets.
140- <3> Add the SureFire plugin and configure it to include files whose names end with
139+ <2> Add the SureFire plugin and configure it to include files whose names end with
141140 `Documentation.java`.
142- <4> Add the Asciidoctor plugin
143- <5> Define an attribute named `snippets` that can be used when including the generated
144- snippets in your documentation.
145- <6> Using `prepare-package` allows the documentation to be
141+ <3> Add the Asciidoctor plugin.
142+ <4> Using `prepare-package` allows the documentation to be
146143 <<getting-started-build-configuration-maven-packaging, included in the package>>.
144+ <5> Add `spring-restdocs-asciidoctor` as a dependency of the Asciidoctor plugin. This
145+ will automatically configure the `snippets` attribute for use in your `.adoc` files to
146+ point to `target/generated-snippets`.
147147
148148[source,groovy,indent=0,subs="verbatim,attributes",role="secondary"]
149149.Gradle
150150----
151151 plugins { <1>
152- id "org.asciidoctor.convert" version "1.5.2 "
152+ id "org.asciidoctor.convert" version "1.5.3 "
153153 }
154154
155- dependencies { <2>
156- testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:{project-version}'
155+ dependencies {
156+ asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor:{project-version}' <2>
157+ testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:{project-version}' <3>
157158 }
158159
159- ext { <3 >
160+ ext { <4 >
160161 snippetsDir = file('build/generated-snippets')
161162 }
162163
163- test { <4 >
164+ test { <5 >
164165 outputs.dir snippetsDir
165166 }
166167
167- asciidoctor { <5>
168- attributes 'snippets': snippetsDir <6>
168+ asciidoctor { <6>
169169 inputs.dir snippetsDir <7>
170170 dependsOn test <8>
171171 }
172172----
173173<1> Apply the Asciidoctor plugin.
174- <2> Add a dependency on `spring-restdocs-mockmvc` in the `testCompile` configuration. If
174+ <2> Add a dependency on `spring-restdocs-asciidoctor` in the `asciidoctor` configuration.
175+ This will automatically configure the `snippets` attribute for use in your `.adoc`
176+ files to point to `build/generated-snippets`.
177+ <3> Add a dependency on `spring-restdocs-mockmvc` in the `testCompile` configuration. If
175178 you want to use REST Assured rather than MockMvc, add a dependency on
176179 `spring-restdocs-restassured` instead.
177- <3> Configure a property to define the output location for generated snippets.
178- <4> Configure the `test` task to add the snippets directory as an output.
179- <5> Configure the `asciidoctor` task
180- <6> Define an attribute named `snippets` that can be used when including the generated
181- snippets in your documentation.
180+ <4> Configure a property to define the output location for generated snippets.
181+ <5> Configure the `test` task to add the snippets directory as an output.
182+ <6> Configure the `asciidoctor` task
182183<7> Configure the snippets directory as an input.
183184<8> Make the task depend on the test task so that the tests are run before the
184185 documentation is created.
@@ -271,28 +272,38 @@ are also supported although slightly more setup is required.
271272===== Setting up your JUnit tests
272273
273274When using JUnit, the first step in generating documentation snippets is to declare a
274- `public` `JUnitRestDocumentation` field that's annotated as a JUnit `@Rule`. The
275- `JUnitRestDocumentation` rule is configured with the output directory into which generated
276- snippets should be written. This output directory should match the snippets directory that
277- you have configured in your `build.gradle` or `pom.xml` file.
275+ `public` `JUnitRestDocumentation` field that's annotated as a JUnit `@Rule`.
278276
279- For Maven (`pom.xml`) that will typically be `target/generated-snippets` and for
280- Gradle (`build.gradle`) it will typically be `build/generated-snippets`:
281277
282- [source,java,indent=0,role="primary"]
283- .Maven
278+ [source,java,indent=0]
284279----
285280@Rule
286- public JUnitRestDocumentation restDocumentation =
287- new JUnitRestDocumentation("target/generated-snippets");
281+ public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
288282----
289283
290- [source,java,indent=0,role="secondary"]
291- .Gradle
284+
285+ By default, the `JUnitRestDocumentation` rule is automatically configured with an output
286+ directory based on your project's build tool:
287+
288+ [cols="2,5"]
289+ |===
290+ | Build tool | Output directory
291+
292+ | Maven
293+ | `target/generated-snippets`
294+
295+ | Gradle
296+ | `build/generated-snippets`
297+
298+ |===
299+
300+ The default can be overridden by providing an output directory when creating the
301+ `JUnitRestDocumentation` instance:
302+
303+ [source,java,indent=0]
292304----
293305@Rule
294- public JUnitRestDocumentation restDocumentation =
295- new JUnitRestDocumentation("build/generated-snippets");
306+ public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("custom");
296307----
297308
298309Next, provide an `@Before` method to configure MockMvc or REST Assured:
@@ -331,18 +342,9 @@ illustrates the approach.
331342The first difference is that `ManualRestDocumentation` should be used in place of
332343`JUnitRestDocumentation` and there's no need for the `@Rule` annotation:
333344
334- [source,java,indent=0,role="primary"]
335- .Maven
336- ----
337- private ManualRestDocumentation restDocumentation =
338- new ManualRestDocumentation("target/generated-snippets");
339- ----
340-
341- [source,java,indent=0,role="secondary"]
342- .Gradle
345+ [source,java,indent=0]
343346----
344- private ManualRestDocumentation restDocumentation =
345- new ManualRestDocumentation("build/generated-snippets");
347+ private ManualRestDocumentation restDocumentation = new ManualRestDocumentation();
346348----
347349
348350Secondly, `ManualRestDocumentation.beforeTest(Class, String)`
@@ -441,7 +443,8 @@ the resulting HTML files depends on whether you are using Maven or Gradle:
441443The generated snippets can then be included in the manually created Asciidoctor file from
442444above using the
443445http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include macro].
444- The `snippets` attribute specified in the <<getting-started-build-configuration, build
446+ The `snippets` attribute that is automatically set by `spring-restdocs-asciidoctor`
447+ configured in the <<getting-started-build-configuration, build
445448configuration>> can be used to reference the snippets output directory. For example:
446449
447450[source,adoc,indent=0]
0 commit comments