Skip to content

Commit 319bd13

Browse files
committed
Polish contribution and rework to use default attribute rather than macro
Rather than introducing a custom macro, this commit opts to implicitly configure the snippets attribute instead. The attribute is configured will the path into which snippets are generated, relative to the directory that contains the Asciidoctor document that is being rendered. The samples and documentation have been updated to use the new spring-restdocs-asciidoctor module and the implicitly configured snippets attribute. Closes spring-projectsgh-297
1 parent 3ac4a1a commit 319bd13

31 files changed

Lines changed: 348 additions & 244 deletions

File tree

‎build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ subprojects {
6767
dependency 'javax.servlet:javax.servlet-api:3.1.0'
6868
dependency 'javax.validation:validation-api:1.1.0.Final'
6969
dependency 'junit:junit:4.12'
70+
dependency 'org.asciidoctor:asciidoctorj:1.5.3.1'
7071
dependency 'org.hamcrest:hamcrest-core:1.3'
7172
dependency 'org.hamcrest:hamcrest-library:1.3'
7273
dependency 'org.hibernate:hibernate-validator:5.2.2.Final'

‎config/checkstyle/checkstyle-suppressions.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@
55
<suppressions>
66
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]" checks="JavadocVariable" />
77
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]" checks="JavadocMethod" />
8-
<suppress files="RestDocsSnippetBlockMacro\.java" checks="RedundantModifier" />
98
</suppressions>

‎docs/src/docs/asciidoc/getting-started.adoc‎

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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

273274
When 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

298309
Next, provide an `@Before` method to configure MockMvc or REST Assured:
@@ -331,18 +342,9 @@ illustrates the approach.
331342
The 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

348350
Secondly, `ManualRestDocumentation.beforeTest(Class, String)`
@@ -441,7 +443,8 @@ the resulting HTML files depends on whether you are using Maven or Gradle:
441443
The generated snippets can then be included in the manually created Asciidoctor file from
442444
above using the
443445
http://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
445448
configuration>> can be used to reference the snippets output directory. For example:
446449

447450
[source,adoc,indent=0]

‎docs/src/docs/asciidoc/working-with-asciidoctor.adoc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ relevant to Spring REST Docs.
1717
[[working-with-asciidoctor-including-snippets]]
1818
=== Including snippets
1919

20-
The http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include
20+
The http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files[include
2121
macro] is used to include generated snippets in your documentation. The `snippets`
22-
attribute specified in the <<getting-started-build-configuration, build configuration>>
23-
can be used to reference the snippets output directory, for example:
22+
attribute that is automatically set by `spring-restdocs-asciidoctor` configured in the
23+
<<getting-started-build-configuration, build configuration>> can be used to reference the
24+
snippets output directory. For example:
2425

2526
[source,adoc,indent=0]
2627
----

‎samples/rest-assured/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ dependencies {
3737
compile 'org.springframework.boot:spring-boot-starter-web'
3838
testCompile 'org.springframework.boot:spring-boot-starter-test'
3939
testCompile "org.springframework.restdocs:spring-restdocs-restassured:${project.ext['spring-restdocs.version']}"
40+
asciidoctor "org.springframework.restdocs:spring-restdocs-asciidoctor:${project.ext['spring-restdocs.version']}"
4041
}
4142

4243
test {
4344
outputs.dir snippetsDir
4445
}
4546

4647
asciidoctor {
47-
attributes 'snippets': snippetsDir
4848
inputs.dir snippetsDir
4949
dependsOn test
5050
}

‎samples/rest-notes-grails/.gitignore‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ classes/
1111
.settings
1212
.classpath
1313
gradlew*
14-
gradle/wrapper
15-
16-
17-
src/docs/generated-snippets
14+
gradle/wrapper

‎samples/rest-notes-grails/build.gradle‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ repositories {
4444
dependencyManagement {
4545
dependencies {
4646
dependency "org.springframework.restdocs:spring-restdocs-restassured:$restDocsVersion"
47+
dependency "org.springframework.restdocs:spring-restdocs-asciidoctor:$restDocsVersion"
4748
}
4849
imports {
4950
mavenBom "org.grails:grails-bom:$grailsVersion"
@@ -53,6 +54,8 @@ dependencyManagement {
5354
}
5455

5556
dependencies {
57+
asciidoctor "org.springframework.restdocs:spring-restdocs-asciidoctor"
58+
5659
compile "org.springframework.boot:spring-boot-starter-logging"
5760
compile "org.springframework.boot:spring-boot-starter-actuator"
5861
compile "org.springframework.boot:spring-boot-autoconfigure"
@@ -85,24 +88,16 @@ task wrapper(type: Wrapper) {
8588
}
8689

8790
ext {
88-
snippetsDir = file('src/docs/generated-snippets')
89-
}
90-
91-
task cleanTempDirs(type: Delete) {
92-
delete fileTree(dir: 'src/docs/generated-snippets')
91+
snippetsDir = file('build/generated-snippets')
9392
}
9493

9594
test {
96-
dependsOn cleanTempDirs
9795
outputs.dir snippetsDir
9896
}
9997

10098
asciidoctor {
10199
dependsOn integrationTest
102100
inputs.dir snippetsDir
103-
sourceDir = file('src/docs')
104-
separateOutputDirs = false
105-
attributes 'snippets': snippetsDir
106101
}
107102

108103
build.dependsOn asciidoctor
File renamed without changes.

‎samples/rest-notes-grails/src/integration-test/groovy/com/example/ApiDocumentationSpec.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import spock.lang.Specification
4545
class ApiDocumentationSpec extends Specification {
4646

4747
@Rule
48-
JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation('src/docs/generated-snippets')
48+
JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation()
4949

5050
@Value('${local.server.port}')
5151
Integer serverPort

‎samples/rest-notes-spring-data-rest/pom.xml‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@
8484
<configuration>
8585
<backend>html</backend>
8686
<doctype>book</doctype>
87-
<attributes>
88-
<snippets>${project.build.directory}/generated-snippets</snippets>
89-
</attributes>
9087
</configuration>
9188
</execution>
9289
</executions>
90+
<dependencies>
91+
<dependency>
92+
<groupId>org.springframework.restdocs</groupId>
93+
<artifactId>spring-restdocs-asciidoctor</artifactId>
94+
<version>${spring-restdocs.version}</version>
95+
</dependency>
96+
</dependencies>
9397
</plugin>
9498
<plugin>
9599
<artifactId>maven-resources-plugin</artifactId>

0 commit comments

Comments
 (0)