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.

build.gradle 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import groovy.json.JsonBuilder
  2. import groovy.json.JsonSlurper
  3. import org.apache.tools.ant.filters.ReplaceTokens
  4. import org.sonar.build.LicenseReader
  5. plugins {
  6. id "com.github.hierynomus.license-report"
  7. id "com.github.johnrengelman.shadow"
  8. id "de.undercouch.download"
  9. id "org.cyclonedx.bom"
  10. }
  11. sonar {
  12. properties {
  13. property 'sonar.projectName', "${projectTitle} :: Application"
  14. }
  15. }
  16. configurations {
  17. zipDist
  18. zip
  19. scanner
  20. web
  21. shutdowner
  22. jdbc_mssql {
  23. transitive = false
  24. }
  25. jdbc_postgresql {
  26. transitive = false
  27. }
  28. jdbc_h2 {
  29. transitive = false
  30. }
  31. bundledPlugin {
  32. transitive = false
  33. }
  34. bundledPlugin_deps {
  35. extendsFrom bundledPlugin
  36. transitive = true
  37. }
  38. appLicenses.extendsFrom(api, web, scanner, jdbc_mssql, jdbc_postgresql, jdbc_h2, bundledPlugin_deps)
  39. cyclonedx
  40. }
  41. jar.enabled = false
  42. shadowJar {
  43. archiveBaseName = 'sonar-application'
  44. archiveClassifier = null
  45. mergeServiceFiles()
  46. zip64 true
  47. manifest {
  48. attributes('Main-Class': 'org.sonar.application.App')
  49. }
  50. }
  51. dependencies {
  52. // please keep list ordered
  53. api 'org.slf4j:slf4j-api'
  54. api 'org.elasticsearch.client:elasticsearch-rest-high-level-client'
  55. api 'org.sonarsource.api.plugin:sonar-plugin-api'
  56. api project(':server:sonar-ce')
  57. api project(':server:sonar-main')
  58. api project(':server:sonar-process')
  59. api project(':server:sonar-webserver')
  60. api project(':sonar-core')
  61. api project(':sonar-plugin-api-impl')
  62. compileOnlyApi 'com.google.code.findbugs:jsr305'
  63. scanner project(path: ':sonar-scanner-engine-shaded', configuration: 'shadow')
  64. cyclonedx project(path: ':sonar-scanner-engine-shaded')
  65. web project(':server:sonar-web')
  66. shutdowner project(':sonar-shutdowner')
  67. jdbc_h2 'com.h2database:h2'
  68. jdbc_mssql 'com.microsoft.sqlserver:mssql-jdbc'
  69. jdbc_postgresql 'org.postgresql:postgresql'
  70. def artifactoryUsername = System.env.'ARTIFACTORY_PRIVATE_USERNAME' ?: (project.hasProperty('artifactoryUsername') ? project.getProperty('artifactoryUsername') : '')
  71. def artifactoryPassword = System.env.'ARTIFACTORY_PRIVATE_PASSWORD' ?: (project.hasProperty('artifactoryPassword') ? project.getProperty('artifactoryPassword') : '')
  72. if (artifactoryUsername && artifactoryPassword) {
  73. zipDist "sonarqube:elasticsearch:${elasticSearchServerVersion}-no-jdk-linux-x86_64@tar.gz"
  74. } else {
  75. zipDist "elasticsearch:elasticsearch:${elasticSearchServerVersion}-linux-x86_64@tar.gz"
  76. }
  77. }
  78. // declare dependencies in configuration bundledPlugin to be packaged in lib/extensions
  79. apply from: 'bundled_plugins.gradle'
  80. //verify if sonar.properties files does not have any external input
  81. task verifySonarProperties(type: Verify) {
  82. def propertiesFile = file('src/main/assembly/conf/sonar.properties')
  83. propertiesFile.withReader { reader ->
  84. def line
  85. while ((line = reader.readLine()) != null) {
  86. if (!line.startsWith('#') && !line.isEmpty()) {
  87. throw new GradleException('sonar.properties file by default must not provide any user configuration.')
  88. }
  89. }
  90. }
  91. }
  92. downloadLicenses {
  93. dependencyConfiguration = 'appLicenses'
  94. }
  95. tasks.register('downloadJres') {
  96. doLast {
  97. def jresMetadata = new JsonSlurper().parse(file(layout.projectDirectory.dir('src/main/resources/jres-metadata.json').asFile))
  98. jresMetadata.each { jre ->
  99. downloadJreFromAdoptium(jre.os, jre.arch, jre.filename, jre.sha256)
  100. }
  101. }
  102. }
  103. task zip(type: Zip, dependsOn: [configurations.compileClasspath]) {
  104. duplicatesStrategy DuplicatesStrategy.EXCLUDE
  105. def archiveDir = "sonarqube-$project.version"
  106. dependsOn tasks.downloadJres
  107. into("${archiveDir}/jres") {
  108. from(layout.buildDirectory.dir('jres'))
  109. }
  110. if(release) {
  111. dependsOn tasks.downloadLicenses
  112. into("${archiveDir}/") {
  113. from(tasks.downloadLicenses.outputs) {
  114. include 'dependency-license.json'
  115. filter(LicenseReader)
  116. }
  117. }
  118. }
  119. into("${archiveDir}/") {
  120. from(file('src/main/assembly')) {
  121. exclude 'conf/sonar.properties'
  122. exclude 'bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml'
  123. exclude 'bin/windows-x86-64/StartSonar.bat'
  124. exclude 'bin/linux-x86-64/sonar.sh'
  125. exclude 'bin/macosx-universal-64/sonar.sh'
  126. }
  127. }
  128. ResolvedArtifact elasticSearchArtifact = configurations.zipDist.resolvedConfiguration.resolvedArtifacts.find {
  129. it.moduleVersion.id.name == "elasticsearch"
  130. }
  131. from(tarTree(elasticSearchArtifact.file)) {
  132. eachFile { fcd ->
  133. def path = fcd.relativePath.segments - fcd.relativeSourcePath.segments + fcd.relativeSourcePath.segments.drop(1)
  134. fcd.relativePath = new RelativePath(true, *path)
  135. }
  136. into("${archiveDir}/elasticsearch")
  137. exclude '**/bin/elasticsearch-certgen'
  138. exclude '**/bin/elasticsearch-certutil'
  139. exclude '**/bin/elasticsearch-create-enrollment-token'
  140. exclude '**/bin/elasticsearch-croneval'
  141. exclude '**/bin/elasticsearch-env-from-file'
  142. exclude '**/bin/elasticsearch-geoip'
  143. exclude '**/bin/elasticsearch-keystore'
  144. exclude '**/bin/elasticsearch-node'
  145. exclude '**/bin/elasticsearch-plugin'
  146. exclude '**/bin/elasticsearch-reconfigure-node'
  147. exclude '**/bin/elasticsearch-reset-password'
  148. exclude '**/bin/elasticsearch-saml-metadata'
  149. exclude '**/bin/elasticsearch-service-tokens'
  150. exclude '**/bin/elasticsearch-setup-passwords'
  151. exclude '**/bin/elasticsearch-shard'
  152. exclude '**/bin/elasticsearch-sql-cli'
  153. exclude '**/bin/elasticsearch-sql-cli-8.6.1.jar'
  154. exclude '**/bin/elasticsearch-syskeygen'
  155. exclude '**/bin/elasticsearch-users'
  156. exclude '**/jdk/**'
  157. exclude '**/lib/tools/ansi-console'
  158. exclude '**/lib/tools/geoip-cli'
  159. exclude '**/lib/tools/plugin-cli'
  160. exclude '**/modules/aggs-matrix-stats/**'
  161. exclude '**/modules/blob-cache/**'
  162. exclude '**/modules/constant-keyword/**'
  163. exclude '**/modules/data-streams/**'
  164. exclude '**/modules/frozen-indices/**'
  165. exclude '**/modules/inference/**'
  166. exclude '**/modules/ingest-attachment/**'
  167. exclude '**/modules/ingest-common/**'
  168. exclude '**/modules/ingest-geoip/**'
  169. exclude '**/modules/ingest-user-agent/**'
  170. exclude '**/modules/kibana/**'
  171. exclude '**/modules/lang-expression/**'
  172. exclude '**/modules/lang-mustache/**'
  173. exclude '**/modules/legacy-geo/**'
  174. exclude '**/modules/mapper-extras/**'
  175. exclude '**/modules/mapper-version/**'
  176. exclude '**/modules/ml-package-loader/**'
  177. exclude '**/modules/percolator/**'
  178. exclude '**/modules/rank-eval/**'
  179. exclude '**/modules/rank-rrf/**'
  180. exclude '**/modules/repositories-metering-api/**'
  181. exclude '**/modules/repository-azure/**'
  182. exclude '**/modules/repository-encrypted/**'
  183. exclude '**/modules/repository-gcs/**'
  184. exclude '**/modules/repository-s3/**'
  185. exclude '**/modules/repository-url/**'
  186. exclude '**/modules/runtime-fields-common/**'
  187. exclude '**/modules/searchable-snapshots/**'
  188. exclude '**/modules/search-business-rules/**'
  189. exclude '**/modules/snapshot-based-recoveries/**'
  190. exclude '**/modules/snapshot-repo-test-kit/**'
  191. exclude '**/modules/spatial/**'
  192. exclude '**/modules/transform/**'
  193. exclude '**/modules/unsigned-long/**'
  194. exclude '**/modules/vectors/**'
  195. exclude '**/modules/vector-tile/**'
  196. exclude '**/modules/wildcard/**'
  197. exclude '**/modules/x-pack-aggregate-metric'
  198. exclude '**/modules/x-pack-aggregate-metric/**'
  199. exclude '**/modules/x-pack-analytics/**'
  200. exclude '**/modules/x-pack-async-search/**'
  201. exclude '**/modules/x-pack-async/**'
  202. exclude '**/modules/x-pack-autoscaling/**'
  203. exclude '**/modules/x-pack-ccr/**'
  204. exclude '**/modules/x-pack-deprecation/**'
  205. exclude '**/modules/x-pack-downsample/**'
  206. exclude '**/modules/x-pack-enrich/**'
  207. exclude '**/modules/x-pack-ent-search/**'
  208. exclude '**/modules/x-pack-eql/**'
  209. exclude '**/modules/x-pack-esql/**'
  210. exclude '**/modules/x-pack-fleet/**'
  211. exclude '**/modules/x-pack-graph/**'
  212. exclude '**/modules/x-pack-identity-provider/**'
  213. exclude '**/modules/x-pack-ilm/**'
  214. exclude '**/modules/x-pack-logstash/**'
  215. exclude '**/modules/x-pack-ml/**'
  216. exclude '**/modules/x-pack-monitoring/**'
  217. exclude '**/modules/x-pack-profiling/**'
  218. exclude '**/modules/x-pack-ql/**'
  219. exclude '**/modules/x-pack-rollup/**'
  220. exclude '**/modules/x-pack-shutdown/**'
  221. exclude '**/modules/x-pack-slm/**'
  222. exclude '**/modules/x-pack-sql/**'
  223. exclude '**/modules/x-pack-stack/**'
  224. exclude '**/modules/x-pack-text-structure/**'
  225. exclude '**/modules/x-pack-voting-only-node/**'
  226. exclude '**/modules/x-pack-watcher/**'
  227. exclude '**/modules/x-pack-write-load-forecaster/**'
  228. includeEmptyDirs = false
  229. }
  230. into("${archiveDir}/conf/") {
  231. from file('src/main/assembly/conf/sonar.properties')
  232. filter(ReplaceTokens, tokens: [
  233. 'searchDefaultHeapSize': '512MB',
  234. 'searchJavaOpts' : '-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError',
  235. 'ceDefaultHeapSize' : '512MB',
  236. 'ceJavaOpts' : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError',
  237. 'webDefaultHeapSize' : '512MB',
  238. 'webJavaOpts' : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError'
  239. ])
  240. }
  241. into("${archiveDir}/bin/linux-x86-64/") {
  242. from file('src/main/assembly/bin/linux-x86-64/sonar.sh')
  243. filter(ReplaceTokens, tokens: [
  244. 'sqversion': version
  245. ])
  246. }
  247. into("${archiveDir}/bin/macosx-universal-64/") {
  248. from file('src/main/assembly/bin/macosx-universal-64/sonar.sh')
  249. filter(ReplaceTokens, tokens: [
  250. 'sqversion': version
  251. ])
  252. }
  253. into("${archiveDir}/bin/windows-x86-64/") {
  254. from file('src/main/assembly/bin/windows-x86-64/StartSonar.bat')
  255. filter(ReplaceTokens, tokens: [
  256. 'sqversion': version
  257. ])
  258. }
  259. into("${archiveDir}/bin/windows-x86-64/lib/") {
  260. from file('src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml')
  261. filter(ReplaceTokens, tokens: [
  262. 'sqversion': version
  263. ])
  264. }
  265. // Create the empty dir (plugins) required by elasticsearch
  266. into("${archiveDir}/elasticsearch/") {
  267. // Create the empty dir required by elasticsearch
  268. from {
  269. new File(buildDir, 'elasticsearch/plugins').mkdirs()
  270. "$buildDir/elasticsearch"
  271. }
  272. }
  273. into("${archiveDir}/lib/extensions/") {
  274. from configurations.bundledPlugin
  275. }
  276. into("${archiveDir}/lib/scanner/") {
  277. from configurations.scanner
  278. }
  279. into("${archiveDir}/lib/") {
  280. from shadowJar
  281. }
  282. into("${archiveDir}/web/") {
  283. duplicatesStrategy DuplicatesStrategy.FAIL
  284. // FIXME use configurations.web with correct artifacts
  285. from(tasks.getByPath(':server:sonar-web:yarn_run').outputs) { a ->
  286. if (official) {
  287. project(':private:branding').fileTree('src').visit { b ->
  288. if (!b.isDirectory()) {
  289. a.exclude b.relativePath.toString()
  290. }
  291. }
  292. }
  293. }
  294. if (official) {
  295. from project(':private:branding').file('src')
  296. }
  297. }
  298. into("${archiveDir}/lib/jdbc/mssql/") {
  299. from configurations.jdbc_mssql
  300. }
  301. into("${archiveDir}/lib/jdbc/postgresql/") {
  302. from configurations.jdbc_postgresql
  303. }
  304. into("${archiveDir}/lib/jdbc/h2/") {
  305. from configurations.jdbc_h2
  306. }
  307. into("${archiveDir}/lib/") {
  308. from configurations.shutdowner
  309. }
  310. }
  311. // Check the size of the archive
  312. zip.doLast {
  313. def minLength = 340000000
  314. def maxLength = 732000000
  315. def length = archiveFile.get().asFile.length()
  316. if (length < minLength)
  317. throw new GradleException("${archiveFileName.get()} size ($length) too small. Min is $minLength")
  318. if (length > maxLength)
  319. throw new GradleException("${destinationDirectory.get()}/${archiveFileName.get()} size ($length) too large. Max is $maxLength")
  320. }
  321. assemble.dependsOn zip
  322. // the script start.sh unpacks OSS distribution into $buildDir/distributions/sonarqube-oss.
  323. // This directory should be deleted when the zip is changed.
  324. task cleanLocalUnzippedDir(dependsOn: zip) {
  325. def unzippedDir = file("$buildDir/distributions/sonarqube-$version")
  326. inputs.files(file("$buildDir/distributions/sonar-application-${version}.zip"))
  327. outputs.upToDateWhen { true }
  328. doLast {
  329. println("delete directory ${unzippedDir}")
  330. project.delete(unzippedDir)
  331. }
  332. }
  333. assemble.dependsOn cleanLocalUnzippedDir
  334. artifacts { zip zip }
  335. artifactoryPublish.skip = false
  336. def bomFile = layout.buildDirectory.file('reports/bom.json')
  337. cyclonedxBom {
  338. includeConfigs = ["runtimeClasspath", "web", "shutdowner", "jdbc_mssql", "jdbc_postgresql", "jdbc_h2", "bundledPlugin_deps",
  339. "cyclonedx"]
  340. outputs.file bomFile
  341. }
  342. tasks.cyclonedxBom {
  343. inputs.files(configurations.runtimeClasspath, configurations.shutdowner, configurations.jdbc_mssql,
  344. configurations.jdbc_postgresql, configurations.jdbc_h2, configurations.bundledPlugin_deps, configurations.cyclonedx)
  345. }
  346. def bomArtifact = artifacts.add('archives', bomFile.get().asFile) {
  347. type 'json'
  348. classifier 'cyclonedx'
  349. builtBy 'cyclonedxBom'
  350. }
  351. publishing {
  352. publications {
  353. mavenJava(MavenPublication) {
  354. artifact zip
  355. }
  356. if (enableBom) {
  357. mavenJava(MavenPublication) {
  358. artifact bomArtifact
  359. }
  360. }
  361. }
  362. }
  363. String.metaClass.urlEncode = { -> URLEncoder.encode(delegate as String, 'UTF-8') }
  364. ext {
  365. WINDOWS = 'windows'
  366. MAC = 'mac'
  367. ALPINE_ADOPTIUM = 'alpine-linux'
  368. ALPINE = 'alpine'
  369. }
  370. /**
  371. * Run this task only when you want to update embedded JREs metadata. Please double check the generated file jres-metadata.json before committing.
  372. */
  373. tasks.register('updateJresMetadata') {
  374. doLast {
  375. // download JSON file from Adoptium API
  376. URL apiUrl = new URL("https://api.adoptium.net/v3/assets/release_name/eclipse/${jre_release_name.urlEncode()}?heap_size=normal&image_type=jre&project=jdk")
  377. def response = new JsonSlurper().parse(apiUrl)
  378. def metadataContent = []
  379. def supportedOsAndArch = [[os:'linux', arch:'x64'], [os:'linux', arch:'aarch64'], [os:ext.ALPINE_ADOPTIUM, arch:'x64'], [os: ext.WINDOWS, arch:'x64'], [os:ext.MAC, arch:'x64'], [os:'mac', arch:'aarch64']]
  380. for (jreFlavor in supportedOsAndArch) {
  381. var candidates = response.binaries.findAll {
  382. it.os == jreFlavor.os && it.architecture == jreFlavor.arch
  383. }
  384. assert candidates.size() == 1, "Expected one JRE package for ${jreFlavor.os} ${jreFlavor.arch} but got ${candidates.size()}"
  385. var jre = candidates[0]
  386. metadataContent << [
  387. 'id': UUID.randomUUID(),
  388. 'filename': jre.package.name,
  389. 'sha256': jre.package.checksum,
  390. 'javaPath': "${jre_release_name}-jre" + (jreFlavor.os == ext.MAC ? '/Contents/Home' : '') + '/bin/java' + (jreFlavor.os == ext.WINDOWS ? '.exe' : ''),
  391. 'os': jreFlavor.os.replace(ext.ALPINE_ADOPTIUM, ext.ALPINE),
  392. 'arch': jreFlavor.arch
  393. ]
  394. }
  395. def jresMetadata = file(layout.projectDirectory.dir('src/main/resources/jres-metadata.json').asFile)
  396. jresMetadata.text = new JsonBuilder(metadataContent).toPrettyString()
  397. }
  398. }
  399. def downloadJreFromAdoptium(os, arch, filename, sha256) {
  400. def jreFile = layout.buildDirectory.file("jres/${filename}")
  401. download.run {
  402. src "https://api.adoptium.net/v3/binary/version/${jre_release_name.urlEncode()}/${os.replace(ext.ALPINE, ext.ALPINE_ADOPTIUM).urlEncode()}/${arch.urlEncode()}/jre/hotspot/normal/eclipse?project=jdk"
  403. dest jreFile
  404. overwrite false
  405. }
  406. verifyChecksum.run {
  407. src jreFile
  408. algorithm 'SHA-256'
  409. checksum sha256
  410. }
  411. }