首页 > 解决方案 > 无法在 docker 中构建 gradle 应用程序

问题描述

我正在尝试从 docker 容器中使用 gradle 构建应用程序。这是我的 Dockerfile:

FROM openjdk:8-jdk
#install git
RUN apt-get install -y git
RUN git clone https://github.com/SFRJ/yurl.git
#install gradle
RUN wget https://downloads.gradle-dn.com/distributions/gradle-6.5-bin.zip
RUN unzip gradle-6.5-bin.zip
ENV GRADLE_HOME /gradle-6.5
ENV PATH $PATH:/gradle-6.5/bin
#compile and run app
RUN cd yurl
RUN gradle clean build --rerun-tasks --no-build-cache
ENTRYPOINT ["java", "-jar", "/yurlapp.jar"]

一切顺利,直到执行构建命令为止。它抛出以下内容:

Step 9/10 : RUN gradle clean build --rerun-tasks --no-build-cache
 ---> Running in a25d344c3571

Welcome to Gradle 6.5!

Here are the highlights of this release:
 - Experimental file-system watching
 - Improved version ordering
 - New samples

For more details see https://docs.gradle.org/6.5/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project ''.
> The project name must not be empty. Set the 'rootProject.name' or adjust the 'include' statement (see https://docs.gradle.org/6.5/dsl/org.gradle.api.initialization.Settings.html#org.gradle.api.initialization.Settings:include(java.lang.String[]) for more details).

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
ERROR: Service 'yurlapp' failed to build: The command '/bin/sh -c gradle clean build --rerun-tasks --no-build-cache' returned a non-zero code: 1

我只是不明白它是什么。当我在 docker 之外运行相同的命令但在 docker 内部失败时,该应用程序构建得非常好。这不是一个多模块项目,不确定它为什么抱怨 rootProject。

我可以确认我有一个 settings.gradle 文件,里面有这个

rootProject.name = 'yurl'

这也是我的 build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.postgresql:postgresql:42.2.11"
    }
}

plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'org.flywaydb.flyway' version '6.3.0'
    id 'nu.studer.jooq' version '4.1'
}

group 'com.javing.yurl'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {

    annotationProcessor 'org.projectlombok:lombok:1.18.8'

    implementation 'org.jooq:jooq'
    implementation 'org.jooq:jooq-codegen'
    jooqRuntime 'org.postgresql:postgresql:42.2.11'
    compile 'org.postgresql:postgresql:42.2.11'
    implementation 'org.projectlombok:lombok:1.18.8'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-jooq'
    implementation 'io.vavr:vavr:0.10.2'
    implementation 'com.konghq:unirest-java:3.7.00'

    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
    testCompile group: 'org.assertj', name: 'assertj-core', version: '3.15.0'

}

jooq {
    sample(sourceSets.main) {
        jdbc {
            driver = 'org.postgresql.Driver'
            url = 'jdbc:postgresql://yurldb:5432/yurldb'
            user = 'postgres'
            password = 'somepassword'
        }
        generator {
            database() {
                name = 'org.jooq.meta.postgres.PostgresDatabase'
                inputSchema = 'public'
                includes = '.*'
            }
            target {
                packageName = 'com.javing.yurl'
                directory = 'build/generated/java'
            }
        }
    }
}

tasks.generateSampleJooqSchemaSource.with {
    def out = new ByteArrayOutputStream()
    javaExecSpec = { JavaExecSpec s ->
        s.standardOutput = out
        s.errorOutput = out
        s.ignoreExitValue = true
        s.jvmArgs '-Xmx512M'
    }
    execResultHandler = { ExecResult r ->
        if (r.exitValue != 0) {
            throw new RuntimeException('jOOQ source code generation failed:\n\n' + out.toString())
        }
    }
}

flyway {
    url = 'jdbc:postgresql://localhost:5432/yurldb'
    user = 'postgres'
    password = 'somepassword'
    schemas = ['public']
    locations = ["filesystem:$project.projectDir/src/main/resources/db/migration"]
}

我不知道如何解决这个问题,我尝试了多种方法,但没有任何效果。我需要使用 gradle 从 docker 内部构建这个应用程序。我知道 gradle 已正确安装,因为如果我在 Dockerfile 中添加版本命令(RUN gradle -v),我可以在 docker 运行时看到打印的内容:

------------------------------------------------------------
Gradle 6.5
------------------------------------------------------------

Build time:   2020-06-02 20:46:21 UTC
Revision:     a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4

Kotlin:       1.3.72
Groovy:       2.5.11
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          1.8.0_252 (Oracle Corporation 25.252-b09)
OS:           Linux 4.18.0-25-generic amd64

所以我的 gradle 和 gradle 配置似乎没问题。我也知道 git 的安装和项目的克隆很好,因为如果我在 Dockerfile 中添加一个RUN ls -s,它将正确打印项目的所有内容。

build.gradle 或 settings.gradle 文件中可能有问题。知道可能是什么吗?

标签: javaspringdockergradledockerfile

解决方案


你可以试试下面的Dockerfile,因为已经改变了一点。

FROM openjdk:8-jdk
#install git
RUN apt-get install -y git
RUN git clone https://github.com/SFRJ/yurl.git
#install gradle
RUN wget https://downloads.gradle-dn.com/distributions/gradle-6.5-bin.zip
RUN unzip gradle-6.5-bin.zip
ENV GRADLE_HOME /gradle-6.5
ENV PATH $PATH:/gradle-6.5/bin
#compile and run app
WORKDIR yurl
RUN gradle clean build --rerun-tasks --no-build-cache
ENTRYPOINT ["java", "-jar", "/yurlapp.jar"]

问题是,您提到了RUN cd yurl要更改目录的阶段,该目录仅对该特定阶段有效,对其余阶段无效。如果您还想将该特定目录用于其他阶段。用于WORKDIR运行我在上面所做的操作。

PS:- 如果您只想将该目录用于 1 个阶段,则不要在 1 个阶段本身WORKDIR使用类似命令。RUN cd DIR && ls -lart


推荐阅读