无法从〜/ .gradle / gradle.properties读取Gradle属性

时间:2014-10-13 00:25:41

标签: android maven properties gradle

我遇到了类似的问题: http://gradle.1045684.n5.nabble.com/gradle-gradle-properties-not-being-read-td4372872.html

事实上,它是完全相同的问题 - 除了该线程的解决方案对我不起作用。我在OS / X上,使用gradle进行android构建。正在发生的事情的要点是使用以下〜/ .gradle / gradle.properties文件:

uploadUsername=scottj
uploadKeyFile=/home/scottjohnson/.ssh/uploadKey

以下build.gradle文件:

...跳过一般gradle和特定于android的东西......

task checkProperties << {
  if (!hasProperty('uploadUsername')) {
    throw new RuntimeException("Couldn't find uploadUsername property. Did you forget to specify it in ~/.gradle/gradle.properties?")
  } else if (!hasProperty('uploadKeyFile')) {
    throw new RuntimeException("Couldn't find uploadKeyFile property. Did you forget to specify it in ~/.gradle/gradle.properties?")
  }
  println("***** DEBUG_jwir3: " + project.getProperty('uploadUsername'))
}

当我运行./gradlew checkProperties时,我得到:

失败:构建因异常而失败。

  • 其中: 构建文件'/Users/scottjohnson/Source/core-android/crux/build.gradle'行:50

  • 出了什么问题: 任务':crux:checkProperties'执行失败。

      

    找不到uploadUsername属性。你忘了在〜/ .gradle / gradle.properties中指定它吗?

  • 尝试: 使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获得更多日志输出。

建立失败

总时间:7.072秒

更有趣的是,如果我指定我想要调试日志记录,我会得到以下内容:

mustafar:core-android scottjohnson$ ./gradlew checkProperties -d | grep properties
19:22:35.507 [DEBUG] [org.gradle.initialization.DefaultGradlePropertiesLoader] Found env project properties: []
19:22:35.508 [DEBUG] [org.gradle.initialization.DefaultGradlePropertiesLoader] Found system project properties: []
19:22:35.865 [DEBUG] [org.gradle.initialization.ProjectPropertySettingBuildLoader] Looking for project properties from: /Users/scottjohnson/Source/core-android/buildSrc/gradle.properties
19:22:37.161 [DEBUG] [org.gradle.initialization.DefaultGradlePropertiesLoader] Found env project properties: []
19:22:37.161 [DEBUG] [org.gradle.initialization.DefaultGradlePropertiesLoader] Found system project properties: []
19:22:37.200 [DEBUG] [org.gradle.initialization.ProjectPropertySettingBuildLoader] Looking for project properties from: /Users/scottjohnson/Source/core-android/gradle.properties
19:22:37.200 [DEBUG] [org.gradle.initialization.ProjectPropertySettingBuildLoader] Adding project properties (if not overwritten by user properties): [CORE_GROUP, CORE_VERSION_NAME, CORE_VERSION_CODE]
19:22:37.201 [DEBUG] [org.gradle.initialization.ProjectPropertySettingBuildLoader] Looking for project properties from: /Users/scottjohnson/Source/core-android/crux/gradle.properties

如您所见,它识别core-android / gradle.properties文件(CORE_GROUP,CORE_VERSION_NAME和CORE_VERSION_CODE)中的属性,但是我没有看到〜/ .gradle / gradle.properties中的任何一个属性被拉进来。

我不想在项目的gradle.properties中指定这些属性,因为它们是特定于用户的(用于推送到私有maven存储库的凭据)。

我做错了什么导致gradle.properties不被拉入?

1 个答案:

答案 0 :(得分:3)

在任务操作中,hasProperty将解析为Task#hasProperty。而是使用project.hasProperty(...)