我有一个存储库,我想在其中稍微分离一下源代码和文档。所以我的目录结构是这样的:
/
- .gitlab-ci.yml
- readme.md
+ doc/
- doc_files.txt
+ src/
+ server/
- gradlew
- source_code.java
因此,从回购的根源来看,我需要执行./src/server/gradlew build
来构建项目。
现在我需要通过Gitlab CI触发gradle构建,也就是说,我会准确地执行所述命令。这就是我对gitlab-ci.yml
:的设想
stages:
- build
server_build:
stage: build
image: gradle:7.5.1-jdk18-alpine
script:
- './gradlew build'
不管怎样,gradlew
似乎不喜欢从任何其他目录执行,并失败如下:
> ./src/server/gradlew build
FAILURE: Build failed with an exception.
* What went wrong:
Directory '/home/user/git/project' does not contain a Gradle build.
A Gradle build should contain a 'settings.gradle' or 'settings.gradle.kts' file in its root directory. It may also contain a 'build.gradle' or 'build.gradle.kts' file.
//...
BUILD FAILED in 668ms
因此,我尝试用-p
设置目录和/或用-b
直接指向src/server/
中的settings.gradle-无论如何,我会得到以下错误:
> ./src/server/gradlew -b settings.gradle -p src/server/ build
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'server'.
> Could not open proj generic class cache for build file '/home/user/git/project/src/server/settings.gradle' (/home/user/.gradle/caches/7.5.1/scripts/qpzgz2vuqu1er7i9j90pmbyq).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 63
// ...
BUILD FAILED in 667ms
而且,很明显,Gitlab CI失败了。因此,在.gitlab-ci.yml
:中执行gradlew
之前,我尝试将Gitlab CI的cd
告知目录/src/server/
cd src/server && ./gradlew build
无论如何,这也不起作用,并且像这样失败(Gitlab CI中运行者的工作输出(:
// ...
$ cd src/server && ./gradlew build
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1
在现有目录结构的情况下,我如何告诉Gitlab CI从存储库的根目录使用gradlew构建?
是否可以在.gitlab-ci.yml文件的before_script中导出GRADLE_USER_HOME
。
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
您可以在获取参考配置https://gist.github.com/daicham/5ac8461b8b49385244aa0977638c3420#file-gitlab ci yml
所以,我实际上是通过偶然发现解决方案来解决的。为了切入正题,我不得不重新添加文件gradle/wrapper/gradle-wrapper.jar
,由于.gitignore
,该文件没有上传。
总而言之,gitlab-ci.yml
看起来是这样的:
stages:
- build
server_build:
stage: build
image: gradle:7.5.1-jdk18-alpine
script:
- cd src/server
- ./gradlew build
gitlab-ci.yml
位于存储库的根目录中,整个Java/Spring Boot项目位于/src/server/
中。这包括gradle.build
、gradlew
等等(特别是:从Spring Initializer中获得的所有内容https://start.spring.io/)。
因此,毕竟不需要任何路径配置。
您应该尝试在根目录中设置setting.gradle文件,并在src/server目录中设置build.gradle。使用-p应该可以正常工作。