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/