【发布时间】:2016-10-29 16:52:12
【问题描述】:
我逐字跟踪 Spring Rest Doc 的 getting started guide,但我无法从生成的 sn-ps 中获取任何 html。
sn-ps 在我配置的目录(build/generated-sn-ps)中生成得很好,但是我看不到任何 html5/ 目录,其中包含从 sn 生成的 html 文件-ps。
at some point 文档说明了如何将文档打包到 jar 中,很明显它需要 html5/ 目录中的一些文件,但这不是在构建运行时创建的:
dependsOn asciidoctor
from("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
我错过了什么?
我的项目文件,build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE'
}
}
plugins {
id 'org.asciidoctor.convert' version '1.5.2'
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
snippetsDir = file('build/generated-snippets')
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web:1.3.5.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-logging:1.3.5.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.5.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-rest:1.3.5.RELEASE'
compile 'org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE'
compile 'org.postgresql:postgresql:9.4.1208'
compile 'commons-io:commons-io:2.5'
testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:1.1.0.RELEASE'
testCompile 'org.springframework.restdocs:spring-restdocs-core:1.1.0.RELEASE'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.3.5.RELEASE'
}
jacoco {
toolVersion = "0.7.6.201602180812"
reportsDir = file("$buildDir/customJacocoReportDir")
}
test {
outputs.dir snippetsDir
jacoco {
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpFile = file("$buildDir/jacoco/classpathdumps")
}
}
asciidoctor {
attributes 'snippets': snippetsDir
inputs.dir snippetsDir
dependsOn test
}
war {
dependsOn asciidoctor
from("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
baseName = project_name
version = version
manifest {
attributes(
'Implementation-Title': project_name,
'Implementation-Version': version
)
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
}
还有一个我用来测试的简单测试文件:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApiDocumentation
{
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build/generated-snippets");
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp()
{
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(documentationConfiguration(restDocumentation))
.build();
}
@Test
public void testIndex() throws Exception
{
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("index"));
}
}
【问题讨论】:
-
你有手写的
.adoc文件,其中包含生成的sn-ps?另外,你是如何运行你的构建的?您需要运行asciidoctor任务以从.adoc文件和 sn-ps 生成 HTML。 -
我没有手写的 adoc。我以为我需要一个,但我不知道把它放在哪里才能被拿起? documentation 对此有点不清楚。在挖掘 asciiidoc gradle 插件代码后,我尝试在第 4 点更改 what the documentation says(将 inputs.dir 更改为 sourceDir),我现在可以在 build/asciidoc/html 中看到 html 文件,这是一个文档错误吗?
-
不,这不是一个错误。将生成的 sn-ps 目录设置为测试任务的输出和 asciidoctor 任务的输入允许 Gradle 找出任务依赖关系并执行准确的最新检查。
标签: java spring api-doc spring-restdocs