build.gradle 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  3. * or more contributor license agreements. Licensed under the Elastic License
  4. * 2.0 and the Server Side Public License, v 1; you may not use this file except
  5. * in compliance with, at your election, the Elastic License 2.0 or the Server
  6. * Side Public License, v 1.
  7. */
  8. import org.gradle.internal.jvm.Jvm
  9. plugins {
  10. id 'java-gradle-plugin'
  11. id 'groovy'
  12. id 'java-test-fixtures'
  13. }
  14. group = 'org.elasticsearch.gradle'
  15. if (project == rootProject) {
  16. // change the build dir used during build init, so that doing a clean
  17. // won't wipe out the buildscript jar
  18. buildDir = 'build-bootstrap'
  19. }
  20. /*****************************************************************************
  21. * Propagating version.properties to the rest of the build *
  22. *****************************************************************************/
  23. // we update the version property to reflect if we are building a snapshot or a release build
  24. // we write this back out below to load it in the Build.java which will be shown in rest main action
  25. // to indicate this being a snapshot build or a release build.
  26. Properties props = VersionPropertiesLoader.loadBuildSrcVersion(project.file('version.properties'))
  27. version = props.getProperty("elasticsearch")
  28. gradlePlugin {
  29. // We already configure publication and we don't need or want the one that comes
  30. // with the java-gradle-plugin
  31. automatedPublishing = false
  32. }
  33. def generateVersionProperties = tasks.register("generateVersionProperties", WriteProperties) {
  34. outputFile = "${buildDir}/version.properties"
  35. comment = 'Generated version properties'
  36. properties(props)
  37. }
  38. tasks.named("processResources").configure {
  39. from(generateVersionProperties)
  40. }
  41. /*****************************************************************************
  42. * Java version *
  43. *****************************************************************************/
  44. if (JavaVersion.current() < JavaVersion.VERSION_11) {
  45. throw new GradleException('At least Java 11 is required to build elasticsearch gradle tools')
  46. }
  47. allprojects {
  48. apply plugin: 'java'
  49. targetCompatibility = '11'
  50. sourceCompatibility = '11'
  51. }
  52. sourceSets {
  53. integTest {
  54. compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
  55. runtimeClasspath += output + compileClasspath
  56. }
  57. }
  58. tasks.withType(JavaCompile).configureEach {
  59. options.encoding = 'UTF-8'
  60. }
  61. /*****************************************************************************
  62. * Dependencies used by the entire build *
  63. *****************************************************************************/
  64. repositories {
  65. mavenCentral()
  66. gradlePluginPortal()
  67. }
  68. dependencies {
  69. api localGroovy()
  70. api gradleApi()
  71. api 'commons-codec:commons-codec:1.12'
  72. api 'org.apache.commons:commons-compress:1.19'
  73. api 'org.apache.ant:ant:1.10.8'
  74. api 'com.netflix.nebula:gradle-extra-configurations-plugin:5.0.1'
  75. api 'com.netflix.nebula:gradle-info-plugin:9.2.0'
  76. api 'org.apache.rat:apache-rat:0.11'
  77. api "org.elasticsearch:jna:5.7.0-1"
  78. api 'com.github.jengelman.gradle.plugins:shadow:6.1.0'
  79. // When upgrading forbidden apis, ensure dependency version is bumped in ThirdPartyPrecommitPlugin as well
  80. api 'de.thetaphi:forbiddenapis:3.1'
  81. api 'com.avast.gradle:gradle-docker-compose-plugin:0.14.0'
  82. api 'org.apache.maven:maven-model:3.6.2'
  83. api 'com.networknt:json-schema-validator:1.0.36'
  84. api "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${props.getProperty('jackson')}"
  85. api 'org.ow2.asm:asm:9.0'
  86. api 'org.ow2.asm:asm-tree:9.0'
  87. api "org.apache.httpcomponents:httpclient:${props.getProperty('httpclient')}"
  88. api "org.apache.httpcomponents:httpcore:${props.getProperty('httpcore')}"
  89. compileOnly "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}"
  90. testImplementation "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}"
  91. testFixturesApi "junit:junit:${props.getProperty('junit')}"
  92. testFixturesApi "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
  93. testFixturesApi gradleApi()
  94. testFixturesApi gradleTestKit()
  95. testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone:2.23.2'
  96. testImplementation 'org.mockito:mockito-core:1.9.5'
  97. integTestImplementation('org.spockframework:spock-core:1.3-groovy-2.5') {
  98. exclude module: "groovy"
  99. }
  100. integTestImplementation "org.xmlunit:xmlunit-core:2.8.2"
  101. }
  102. /*****************************************************************************
  103. * Bootstrap repositories *
  104. *****************************************************************************/
  105. // this will only happen when buildSrc is built on its own during build init
  106. if (project == rootProject) {
  107. repositories {
  108. if (System.getProperty("repos.mavenLocal") != null) {
  109. mavenLocal()
  110. }
  111. }
  112. dependencies {
  113. // add this so the runtime classpath so Gradle will properly track it as a build runtime classpath input
  114. runtimeOnly project('reaper')
  115. }
  116. // only run tests as build-tools
  117. tasks.named("test").configure {
  118. enabled = false
  119. }
  120. }
  121. /*****************************************************************************
  122. * Normal project checks *
  123. *****************************************************************************/
  124. // this happens when included as a normal project in the build, which we do
  125. // to enforce precommit checks like forbidden apis, as well as setup publishing
  126. if (project != rootProject) {
  127. apply plugin: 'elasticsearch.build'
  128. apply plugin: 'elasticsearch.publish'
  129. // groovydoc succeeds, but has some weird internal exception...
  130. tasks.named("groovydoc").configure {
  131. enabled = false
  132. }
  133. // build-tools is not ready for primetime with these...
  134. tasks.named("dependencyLicenses").configure { enabled = false }
  135. tasks.named("dependenciesInfo").configure {enabled = false }
  136. tasks.named("dependenciesGraph").configure {enabled = false }
  137. disableTasks('forbiddenApisMain', 'forbiddenApisTest', 'forbiddenApisIntegTest', 'forbiddenApisTestFixtures')
  138. tasks.named("jarHell").configure {
  139. enabled = false
  140. }
  141. tasks.named("thirdPartyAudit").configure {
  142. enabled = false
  143. }
  144. configurations.register("distribution")
  145. configurations.register("reaper")
  146. dependencies {
  147. reaper project('reaper')
  148. distribution project(':distribution:archives:windows-zip')
  149. distribution project(':distribution:archives:darwin-tar')
  150. distribution project(':distribution:archives:darwin-aarch64-tar')
  151. distribution project(':distribution:archives:linux-aarch64-tar')
  152. distribution project(':distribution:archives:linux-tar')
  153. integTestRuntimeOnly(project(":libs:elasticsearch-core"))
  154. }
  155. eclipse {
  156. classpath {
  157. plusConfigurations += [configurations.integTestRuntimeClasspath]
  158. }
  159. }
  160. // for external projects we want to remove the marker file indicating we are running the Elasticsearch project
  161. tasks.named("processResources").configure {
  162. exclude 'buildSrc.marker'
  163. into('META-INF') {
  164. from configurations.reaper
  165. }
  166. }
  167. // Track reaper jar as a test input using runtime classpath normalization strategy
  168. tasks.withType(Test).configureEach {
  169. inputs.files(configurations.reaper).withNormalizer(ClasspathNormalizer)
  170. }
  171. normalization {
  172. runtimeClasspath {
  173. // We already include the reaper jar as part of our runtime classpath. Ignore the copy in META-INF.
  174. ignore('META-INF/reaper.jar')
  175. }
  176. }
  177. tasks.named("forbiddenPatterns").configure {
  178. exclude '**/*.wav'
  179. exclude '**/*.p12'
  180. exclude '**/*.jks'
  181. exclude '**/*.crt'
  182. // the file that actually defines nocommit
  183. exclude '**/ForbiddenPatternsTask.java'
  184. exclude '**/*.bcfks'
  185. }
  186. tasks.named("testingConventions") {
  187. naming.clear()
  188. naming {
  189. Tests {
  190. baseClass 'org.elasticsearch.gradle.test.GradleUnitTestCase'
  191. }
  192. IT {
  193. baseClass 'org.elasticsearch.gradle.test.GradleIntegrationTestCase'
  194. }
  195. }
  196. }
  197. tasks.register("integTest", Test) {
  198. inputs.dir(file("src/testKit")).withPropertyName("testkit dir").withPathSensitivity(PathSensitivity.RELATIVE)
  199. systemProperty 'test.version_under_test', version
  200. testClassesDirs = sourceSets.integTest.output.classesDirs
  201. classpath = sourceSets.integTest.runtimeClasspath
  202. }
  203. tasks.named("check").configure { dependsOn("integTest") }
  204. // for now we hardcode the tests for our build to use the gradle jvm.
  205. tasks.withType(Test).configureEach {
  206. onlyIf { org.elasticsearch.gradle.info.BuildParams.inFipsJvm == false }
  207. it.executable = Jvm.current().getJavaExecutable()
  208. maxParallelForks = providers.systemProperty('tests.jvms').forUseAtConfigurationTime().getOrElse(org.elasticsearch.gradle.info.BuildParams.defaultParallel.toString()) as Integer
  209. }
  210. publishing.publications.named("elastic").configure {
  211. suppressPomMetadataWarningsFor("testFixturesApiElements")
  212. suppressPomMetadataWarningsFor("testFixturesRuntimeElements")
  213. }
  214. }
  215. // Define this here because we need it early.
  216. class VersionPropertiesLoader {
  217. static Properties loadBuildSrcVersion(File input) throws IOException {
  218. Properties props = new Properties();
  219. InputStream is = new FileInputStream(input)
  220. try {
  221. props.load(is)
  222. } finally {
  223. is.close()
  224. }
  225. loadBuildSrcVersion(props, System.getProperties())
  226. return props
  227. }
  228. protected static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
  229. String elasticsearch = loadedProps.getProperty("elasticsearch")
  230. if (elasticsearch == null) {
  231. throw new IllegalStateException("Elasticsearch version is missing from properties.")
  232. }
  233. if (elasticsearch.matches("[0-9]+\\.[0-9]+\\.[0-9]+") == false) {
  234. throw new IllegalStateException(
  235. "Expected elasticsearch version to be numbers only of the form X.Y.Z but it was: " +
  236. elasticsearch
  237. )
  238. }
  239. String qualifier = systemProperties.getProperty("build.version_qualifier", "")
  240. if (qualifier.isEmpty() == false) {
  241. if (qualifier.matches("(alpha|beta|rc)\\d+") == false) {
  242. throw new IllegalStateException("Invalid qualifier: " + qualifier)
  243. }
  244. elasticsearch += "-" + qualifier
  245. }
  246. final String buildSnapshotSystemProperty = systemProperties.getProperty("build.snapshot", "true");
  247. switch (buildSnapshotSystemProperty) {
  248. case "true":
  249. elasticsearch += "-SNAPSHOT"
  250. break;
  251. case "false":
  252. // do nothing
  253. break;
  254. default:
  255. throw new IllegalArgumentException(
  256. "build.snapshot was set to [" + buildSnapshotSystemProperty + "] but can only be unset or [true|false]");
  257. }
  258. loadedProps.put("elasticsearch", elasticsearch)
  259. }
  260. }