gradle - 如何使用自定义Gradle构建文件和Gradle设置文件

标签 gradle build

gradle命令有一堆标志来定制其环境[1]。其中包括--build-file--settings-file。但是我似乎无法让他们按照我期望的方式工作。

我期望以下工作

$ cat <<-EOF > alt-settings.gradle
    rootProject.name = 'custom-name'
EOF
$ cat <<-EOF > alt-build.gradle
    task test { /* ... */ }
EOF
$ gradle \
    --settings-file alt-settings.gradle \
    --build-file alt-build.gradle \
    tasks --all

但这引发了异常
Build file './alt-build.gradle' is not part of the build defined by settings file './alt-settings.gradle'. If this is an unrelated build, it must have its own settings file.

如果我从上面的命令中省略了--settings-file,那么事情就起作用了,而gradle会选择alt-build.gradle

出了什么问题,我该如何解决?

理想情况下,即使有一个settings.gradle文件不起作用,我也希望能够运行gradle。例如在下面的senario中

$ cat <<-EOF > alt-build.gradle
    task test { /* ... */ }
EOF
$ cat <<-EOF > alt-settings.gradle
    rootProject.name = 'custom-name'
EOF

$ cat <<-EOF > settings.gradle
    This will not compile
EOF
$ cat <<-EOF > build.gradle
    This will not compile
EOF

$ gradle \
    --settings-file alt-settings.gradle \
    --build-file alt-build.gradle \
    tasks --all

[1] https://docs.gradle.org/current/userguide/command_line_interface.html#environment_options

最佳答案

您需要在自定义设置文件中配置自定义构建文件,然后仅在调用gradle时使用--settings-file参数。
alt-settings.gradle:

rootProject.name = 'custom-name'
rootProject.buildFileName = 'alt-build.gradle'
alt-build.gradle:
task test { /* ... */ }
gradle电话:
gradle --settings-file alt-settings.gradle tasks --all
这样,根本不使用默认的构建和设置文件,并且它们是否起作用也无关紧要。

关于gradle - 如何使用自定义Gradle构建文件和Gradle设置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61544790/

相关文章:

android - Android Studio将文件下载到的目录位于什么目录?

gradle - Gradle找不到我要建立的子目录

c++ - 查找二进制到 C/C++ 的链接

c++ - 编译 SDL_ttf 时未定义对 WinMain@16 的引用?

build - 为什么本地应用程序的构建时间受网络影响?

gradle - Libgdx V1.3.1 启动画面补间错误 : Exception in thread "LWJGL Application" java. lang.RuntimeException: No TweenAccessor was found for the target

gradle - 如何将 Gradle 中的 srcPaths 设置为子目录集合中的特定子目录

c++ - 在我的 main.cpp 中使用外部类

javascript - ng build -prod 与 ng build --prod --build-optimizer=true

spring - 将Spring Boot RESTful Web Service部署到Tomcat时,Web.xml仍然丢失