我正在开发一个通过 Docker 在本地运行的应用程序。
Dockerfile
最重要的部分是:
FROM eclipse-temurin:17 as source
COPY . /data
WORKDIR /data
FROM source as build
RUN ./gradlew clean build --info -x test
如您所见,上面的 jar 是由主应用程序目录中存在的
gradle wrapper
构建的。到目前为止,我只使用中央 Maven 依赖项,所以在我的 build.gradle.kts
中,我有这样的部分:
repositories {
mavenCentral()
}
我当前的目标是添加一些本地依赖项。我在上面的配置中添加了
mavenLocal()
,并在dependencies
部分指定了依赖关系。 IntelliJ 可以将 jar 添加到 External Libraries
,但我不知道如何在容器中构建应用程序。我尝试将本地 .m2/repository
作为卷安装在 docker-compose.yml
中,如下所示:
volumes:
- ./config:/config
- ~/.m2/repository:/root/.m2/repository
我可以看到容器中存在相关的jar,但是构建过程的输出仍然是这样的:
47.23 FAILURE: Build failed with an exception.
47.23
47.23 * What went wrong:
47.23 Execution failed for task ':compileJava'.
47.23 > Could not resolve all files for configuration ':compileClasspath'.
47.23 > Could not find com.x:y:0.0.0-main-SNAPSHOT.
47.23 Required by:
47.23 project :
我缺少什么部分?我是否需要在
gradle
设置中添加更多内容来通知 gradlew
它应该在某个特定位置查找文件?我尝试用 mavenLocal()
(或 maven(url = "/root/.m2/repository")
)替换 maven(url = "~/.m2/repository"
,但也不起作用。我还使用 --info
标志运行构建,与此依赖项相关的唯一日志是:
50.36 Resource missing. [HTTP GET: https://repo.maven.apache.org/maven2/com/x/y/0.0.0-main-SNAPSHOT/maven-metadata.xml]
50.36 Resource missing. [HTTP GET: https://repo.maven.apache.org/maven2/com/x/y/0.0.0-main-SNAPSHOT/y-0.0.0-main-SNAPSHOT.pom]
如何将依赖项从本地 Maven 存储库传递到构建过程?
问题与 gradle 本身无关,而是与 Docker 有关 - 卷在此过程中安装得太晚,因此 gradle 无法解决依赖关系,因为
.m2
还不存在。我将存储库复制到主应用程序目录并修改了Dockerfile
,如下所示:
FROM eclipse-temurin:17 as source
COPY . /data
COPY ./repository /root/.m2/repository
WORKDIR /data
FROM source as build
COPY --from=source /root/.m2/repository /root/.m2/repository
RUN ./gradlew clean build -x test
一切都按预期进行。