【问题标题】:Spring Rest Doc not producing htmlSpring Rest Doc 不生成 html
【发布时间】: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


【解决方案1】:

使用:{spring-restdocs.version} 代替 {project-version} 然后更新项目。

它看起来像这样,

dependencies {
    asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor:{spring-restdocs.version}' 
    testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:{spring-restdocs.version}' 
}

在那之后, 项目 > 右键单击​​ > maven > 更新项目。

然后重新构建项目。希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    参考生成的 sn-ps 在src/main/asciidoc (Maven) 或 src/docs/asciidoc (Gradle) 下创建一个 .adoc 文件(如 api-guide.adoc)。然后会在指定目录生成html。

    【讨论】:

    • 如果我这样做,仍然没有生成 html。如果我更改 asciidoctor 部分中的build.gradle 文件(inputs.dir 更改为 sourceDir),我可以看到从build/asciidoc/html 中生成的 sn-ps 生成的一些 html,但不是我刚刚创建的api-guide.adoc src/main/asciidoc.
    • Asciidoctor Gradle 插件默认在src/docs/asciidoc 中查找,因此您的手写.adoc 文件应该放在那里。
    • 我也有同样的问题。 .adoc 文档在正确的位置,但仍然没有 html。
    • “在指定目录中生成”是什么意思?它在哪里指定?它不是 'src/main/asciidocs' 文件夹,是吗?
    • @DirkSchumacher 通过指定目录表示build/asciidoc/html5。我假设您已按照原样遵循文档。
    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多