由Google JIB创建的Docker镜像未包含Spring Rest Docs的asciidoc部分。

huangapple go评论73阅读模式
英文:

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

问题

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'
    }
}
英文:

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'
    }
}

答案1

得分: 2

你已经配置了 `bootJar` 任务依赖于 `asciidoctor` 任务,并包含生成的 HTML 文件:

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

当 Jib 构建容器镜像时,它不会使用 jar 文件,因此您需要向 Jib 添加类似的配置。

首先,让我们看看如何让它包含生成的 HTML。它提供了一个名为 jib 的扩展,您可以使用 extraDirectories 来实现:

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

您可以在Jib Gradle 插件的文档中了解有关向 Jib 生成的镜像中添加文件的更多信息。

现在,我们需要配置 jib 任务依赖于 asciidoctor 任务。这确保在 Jib 尝试将其包含在镜像中之前,HTML 已经生成。由于扩展和任务都命名为 jib,我们需要明确引用任务:

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

如果您曾经将镜像构建到本地的 Docker 守护程序中,您可能还希望对 jibDockerBuild 任务进行类似的配置:

jibDockerBuild {
    dependsOn asciidoctor
}

<details>
<summary>英文:</summary>
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&#39;t use the jar file when it&#39;s building the container image so you need to add similar configuration to Jib.
Let&#39;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](https://github.com/GoogleContainerTools/jib/tree/8862aa3e25662a9f5a1f2a7cd39fd23278eb6c52/jib-gradle-plugin#adding-arbitrary-files-to-the-image).
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
}


</details>
# 答案2
**得分**: 1
和我发现另一种方法。
首先,JIB 通过 `java -cp` 命令执行一个主 Java 类,而不是打包的 `.jar` 文件。
而且 asciidoctor 任务将 html 文件复制到 jar 文件中。
并且我可以在 jib gradle 的 GitHub 页面找到选项。
1. 如果你使用 `containerizingMode = 'packaged'` 选项,它将构建 jar 文件并在 Docker 容器中执行 `java -cp` 命令。
2. 使用这个选项的 `./gradlew jib` 任务将执行 `jar` 任务。
所以我将我的 bootJar 任务复制到 jar 任务中,并使用那个选项。效果很好。
<details>
<summary>英文:</summary>
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 = &#39;packaged&#39;` 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
</details>

huangapple
  • 本文由 发表于 2020年10月4日 00:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64186363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定