Java – The Docker Image created by Google JIB does not contain asciidoc for spring rest documents

The Docker Image created by Google JIB does not contain asciidoc for spring rest documents… here is a solution to the problem.

The Docker Image created by Google JIB does not contain asciidoc for spring rest documents

I use Spring Rest documentation and JIB

When I execute ./gradlew build and java -jar/some/build/libs/app.jar. I can get the API documentation for Spring REST documentation generation in example.com/docs/asciidocname.html.

But the docker image with ./gradlew jib does not contain this URL.

When I execute ./gradlew jib, I

want to get the API documentation generated by Spring Rest Docs

Here’s a part of my build.gradle

plugins {
    id "org.asciidoctor.convert" version "2.4.0"
    id "com.google.cloud.tools.jib" version "2.5.0"
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
    asciidoctor
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
    maven { url 'https://repo.spring.io/snapshot' }
}

sourceCompatibility = '11'

dependencies {
    /**
     * RestDocs
     */
    asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor'
    testImplementation('org.springframework.restdocs:spring-restdocs-mockmvc')
}

test {
    useJUnitPlatform {
        includeEngines 'junit-jupiter'
    }
}

/*************************
 * Rest Docs
 *************************/
asciidoctor {
    dependsOn test
}

bootJar {
    dependsOn asciidoctor
    from ("${asciidoctor.outputDir}/html5") {
        into 'static/docs'
    }
}

Solution

You have configured the bootJar task to depend on the asciidoctor task and include the generated HTML file:

bootJar {
    dependsOn asciidoctor
    from ("${asciidoctor.outputDir}/html5") {
        into 'static/docs'
    }
}

Jib does not use jar files when building container images, so you need to add a similar configuration for Jib.

Let’s first see how to make it contain the generated HTML. It provides an extension called jib where you can do this using extraDirectories:

jib {
    extraDirectories {
        paths {
            path {
                from = "${asciidoctor.outputDir}/html5"
                into = "/app/resources/static/docs"
            }
        }
    }
}

You can find it in the documentation for its Gradle plugin to learn more about adding files to images generated by Jib

Now we need to configure the jib task to depend on the asciidoctor task. This ensures that the HTML is generated before Jib attempts to include it in the image. Since both the extension and the task are named jib, we need to explicitly reference the task:

tasks.named('jib') {
    dependsOn asciidoctor
}

If you’ve ever built images to a local Docker daemon, you might want to make a similar configuration for the jibDockerBuild task:

jibDockerBuild {
    dependsOn asciidoctor
}

Related Problems and Solutions