You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrapper.gradle 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. // This file contains tasks for the gradle wrapper generation.
  20. // Ensure the wrapper script is generated based on the version defined in the project
  21. // and not the version installed on the machine running the task.
  22. // Read more about the wrapper here: https://docs.gradle.org/current/userguide/gradle_wrapper.html
  23. wrapper {
  24. gradleVersion = project.gradleVersion
  25. distributionType = Wrapper.DistributionType.ALL
  26. }
  27. // Custom task to inject support for downloading the gradle wrapper jar if it doesn't exist.
  28. // This allows us to avoid checking in the jar to our repository.
  29. // Additionally adds a license header to the wrapper while editing the file contents.
  30. task bootstrapWrapper() {
  31. // In the doLast block so this runs when the task is called and not during project configuration.
  32. doLast {
  33. def wrapperBasePath = "\$APP_HOME/gradle/wrapper"
  34. def wrapperJarPath = wrapperBasePath + "/gradle-wrapper.jar"
  35. // Add a trailing zero to the version if needed.
  36. def fullVersion = project.gradleVersion.count(".") == 1 ? "${project.gradleVersion}.0" : versions.gradle
  37. // Leverages the wrapper jar checked into the gradle project on github because the jar isn't
  38. // available elsewhere. Using raw.githubusercontent.com instead of github.com because
  39. // github.com servers deprecated TLSv1/TLSv1.1 support some time ago, so older versions
  40. // of curl (built against OpenSSL library that doesn't support TLSv1.2) would fail to
  41. // fetch the jar.
  42. def wrapperBaseUrl = "https://raw.githubusercontent.com/gradle/gradle/v$fullVersion/gradle/wrapper"
  43. def wrapperJarUrl = wrapperBaseUrl + "/gradle-wrapper.jar"
  44. def bootstrapString = """
  45. # Loop in case we encounter an error.
  46. for attempt in 1 2 3; do
  47. if [ ! -e "$wrapperJarPath" ]; then
  48. if ! curl -s -S --retry 3 -L -o "$wrapperJarPath" "$wrapperJarUrl"; then
  49. rm -f "$wrapperJarPath"
  50. # Pause for a bit before looping in case the server throttled us.
  51. sleep 5
  52. continue
  53. fi
  54. fi
  55. done
  56. """.stripIndent()
  57. def wrapperScript = wrapper.scriptFile
  58. def wrapperLines = wrapperScript.readLines()
  59. wrapperScript.withPrintWriter { out ->
  60. def bootstrapWritten = false
  61. wrapperLines.each { line ->
  62. // Print the wrapper bootstrap before the first usage of the wrapper jar.
  63. if (!bootstrapWritten && line.contains("gradle-wrapper.jar")) {
  64. out.println(bootstrapString)
  65. bootstrapWritten = true
  66. }
  67. out.print(line)
  68. out.println()
  69. }
  70. }
  71. }
  72. }
  73. wrapper.finalizedBy bootstrapWrapper
  74. // Remove the generated batch file since we don't test building in the Windows environment.
  75. task removeWindowsScript(type: Delete) {
  76. delete "$rootDir/gradlew.bat"
  77. }
  78. wrapper.finalizedBy removeWindowsScript