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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import groovy.json.JsonOutput
  2. import groovy.json.JsonSlurper
  3. import org.apache.tools.ant.filters.ReplaceTokens
  4. plugins {
  5. id "com.github.hierynomus.license-report"
  6. id "com.github.johnrengelman.shadow"
  7. id "de.undercouch.download"
  8. id "org.cyclonedx.bom"
  9. }
  10. sonar {
  11. properties {
  12. property 'sonar.projectName', "${projectTitle} :: Application"
  13. }
  14. }
  15. configurations {
  16. zip
  17. scanner
  18. web
  19. shutdowner
  20. jdbc_mssql {
  21. transitive = false
  22. }
  23. jdbc_postgresql {
  24. transitive = false
  25. }
  26. jdbc_h2 {
  27. transitive = false
  28. }
  29. bundledPlugin {
  30. transitive = false
  31. }
  32. bundledPlugin_deps {
  33. extendsFrom bundledPlugin
  34. transitive = true
  35. }
  36. appLicenses.extendsFrom(api, web, scanner, jdbc_mssql, jdbc_postgresql, jdbc_h2, bundledPlugin_deps)
  37. cyclonedx
  38. }
  39. jar.enabled = false
  40. shadowJar {
  41. archiveBaseName = 'sonar-application'
  42. archiveClassifier = null
  43. mergeServiceFiles()
  44. manifest {
  45. attributes('Main-Class': 'org.sonar.application.App')
  46. }
  47. }
  48. dependencies {
  49. // please keep list ordered
  50. api 'org.slf4j:slf4j-api'
  51. api 'org.elasticsearch.client:elasticsearch-rest-high-level-client'
  52. api 'org.sonarsource.api.plugin:sonar-plugin-api'
  53. api project(':server:sonar-ce')
  54. api project(':server:sonar-main')
  55. api project(':server:sonar-process')
  56. api project(':server:sonar-webserver')
  57. api project(':sonar-core')
  58. api project(':sonar-plugin-api-impl')
  59. compileOnlyApi 'com.google.code.findbugs:jsr305'
  60. scanner project(path: ':sonar-scanner-engine-shaded', configuration: 'shadow')
  61. cyclonedx project(path: ':sonar-scanner-engine-shaded')
  62. web project(':server:sonar-web')
  63. shutdowner project(':sonar-shutdowner')
  64. jdbc_h2 'com.h2database:h2'
  65. jdbc_mssql 'com.microsoft.sqlserver:mssql-jdbc'
  66. jdbc_postgresql 'org.postgresql:postgresql'
  67. }
  68. // declare dependencies in configuration bundledPlugin to be packaged in lib/extensions
  69. apply from: 'bundled_plugins.gradle'
  70. //verify if sonar.properties files does not have any external input
  71. task verifySonarProperties(type: Verify) {
  72. def propertiesFile = file('src/main/assembly/conf/sonar.properties')
  73. propertiesFile.withReader { reader ->
  74. def line
  75. while ((line = reader.readLine()) != null) {
  76. if (!line.startsWith('#') && !line.isEmpty()) {
  77. throw new GradleException('sonar.properties file by default must not provide any user configuration.')
  78. }
  79. }
  80. }
  81. }
  82. task verifyElasticSearchDownload(type: Verify) {
  83. src new File(buildDir, "$elasticsearchDownloadUrlFile")
  84. algorithm 'SHA-512'
  85. checksum elasticsearchDownloadSha512
  86. }
  87. task downloadElasticSearch(type: Download) {
  88. def artifactoryUsername = System.env.'ARTIFACTORY_PRIVATE_USERNAME' ?: (project.hasProperty('artifactoryUsername') ? project.getProperty('artifactoryUsername') : '')
  89. def artifactoryPassword = System.env.'ARTIFACTORY_PRIVATE_PASSWORD' ?: (project.hasProperty('artifactoryPassword') ? project.getProperty('artifactoryPassword') : '')
  90. if (artifactoryUsername && artifactoryPassword) {
  91. src "$elasticsearchDownloadRepoxUrlPath$elasticsearchDownloadUrlFile"
  92. username artifactoryUsername
  93. password artifactoryPassword
  94. } else {
  95. src "$elasticsearchDownloadUrlPath$elasticsearchDownloadUrlFile"
  96. }
  97. dest "$buildDir/$elasticsearchDownloadUrlFile"
  98. onlyIfModified true
  99. finalizedBy verifyElasticSearchDownload
  100. }
  101. downloadLicenses {
  102. dependencyConfiguration = 'appLicenses'
  103. }
  104. task zip(type: Zip, dependsOn: [configurations.compileClasspath, downloadElasticSearch, verifyElasticSearchDownload]) {
  105. duplicatesStrategy DuplicatesStrategy.EXCLUDE
  106. def archiveDir = "sonarqube-$project.version"
  107. if(release) {
  108. dependsOn tasks.downloadLicenses
  109. into("${archiveDir}/") {
  110. from(tasks.downloadLicenses.outputs) {
  111. include 'dependency-license.json'
  112. eachFile { jsonFile ->
  113. def json = new JsonSlurper().parse(jsonFile.file)
  114. json.dependencies.each { dependency ->
  115. if (dependency.licenses.size() > 1) {
  116. def idx = dependency.licenses.findIndexOf { it.name == "Elastic License 2.0" }
  117. if (idx >= 0) {
  118. dependency.licenses = [dependency.licenses[idx]]
  119. }
  120. }
  121. }
  122. json.dependencies.sort { it.name }
  123. def jsonText = JsonOutput.toJson(json)
  124. jsonFile.file.text = JsonOutput.prettyPrint(jsonText)
  125. }
  126. }
  127. }
  128. }
  129. into("${archiveDir}/") {
  130. from(file('src/main/assembly')) {
  131. exclude 'conf/sonar.properties'
  132. exclude 'bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml'
  133. exclude 'bin/windows-x86-64/StartSonar.bat'
  134. exclude 'elasticsearch-patch'
  135. exclude 'bin/linux-x86-64/sonar.sh'
  136. exclude 'bin/macosx-universal-64/sonar.sh'
  137. }
  138. }
  139. from(tarTree(downloadElasticSearch.dest)) {
  140. eachFile { fcd ->
  141. def path = fcd.relativePath.segments - fcd.relativeSourcePath.segments + fcd.relativeSourcePath.segments.drop(1)
  142. fcd.relativePath = new RelativePath(true, *path)
  143. }
  144. into("${archiveDir}/elasticsearch")
  145. filesMatching('**/bin/elasticsearch-env') {
  146. // to avoid warning logs
  147. filter { line -> line.replaceAll('echo "warning: no-jdk distributions.*', ':') }
  148. }
  149. // elasticsearch script will be replaced by patched version below
  150. exclude '**/bin/elasticsearch'
  151. exclude '**/bin/elasticsearch-certgen'
  152. exclude '**/bin/elasticsearch-certutil'
  153. exclude '**/bin/elasticsearch-cli'
  154. exclude '**/bin/elasticsearch-croneval'
  155. exclude '**/bin/elasticsearch-geoip'
  156. exclude '**/bin/elasticsearch-keystore'
  157. exclude '**/bin/elasticsearch-migrate'
  158. exclude '**/bin/elasticsearch-node'
  159. exclude '**/bin/elasticsearch-plugin'
  160. exclude '**/bin/elasticsearch-saml-metadata'
  161. exclude '**/bin/elasticsearch-service-tokens'
  162. exclude '**/bin/elasticsearch-setup-passwords'
  163. exclude '**/bin/elasticsearch-shard'
  164. exclude '**/bin/elasticsearch-sql-cli*'
  165. exclude '**/bin/elasticsearch-syskeygen'
  166. exclude '**/bin/elasticsearch-users'
  167. exclude '**/bin/x-pack-env'
  168. exclude '**/bin/x-pack-security-env'
  169. exclude '**/bin/x-pack-watcher-env'
  170. exclude '**/jdk/**'
  171. exclude '**/lib/tools/**'
  172. exclude '**/modules/aggs-matrix-stats/**'
  173. exclude '**/modules/constant-keyword/**'
  174. exclude '**/modules/frozen-indices/**'
  175. exclude '**/modules/ingest-common/**'
  176. exclude '**/modules/ingest-geoip/**'
  177. exclude '**/modules/ingest-user-agent/**'
  178. exclude '**/modules/kibana/**'
  179. exclude '**/modules/lang-expression/**'
  180. exclude '**/modules/lang-mustache/**'
  181. exclude '**/modules/legacy-geo/**'
  182. exclude '**/modules/mapper-extras/**'
  183. exclude '**/modules/mapper-version/**'
  184. exclude '**/modules/percolator/**'
  185. exclude '**/modules/rank-eval/**'
  186. exclude '**/modules/repositories-metering-api/**'
  187. exclude '**/modules/repository-encrypted/**'
  188. exclude '**/modules/repository-url/**'
  189. exclude '**/modules/runtime-fields-common/**'
  190. exclude '**/modules/search-business-rules/**'
  191. exclude '**/modules/searchable-snapshots/**'
  192. exclude '**/modules/snapshot-repo-test-kit/**'
  193. exclude '**/modules/spatial/**'
  194. exclude '**/modules/transform/**'
  195. exclude '**/modules/unsigned-long/**'
  196. exclude '**/modules/vector-tile/**'
  197. exclude '**/modules/vectors/**'
  198. exclude '**/modules/wildcard/**'
  199. exclude '**/modules/x-pack-*/**'
  200. includeEmptyDirs = false
  201. }
  202. into("${archiveDir}/conf/") {
  203. from file('src/main/assembly/conf/sonar.properties')
  204. filter(ReplaceTokens, tokens: [
  205. 'searchDefaultHeapSize': '512MB',
  206. 'searchJavaOpts' : '-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError',
  207. 'ceDefaultHeapSize' : '512MB',
  208. 'ceJavaOpts' : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError',
  209. 'webDefaultHeapSize' : '512MB',
  210. 'webJavaOpts' : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError'
  211. ])
  212. }
  213. into("${archiveDir}/bin/linux-x86-64/") {
  214. from file('src/main/assembly/bin/linux-x86-64/sonar.sh')
  215. filter(ReplaceTokens, tokens: [
  216. 'sqversion': version
  217. ])
  218. }
  219. into("${archiveDir}/bin/macosx-universal-64/") {
  220. from file('src/main/assembly/bin/macosx-universal-64/sonar.sh')
  221. filter(ReplaceTokens, tokens: [
  222. 'sqversion': version
  223. ])
  224. }
  225. into("${archiveDir}/bin/windows-x86-64/") {
  226. from file('src/main/assembly/bin/windows-x86-64/StartSonar.bat')
  227. filter(ReplaceTokens, tokens: [
  228. 'sqversion': version
  229. ])
  230. }
  231. into("${archiveDir}/bin/windows-x86-64/lib/") {
  232. from file('src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml')
  233. filter(ReplaceTokens, tokens: [
  234. 'sqversion': version
  235. ])
  236. }
  237. into("${archiveDir}/elasticsearch/") {
  238. from file('src/main/assembly/elasticsearch-patch')
  239. include 'bin/elasticsearch'
  240. }
  241. // Create the empty dir (plugins) required by elasticsearch
  242. into("${archiveDir}/elasticsearch/") {
  243. from "$buildDir/elasticsearch"
  244. }
  245. into("${archiveDir}/lib/extensions/") {
  246. from configurations.bundledPlugin
  247. }
  248. into("${archiveDir}/lib/scanner/") {
  249. from configurations.scanner
  250. }
  251. into("${archiveDir}/lib/") {
  252. from shadowJar
  253. }
  254. into("${archiveDir}/web/") {
  255. duplicatesStrategy DuplicatesStrategy.FAIL
  256. // FIXME use configurations.web with correct artifacts
  257. from(tasks.getByPath(':server:sonar-web:yarn_run').outputs) { a ->
  258. if (official) {
  259. project(':private:branding').fileTree('src').visit { b ->
  260. if (!b.isDirectory()) {
  261. a.exclude b.relativePath.toString()
  262. }
  263. }
  264. }
  265. }
  266. if (official) {
  267. from project(':private:branding').file('src')
  268. }
  269. }
  270. into("${archiveDir}/lib/jdbc/mssql/") {
  271. from configurations.jdbc_mssql
  272. }
  273. into("${archiveDir}/lib/jdbc/postgresql/") {
  274. from configurations.jdbc_postgresql
  275. }
  276. into("${archiveDir}/lib/jdbc/h2/") {
  277. from configurations.jdbc_h2
  278. }
  279. into("${archiveDir}/lib/") {
  280. from configurations.shutdowner
  281. }
  282. }
  283. // Create the empty dir required by elasticsearch
  284. zip.doFirst {
  285. new File(buildDir, 'elasticsearch/plugins').mkdirs()
  286. }
  287. // Check the size of the archive
  288. zip.doLast {
  289. def minLength = 272000000
  290. def maxLength = 297000000
  291. def length = archiveFile.get().asFile.length()
  292. if (length < minLength)
  293. throw new GradleException("${archiveFileName.get()} size ($length) too small. Min is $minLength")
  294. if (length > maxLength)
  295. throw new GradleException("${destinationDirectory.get()}/${archiveFileName.get()} size ($length) too large. Max is $maxLength")
  296. }
  297. assemble.dependsOn zip
  298. // the script start.sh unpacks OSS distribution into $buildDir/distributions/sonarqube-oss.
  299. // This directory should be deleted when the zip is changed.
  300. task cleanLocalUnzippedDir(dependsOn: zip) {
  301. def unzippedDir = file("$buildDir/distributions/sonarqube-$version")
  302. inputs.files(file("$buildDir/distributions/sonar-application-${version}.zip"))
  303. outputs.upToDateWhen { true }
  304. doLast {
  305. println("delete directory ${unzippedDir}")
  306. project.delete(unzippedDir)
  307. }
  308. }
  309. assemble.dependsOn cleanLocalUnzippedDir
  310. artifacts { zip zip }
  311. artifactoryPublish.skip = false
  312. def bomFile = layout.buildDirectory.file('reports/bom.json')
  313. cyclonedxBom {
  314. includeConfigs += ["runtimeClasspath", "web", "shutdowner", "jdbc_mssql", "jdbc_postgresql", "jdbc_h2", "bundledPlugin_deps",
  315. "cyclonedx"]
  316. outputs.file bomFile
  317. }
  318. tasks.cyclonedxBom {
  319. inputs.files(configurations.runtimeClasspath, configurations.shutdowner, configurations.jdbc_mssql,
  320. configurations.jdbc_postgresql, configurations.jdbc_h2, configurations.bundledPlugin_deps, configurations.cyclonedx)
  321. }
  322. def bomArtifact = artifacts.add('archives', bomFile.get().asFile) {
  323. type 'json'
  324. classifier 'cyclonedx'
  325. builtBy 'cyclonedxBom'
  326. }
  327. publishing {
  328. publications {
  329. mavenJava(MavenPublication) {
  330. artifact zip
  331. }
  332. if (enableBom) {
  333. mavenJava(MavenPublication) {
  334. artifact bomArtifact
  335. }
  336. }
  337. }
  338. }