简体   繁体   English

Gradle Docker 构建问题

[英]Gradle Docker Build Issues

I have a Dockerfile with the following contents:我有一个包含以下内容的 Dockerfile:

FROM gradle:6.9.1-jdk17 as builder
COPY services/api/build.gradle.kts ./
COPY services/api/src ./
COPY services/api/settings.gradle.kts ./
RUN gradle build --no-daemon

FROM openjdk:8-jre-slim
COPY --from=builder /home/* /home
ENTRYPOINT [ "java", "-jar", "./home/build/libs/api.jar" ]

When I copy from builder using COPY --from=builder /home/* /home it works with no issue and if I sh into the image I can see that there is a folder structure inside the home folder including build/libs/api.jar .当我从制造商使用复制COPY --from=builder /home/* /home它的工作原理没有问题,如果我sh成图像,我可以看到,有内部的文件夹结构home文件夹,包括build/libs/api.jar However I want to copy just the JAR file , so I want to run COPY --from=builder /home/build/libs/api.jar .但是我只想复制JAR文件,所以我想运行COPY --from=builder /home/build/libs/api.jar . which should allow me to simply call ENTRYPOINT [ "java", "-jar", "./api.jar" ] instead of the fully qualified ENTRYPOINT [ "java", "-jar", "./home/build/libs/api.jar" ] .这应该允许我简单地调用ENTRYPOINT [ "java", "-jar", "./api.jar" ]而不是完全限定的ENTRYPOINT [ "java", "-jar", "./home/build/libs/api.jar" ]

Secondly, when I try running the image the container says Error: Could not find or load main class com.library.api.MainKt .其次,当我尝试运行图像时,容器显示Error: Could not find or load main class com.library.api.MainKt So I tried building the api.jar file locally using the gradle build --no-daemon command that I am running in my dockerfile and reducing the dockerfile 's contents to simply copy that file from the local build folder (omitting the gradle build altogether) and this runs it perfectly.因此,我尝试使用我在dockerfile运行的gradle build --no-daemon命令在本地构建api.jar文件,并减少dockerfile的内容以简单地从本地构建文件夹复制该文件(完全省略 gradle 构建),这完美地运行它。

I'm tearing my hair out on this issue.我正在为这个问题而烦恼。 My gut says it's gradle not building the jar in the same way in the dockerfile as it is on my machine, but the versions are the same and I am struggling to see what else could be different.我的直觉说它在 dockerfile 中构建 jar 的方式与在我的机器上不同,这是 gradle,但版本是相同的,我正在努力寻找其他可能不同的地方。

For reference, my build.gradle.kts contents are as follows:作为参考,我的build.gradle.kts内容如下:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.31"
    kotlin("plugin.serialization") version "1.4.31"
}

group "com.cabanta"

repositories {
    mavenCentral()
}

dependencies {
  ...
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "15"
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.library.api.MainKt"
    }
    exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")

    // To add all of the dependencies otherwise a "NoClassDefFoundError" error
    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

If you need anything else to be able to answer, please let me know.如果您还需要什么才能回答,请告诉我。

Thanks in advance for any help you wonderful people can give.预先感谢您提供的任何帮助。

a final piece of pertinent information may be that my main class doesn't have a class, it simply has a main() as a function (as should be allowed in Kotlin and like I said, it builds and works when running through gradle on the local machine)最后一条相关信息可能是我的主类没有类,它只有一个main()作为函数(在 Kotlin 中应该允许,就像我说的那样,它在运行 gradle 时构建并运行)本地机器)

The issue turned out to be the way that I was copying files from local to the gradle step.问题原来是我将文件从本地复制到 gradle 步骤的方式。

COPY services/api/build.gradle.kts ./
COPY services/api/src ./
COPY services/api/settings.gradle.kts ./

This, I have now changed to COPY services/api/* ./ , which copies all of the build files that are needed.现在,我已更改为COPY services/api/* ./ ,它复制所有需要的构建文件。

The API before, was being created, however it was being created without any content, which meant that Java couldn't find the class ... because it was never there in the first place.之前的 API 正在创建,但是它是在没有任何内容的情况下创建的,这意味着 Java 无法找到该类……因为它从一开始就不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM