为什么我的 gradle 构建失败但 gradlew 构建成功?

why is my gradle build failing but gradlew build is successsful?

我有一个要构建的 gradle 项目。

当我执行 gradle clean build 时,它失败并显示以下错误消息

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':FacebookUpdateDependencies'.
> Could not resolve all dependencies for configuration ':compiler'.
   > Could not resolve com.company.facebook:compiler:1.2+.
     Required by:
         ::1.0
      > Could not resolve com.company.facebook:compiler:1.2+.
         > Failed to list versions for com.company.facebook:compiler.
            > Unable to load Maven meta-data from http://mycompanynexus/compiler/maven-metadata.xml.
               > Could not GET 'http://mycompanynexus/compiler/maven-metadata.xml'. Received status code 500 from server: Internal Server Error

然而,当我尝试 ./gradlew clean build构建成功。

为什么前者构建失败而不是后者?

这里是 build.gradle

的相关部分
task wrapper(type: Wrapper) {
    gradleVersion = '2.11'
}

编辑

local$: gradle -v

------------------------------------------------------------
Gradle 2.13
------------------------------------------------------------

Build time:   2016-04-25 04:10:10 UTC
Build number: none
Revision:     3b427b1481e46232107303c90be7b05079b05b1c

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.7.0_79 (Oracle Corporation 24.79-b02)
OS:           Mac OS X 10.11.1 x86_64

local$ ./gradlew -v

------------------------------------------------------------
Gradle 1.12
------------------------------------------------------------

Build time:   2014-04-29 09:24:31 UTC
Build number: none
Revision:     a831fa866d46cbee94e61a09af15f9dd95987421

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
Ivy:          2.2.0
JVM:          1.7.0_79 (Oracle Corporation 24.79-b02)
OS:           Mac OS X 10.11.1 x86_64

这告诉您当调用 gradlew 命令时将使用您的 Gradle 版本 2.11,而不是系统上安装的那个。总是 运行 gradlew 命令,因为它确保你的构建是 运行 相对于它应该的 gradle 版本,你永远不需要安装正确版本的 gradle 并真正加快了构建时间。

这是 Gradle 包装器定义的摘录:

Most tools require installation on your computer before you can use them. If the installation is easy, you may think that’s fine. But it can be an unnecessary burden on the users of the build. Equally importantly, will the user install the right version of the tool for the build? What if they’re building an old version of the software?

The Gradle Wrapper (henceforth referred to as the “Wrapper”) solves both these problems and is the preferred way of starting a Gradle build. If a Gradle project has set up the Wrapper (and we recommend all projects do so), you can execute the build using one of the following commands from the root of the project:

./gradlew(在类 Unix 平台上,例如 Linux 和 Mac OS X) gradlew(在 Windows 上使用 gradlew.bat 批处理文件) 每个 Wrapper 都绑定到 Gradle 的特定版本,因此当您首先 运行 上面的命令之一针对给定的 Gradle 版本时,它将下载 相应的 Gradle 发行版并使用它来执行构建。

我刚刚注意到包装器版本是 1.12 而不是 2.12。也就是wrapper和gradle的安装版本有非常大的版本差异。在过去 2 年中,不同的 gradle 插件可能有许多不兼容的更改,这可能会导致您观察到的构建问题。

如前所述,gradle 包装器是 gradle 的单独版本,位于项目的 gradle/wrapper 文件夹中。在您的项目 (gradle wrapper) 中执行 wrapper 任务时,包装器只有 built/updated。那是你的项目的包装器在过去 2 年内没有更新,或者最初是用错误的版本创建的(1.12 而不是 2.11)。

鉴于上述情况,很难说出真正导致构建失败的原因 - 除了 gradle 1.122.13 之间的不兼容性。但是这个版本不匹配可能是你问题的根本原因。

一旦您使用版本 2.11(构建文件中提到的版本)重新生成包装器,很可能您的项目将不再使用包装器构建,因为 2.11 可能不兼容也 1.12...