gpt4 book ai didi

java - 无法在 docker 内构建 gradle 应用程序

转载 作者:行者123 更新时间:2023-12-02 19:17:43 46 4
gpt4 key购买 nike

我正在尝试从 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 文件中可能有问题。知道会是什么吗?

最佳答案

您可以尝试一下下面的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 来运行我上面执行的操作。

P.S.:- 如果您只想将该目录用于 1 个阶段,则不要使用 WORKDIR,而是使用 RUN cd DIR && ls -lart ,就像 1 个阶段本身中的命令一样.

关于java - 无法在 docker 内构建 gradle 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62231660/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com