build.gradle 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Licensed to Elasticsearch under one or more contributor
  3. * license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright
  5. * ownership. Elasticsearch licenses this file to you under
  6. * the Apache License, Version 2.0 (the "License"); you may
  7. * not use this file except in compliance with the License.
  8. * 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. import org.gradle.util.GradleVersion
  20. import java.util.regex.Matcher
  21. plugins {
  22. id 'java-gradle-plugin'
  23. id 'groovy'
  24. }
  25. group = 'org.elasticsearch.gradle'
  26. String minimumGradleVersion = file('src/main/resources/minimumGradleVersion').text.trim()
  27. if (GradleVersion.current() < GradleVersion.version(minimumGradleVersion)) {
  28. throw new GradleException("Gradle ${minimumGradleVersion}+ is required to build elasticsearch")
  29. }
  30. if (project == rootProject) {
  31. // change the build dir used during build init, so that doing a clean
  32. // won't wipe out the buildscript jar
  33. buildDir = 'build-bootstrap'
  34. }
  35. /*****************************************************************************
  36. * Propagating version.properties to the rest of the build *
  37. *****************************************************************************/
  38. // we update the version property to reflect if we are building a snapshot or a release build
  39. // we write this back out below to load it in the Build.java which will be shown in rest main action
  40. // to indicate this being a snapshot build or a release build.
  41. File propsFile = project.file('version.properties')
  42. Properties props = VersionPropertiesLoader.loadBuildSrcVersion(propsFile)
  43. version = props.getProperty("elasticsearch")
  44. processResources {
  45. inputs.file(propsFile)
  46. // We need to be explicit with the version because we add snapshot and qualifier to it based on properties
  47. inputs.property("dynamic_elasticsearch_version", props.getProperty("elasticsearch"))
  48. doLast {
  49. Writer writer = file("$destinationDir/version.properties").newWriter()
  50. try {
  51. props.store(writer, "Generated version properties")
  52. } finally {
  53. writer.close()
  54. }
  55. }
  56. }
  57. /*****************************************************************************
  58. * Java version *
  59. *****************************************************************************/
  60. if (JavaVersion.current() < JavaVersion.VERSION_11) {
  61. throw new GradleException('At least Java 11 is required to build elasticsearch gradle tools')
  62. }
  63. // Gradle 4.10 does not support setting this to 11 yet
  64. targetCompatibility = "10"
  65. sourceCompatibility = "10"
  66. // We have a few classes that need to be compiled for older java versions because these are used to run checks against
  67. // those
  68. sourceSets {
  69. minimumRuntime {
  70. // We only want Java here, but the Groovy doesn't configure javadoc correctly if we don't define this as groovy
  71. groovy {
  72. srcDirs = ['src/main/minimumRuntime']
  73. }
  74. }
  75. }
  76. compileMinimumRuntimeGroovy {
  77. // We can't use BuildPlugin here, so read from file
  78. String minimumRuntimeVersion = file('src/main/resources/minimumRuntimeVersion').text.trim()
  79. targetCompatibility = minimumRuntimeVersion
  80. sourceCompatibility = minimumRuntimeVersion
  81. }
  82. dependencies {
  83. if (project.ext.has("isEclipse") == false || project.ext.isEclipse == false) {
  84. // eclipse is confused if this is set explicitly
  85. compile sourceSets.minimumRuntime.output
  86. }
  87. minimumRuntimeCompile "junit:junit:${props.getProperty('junit')}"
  88. minimumRuntimeCompile localGroovy()
  89. minimumRuntimeCompile gradleApi()
  90. }
  91. jar {
  92. from sourceSets.minimumRuntime.output
  93. }
  94. /*****************************************************************************
  95. * Dependencies used by the entire build *
  96. *****************************************************************************/
  97. repositories {
  98. jcenter()
  99. }
  100. dependencies {
  101. compile localGroovy()
  102. compile "com.carrotsearch.randomizedtesting:junit4-ant:${props.getProperty('randomizedrunner')}"
  103. compile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
  104. compile 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3'
  105. compile 'com.netflix.nebula:nebula-publishing-plugin:4.4.4'
  106. compile 'com.netflix.nebula:gradle-info-plugin:3.0.3'
  107. compile 'org.eclipse.jgit:org.eclipse.jgit:3.2.0.201312181205-r'
  108. compile 'com.perforce:p4java:2012.3.551082' // THIS IS SUPPOSED TO BE OPTIONAL IN THE FUTURE....
  109. compile 'org.apache.rat:apache-rat:0.11'
  110. compile "org.elasticsearch:jna:4.5.1"
  111. compile 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
  112. compile 'de.thetaphi:forbiddenapis:2.6'
  113. compile 'com.avast.gradle:gradle-docker-compose-plugin:0.8.12'
  114. testCompile "junit:junit:${props.getProperty('junit')}"
  115. }
  116. // Gradle 2.14+ removed ProgressLogger(-Factory) classes from the public APIs
  117. // Use logging dependency instead
  118. // Gradle 4.3.1 stopped releasing the logging jars to jcenter, just use the last available one
  119. GradleVersion logVersion = GradleVersion.current() > GradleVersion.version('4.3') ? GradleVersion.version('4.3') : GradleVersion.current()
  120. dependencies {
  121. compileOnly "org.gradle:gradle-logging:${logVersion.getVersion()}"
  122. }
  123. /*****************************************************************************
  124. * Bootstrap repositories *
  125. *****************************************************************************/
  126. // this will only happen when buildSrc is built on its own during build init
  127. if (project == rootProject) {
  128. repositories {
  129. if (System.getProperty("repos.mavenLocal") != null) {
  130. mavenLocal()
  131. }
  132. }
  133. // only run tests as build-tools
  134. test.enabled = false
  135. }
  136. /*****************************************************************************
  137. * Normal project checks *
  138. *****************************************************************************/
  139. // this happens when included as a normal project in the build, which we do
  140. // to enforce precommit checks like forbidden apis, as well as setup publishing
  141. if (project != rootProject) {
  142. apply plugin: 'elasticsearch.build'
  143. apply plugin: 'nebula.maven-base-publish'
  144. apply plugin: 'nebula.maven-scm'
  145. // we need to apply these again to override the build plugin
  146. targetCompatibility = "10"
  147. sourceCompatibility = "10"
  148. // groovydoc succeeds, but has some weird internal exception...
  149. groovydoc.enabled = false
  150. // build-tools is not ready for primetime with these...
  151. dependencyLicenses.enabled = false
  152. dependenciesInfo.enabled = false
  153. forbiddenApisMain.enabled = false
  154. forbiddenApisTest.enabled = false
  155. forbiddenApisMinimumRuntime.enabled = false
  156. jarHell.enabled = false
  157. thirdPartyAudit.enabled = false
  158. configurations {
  159. distribution
  160. }
  161. dependencies {
  162. distribution project(':distribution:archives:windows-zip')
  163. distribution project(':distribution:archives:oss-windows-zip')
  164. distribution project(':distribution:archives:darwin-tar')
  165. distribution project(':distribution:archives:oss-darwin-tar')
  166. distribution project(':distribution:archives:linux-tar')
  167. distribution project(':distribution:archives:oss-linux-tar')
  168. }
  169. String localDownloads = "${rootProject.buildDir}/local-downloads"
  170. task setupLocalDownloads(type:Copy) {
  171. from configurations.distribution
  172. into localDownloads
  173. }
  174. unitTest {
  175. // The test task is configured to runtimeJava version, but build-tools doesn't support all of them, so test
  176. // with compiler instead on the ones that are too old.
  177. if (project.runtimeJavaVersion <= JavaVersion.VERSION_1_10) {
  178. jvm = "${project.compilerJavaHome}/bin/java"
  179. }
  180. }
  181. // This can't be an RandomizedTestingTask because we can't yet reference it
  182. task integTest(type: Test) {
  183. // integration test requires the local testing repo for example plugin builds
  184. dependsOn project.rootProject.allprojects.collect {
  185. it.tasks.matching { it.name == 'publishNebulaPublicationToLocalTestRepository'}
  186. }
  187. dependsOn setupLocalDownloads
  188. exclude "**/*Tests.class"
  189. testClassesDirs = sourceSets.test.output.classesDirs
  190. classpath = sourceSets.test.runtimeClasspath
  191. inputs.dir(file("src/testKit"))
  192. // tell BuildExamplePluginsIT where to find the example plugins
  193. systemProperty (
  194. 'test.build-tools.plugin.examples',
  195. files(
  196. project(':example-plugins').subprojects.collect { it.projectDir }
  197. ).asPath,
  198. )
  199. systemProperty 'test.local-test-repo-path', "${rootProject.buildDir}/local-test-repo"
  200. systemProperty 'test.local-test-downloads-path', localDownloads
  201. systemProperty 'test.version_under_test', version
  202. Matcher isLuceneSnapshot = (/\w+-snapshot-([a-z0-9]+)/ =~ versions.lucene)
  203. if (isLuceneSnapshot) {
  204. systemProperty 'test.lucene-snapshot-revision', isLuceneSnapshot[0][1]
  205. }
  206. String defaultParallel = System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
  207. if (defaultParallel == "auto") {
  208. defaultParallel = Math.max(Runtime.getRuntime().availableProcessors(), 4)
  209. }
  210. maxParallelForks defaultParallel as Integer
  211. }
  212. check.dependsOn(integTest)
  213. // TODO: re-enable once randomizedtesting gradle code is published and removed from here
  214. licenseHeaders.enabled = false
  215. forbiddenPatterns {
  216. exclude '**/*.wav'
  217. exclude '**/*.p12'
  218. // the file that actually defines nocommit
  219. exclude '**/ForbiddenPatternsTask.java'
  220. }
  221. testingConventions {
  222. naming.clear()
  223. naming {
  224. Tests {
  225. baseClass 'org.elasticsearch.gradle.test.GradleUnitTestCase'
  226. }
  227. IT {
  228. baseClass 'org.elasticsearch.gradle.test.GradleIntegrationTestCase'
  229. }
  230. }
  231. }
  232. /*
  233. * We alread configure publication and we don't need or want this one that
  234. * comes from the java-gradle-plugin.
  235. */
  236. afterEvaluate {
  237. generatePomFileForPluginMavenPublication.enabled = false
  238. }
  239. }
  240. // Define this here because we need it early.
  241. class VersionPropertiesLoader {
  242. static Properties loadBuildSrcVersion(File input) throws IOException {
  243. Properties props = new Properties();
  244. InputStream is = new FileInputStream(input)
  245. try {
  246. props.load(is)
  247. } finally {
  248. is.close()
  249. }
  250. loadBuildSrcVersion(props, System.getProperties())
  251. return props
  252. }
  253. protected static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
  254. String elasticsearch = loadedProps.getProperty("elasticsearch")
  255. if (elasticsearch == null) {
  256. throw new IllegalStateException("Elasticsearch version is missing from properties.")
  257. }
  258. if (elasticsearch.matches("[0-9]+\\.[0-9]+\\.[0-9]+") == false) {
  259. throw new IllegalStateException(
  260. "Expected elasticsearch version to be numbers only of the form X.Y.Z but it was: " +
  261. elasticsearch
  262. )
  263. }
  264. String qualifier = systemProperties.getProperty("build.version_qualifier", "");
  265. if (qualifier.isEmpty() == false) {
  266. if (qualifier.matches("(alpha|beta|rc)\\d+") == false) {
  267. throw new IllegalStateException("Invalid qualifier: " + qualifier)
  268. }
  269. elasticsearch += "-" + qualifier
  270. }
  271. if ("true".equals(systemProperties.getProperty("build.snapshot", "true"))) {
  272. elasticsearch += "-SNAPSHOT"
  273. }
  274. loadedProps.put("elasticsearch", elasticsearch)
  275. }
  276. }