Docker Image created by Google JIB did not include asciidoc of spring rest docs

878 views Asked by At

I use Spring Rest Docs and JIB

When i do ./gradlew build and java -jar /some/build/libs/app.jar. I can get api documents generated by spring rest docs at example.com/docs/asciidocname.html.

but docker image with ./gradlew jib does not contain this url.

I want to get Api Document that is generated by Spring Rest Docs When i do ./gradlew jib

the below is 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'
    }
}

2

There are 2 answers

0
Andy Wilkinson On BEST ANSWER

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

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

Jib doesn't use the jar file when it's building the container image so you need to add similar configuration to Jib.

Let's look at getting it to include the generated HTML first. It provides an extension, named jib, where you can use extraDirectories to do this:

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

You can learn more about adding files to the image that Jib generates in the documentation for its Gradle plugin.

Now we need to configure the jib task to depend on the asciidoctor task. This ensures that the HTML has been generated before Jib tries to include it in the image. As both the extension and the task are named jib, we need to explicitly refer to the task:

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

If you ever build the image to your local Docker daemon, you may want similar configuration for the jibDockerBuild task as well:

jibDockerBuild {
    dependsOn asciidoctor
}
0
labyu On

and I found out another way.

first. JIB execute a main java class through java -cp command. not packaged .jar file.

and asciidoctor task copy html files into jar file.

and i can find options at jib gradle github.

  1. if you use containerizingMode = 'packaged' option. it would build jar files and execute java -cp command in docker container.
  2. and ./gradlew jib task with this option will execute jar task.

So i copy my bootJar tasks to jar task and use that option. it works good