aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application/build.gradle
blob: 85ee265ffca8a04c1677383cebef0bb533152141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import org.apache.tools.ant.filters.ReplaceTokens
import org.sonar.build.LicenseReader

plugins {
  id "com.github.hierynomus.license-report"
  id "com.github.johnrengelman.shadow"
  id "de.undercouch.download"
  id "org.cyclonedx.bom"
}

sonar {
  properties {
    property 'sonar.projectName', "${projectTitle} :: Application"
  }
}

configurations {
  zip
  scanner
  web
  shutdowner
  jdbc_mssql {
    transitive = false
  }
  jdbc_postgresql {
    transitive = false
  }
  jdbc_h2 {
    transitive = false
  }
  bundledPlugin {
    transitive = false
  }
  bundledPlugin_deps {
    extendsFrom bundledPlugin
    transitive = true
  }
  appLicenses.extendsFrom(api, web, scanner, jdbc_mssql, jdbc_postgresql, jdbc_h2, bundledPlugin_deps)
  cyclonedx
}

jar.enabled = false
shadowJar {
  archiveBaseName = 'sonar-application'
  archiveClassifier = null
  mergeServiceFiles()
  manifest {
    attributes('Main-Class': 'org.sonar.application.App')
  }
}

dependencies {
    // please keep list ordered
    api 'org.slf4j:slf4j-api'

    api 'org.elasticsearch.client:elasticsearch-rest-high-level-client'
    api 'org.sonarsource.api.plugin:sonar-plugin-api'
    api project(':server:sonar-ce')
    api project(':server:sonar-main')
    api project(':server:sonar-process')
    api project(':server:sonar-webserver')
    api project(':sonar-core')
    api project(':sonar-plugin-api-impl')

    compileOnlyApi 'com.google.code.findbugs:jsr305'

    scanner project(path: ':sonar-scanner-engine-shaded', configuration: 'shadow')
    cyclonedx project(path: ':sonar-scanner-engine-shaded')
    web project(':server:sonar-web')
    shutdowner project(':sonar-shutdowner')

    jdbc_h2 'com.h2database:h2'
    jdbc_mssql 'com.microsoft.sqlserver:mssql-jdbc'
    jdbc_postgresql 'org.postgresql:postgresql'
}

// declare dependencies in configuration bundledPlugin to be packaged in lib/extensions
apply from: 'bundled_plugins.gradle'

//verify if sonar.properties files does not have any external input
task verifySonarProperties(type: Verify) {
  def propertiesFile = file('src/main/assembly/conf/sonar.properties')
  propertiesFile.withReader { reader ->
    def line
    while ((line = reader.readLine()) != null) {
      if (!line.startsWith('#') && !line.isEmpty()) {
        throw new GradleException('sonar.properties file by default must not provide any user configuration.')
      }
    }
  }
}

def artifactoryUsername = System.env.'ARTIFACTORY_PRIVATE_USERNAME' ?: (project.hasProperty('artifactoryUsername') ? project.getProperty('artifactoryUsername') : '')
def artifactoryPassword = System.env.'ARTIFACTORY_PRIVATE_PASSWORD' ?: (project.hasProperty('artifactoryPassword') ? project.getProperty('artifactoryPassword') : '')

task verifyElasticSearchDownload(type: Verify) {
  if (artifactoryUsername && artifactoryPassword) {
    src new File(buildDir, "$elasticsearchDownloadUrlFileNoJdk")
    algorithm 'SHA-512'
    checksum elasticsearchDownloadSha512NoJdk
  } else {
    src new File(buildDir, "$elasticsearchDownloadUrlFileJdk")
    algorithm 'SHA-512'
    checksum elasticsearchDownloadSha512Jdk
  }
}

task downloadElasticSearch(type: Download) {
  if (artifactoryUsername && artifactoryPassword) {
    src "$elasticsearchDownloadRepoxUrlPath$elasticsearchDownloadUrlFileNoJdk"
    username artifactoryUsername
    password artifactoryPassword
    dest "$buildDir/$elasticsearchDownloadUrlFileNoJdk"
  } else {
    src "$elasticsearchDownloadUrlPath$elasticsearchDownloadUrlFileJdk"
    dest "$buildDir/$elasticsearchDownloadUrlFileJdk"
  }
  onlyIfModified true
  finalizedBy verifyElasticSearchDownload
}

downloadLicenses {
  dependencyConfiguration = 'appLicenses'
}

task zip(type: Zip, dependsOn: [configurations.compileClasspath, downloadElasticSearch, verifyElasticSearchDownload]) {
  duplicatesStrategy DuplicatesStrategy.EXCLUDE
  def archiveDir = "sonarqube-$project.version"

  if(release) {
    dependsOn tasks.downloadLicenses
    into("${archiveDir}/") {
      from(tasks.downloadLicenses.outputs) {
        include 'dependency-license.json'
        filter(LicenseReader)
      }
    }
  }

  into("${archiveDir}/") {
    from(file('src/main/assembly')) {
      exclude 'conf/sonar.properties'
      exclude 'bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml'
      exclude 'bin/windows-x86-64/StartSonar.bat'
      exclude 'bin/linux-x86-64/sonar.sh'
      exclude 'bin/macosx-universal-64/sonar.sh'
    }
  }


  from(tarTree(downloadElasticSearch.dest)) {
    eachFile { fcd ->
      def path = fcd.relativePath.segments - fcd.relativeSourcePath.segments + fcd.relativeSourcePath.segments.drop(1)
      fcd.relativePath = new RelativePath(true, *path)
    }
    into("${archiveDir}/elasticsearch")
    exclude '**/bin/elasticsearch-certgen'
    exclude '**/bin/elasticsearch-certutil'
    exclude '**/bin/elasticsearch-create-enrollment-token'
    exclude '**/bin/elasticsearch-croneval'
    exclude '**/bin/elasticsearch-env-from-file'
    exclude '**/bin/elasticsearch-geoip'
    exclude '**/bin/elasticsearch-keystore'
    exclude '**/bin/elasticsearch-node'
    exclude '**/bin/elasticsearch-plugin'
    exclude '**/bin/elasticsearch-reconfigure-node'
    exclude '**/bin/elasticsearch-reset-password'
    exclude '**/bin/elasticsearch-saml-metadata'
    exclude '**/bin/elasticsearch-service-tokens'
    exclude '**/bin/elasticsearch-setup-passwords'
    exclude '**/bin/elasticsearch-shard'
    exclude '**/bin/elasticsearch-sql-cli'
    exclude '**/bin/elasticsearch-sql-cli-8.6.1.jar'
    exclude '**/bin/elasticsearch-syskeygen'
    exclude '**/bin/elasticsearch-users'
    exclude '**/jdk/**'
    exclude '**/lib/tools/plugin-cli'
    exclude '**/lib/tools/geoip-cli'
    exclude '**/lib/tools/ansi-console'
    exclude '**/modules/aggs-matrix-stats/**'
    exclude '**/modules/constant-keyword/**'
    exclude '**/modules/data-streams/**'
    exclude '**/modules/frozen-indices/**'
    exclude '**/modules/ingest-attachment/**'
    exclude '**/modules/ingest-common/**'
    exclude '**/modules/ingest-geoip/**'
    exclude '**/modules/ingest-user-agent/**'
    exclude '**/modules/kibana/**'
    exclude '**/modules/lang-expression/**'
    exclude '**/modules/lang-mustache/**'
    exclude '**/modules/legacy-geo/**'
    exclude '**/modules/mapper-extras/**'
    exclude '**/modules/mapper-version/**'
    exclude '**/modules/percolator/**'
    exclude '**/modules/rank-eval/**'
    exclude '**/modules/repositories-metering-api/**'
    exclude '**/modules/repository-azure/**'
    exclude '**/modules/repository-encrypted/**'
    exclude '**/modules/repository-gcs/**'
    exclude '**/modules/repository-s3/**'
    exclude '**/modules/repository-url/**'
    exclude '**/modules/runtime-fields-common/**'
    exclude '**/modules/search-business-rules/**'
    exclude '**/modules/searchable-snapshots/**'
    exclude '**/modules/snapshot-based-recoveries/**'
    exclude '**/modules/snapshot-repo-test-kit/**'
    exclude '**/modules/spatial/**'
    exclude '**/modules/transform/**'
    exclude '**/modules/unsigned-long/**'
    exclude '**/modules/vector-tile/**'
    exclude '**/modules/vectors/**'
    exclude '**/modules/wildcard/**'
    exclude '**/modules/x-pack-aggregate-metric'
    exclude '**/modules/x-pack-analytics/**'
    exclude '**/modules/x-pack-async/**'
    exclude '**/modules/x-pack-async-search/**'
    exclude '**/modules/x-pack-autoscaling/**'
    exclude '**/modules/x-pack-ccr/**'
    exclude '**/modules/x-pack-deprecation/**'
    exclude '**/modules/x-pack-enrich/**'
    exclude '**/modules/x-pack-eql/**'
    exclude '**/modules/x-pack-fleet/**'
    exclude '**/modules/x-pack-graph/**'
    exclude '**/modules/x-pack-identity-provider/**'
    exclude '**/modules/x-pack-ilm/**'
    exclude '**/modules/x-pack-logstash/**'
    exclude '**/modules/x-pack-monitoring/**'
    exclude '**/modules/x-pack-ql/**'
    exclude '**/modules/x-pack-rollup/**'
    exclude '**/modules/x-pack-ml/**'
    exclude '**/modules/x-pack-shutdown/**'
    exclude '**/modules/x-pack-sql/**'
    exclude '**/modules/x-pack-stack/**'
    exclude '**/modules/x-pack-text-structure/**'
    exclude '**/modules/x-pack-voting-only-node/**'
    exclude '**/modules/x-pack-watcher/**'
    exclude '**/modules/x-pack-write-load-forecaster/**'
    includeEmptyDirs = false
  }

  into("${archiveDir}/conf/") {
    from file('src/main/assembly/conf/sonar.properties')
    filter(ReplaceTokens, tokens: [
            'searchDefaultHeapSize': '512MB',
            'searchJavaOpts'       : '-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError',
            'ceDefaultHeapSize'    : '512MB',
            'ceJavaOpts'           : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError',
            'webDefaultHeapSize'   : '512MB',
            'webJavaOpts'          : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError'
    ])
  }

  into("${archiveDir}/bin/linux-x86-64/") {
    from file('src/main/assembly/bin/linux-x86-64/sonar.sh')
    filter(ReplaceTokens, tokens: [
            'sqversion': version
    ])
  }

  into("${archiveDir}/bin/macosx-universal-64/") {
    from file('src/main/assembly/bin/macosx-universal-64/sonar.sh')
    filter(ReplaceTokens, tokens: [
            'sqversion': version
    ])
  }

  into("${archiveDir}/bin/windows-x86-64/") {
    from file('src/main/assembly/bin/windows-x86-64/StartSonar.bat')
    filter(ReplaceTokens, tokens: [
            'sqversion': version
    ])
  }

  into("${archiveDir}/bin/windows-x86-64/lib/") {
    from file('src/main/assembly/bin/windows-x86-64/lib/SonarServiceWrapperTemplate.xml')
    filter(ReplaceTokens, tokens: [
            'sqversion': version
    ])
  }
  // Create the empty dir (plugins) required by elasticsearch
  into("${archiveDir}/elasticsearch/") {
    // Create the empty dir required by elasticsearch
    from {
      new File(buildDir, 'elasticsearch/plugins').mkdirs()
      "$buildDir/elasticsearch"
    }
  }
  into("${archiveDir}/lib/extensions/") {
    from configurations.bundledPlugin
  }
  into("${archiveDir}/lib/scanner/") {
    from configurations.scanner
  }
  into("${archiveDir}/lib/") {
    from shadowJar
  }
  into("${archiveDir}/web/") {
    duplicatesStrategy DuplicatesStrategy.FAIL
    // FIXME use configurations.web with correct artifacts
    from(tasks.getByPath(':server:sonar-web:yarn_run').outputs) { a ->
      if (official) {
         project(':private:branding').fileTree('src').visit { b ->
           if (!b.isDirectory()) {
             a.exclude b.relativePath.toString()
           }
         }
      }
    }
    if (official) {
      from project(':private:branding').file('src')
    }
  }
  into("${archiveDir}/lib/jdbc/mssql/") {
    from configurations.jdbc_mssql
  }
  into("${archiveDir}/lib/jdbc/postgresql/") {
    from configurations.jdbc_postgresql
  }
  into("${archiveDir}/lib/jdbc/h2/") {
    from configurations.jdbc_h2
  }
  into("${archiveDir}/lib/") {
    from configurations.shutdowner
  }
}

// Check the size of the archive
zip.doLast {
  def minLength = 340000000
  def maxLength = 371000000

  def length = archiveFile.get().asFile.length()
  if (length < minLength)
    throw new GradleException("${archiveFileName.get()} size ($length) too small. Min is $minLength")
  if (length > maxLength)
    throw new GradleException("${destinationDirectory.get()}/${archiveFileName.get()} size ($length) too large. Max is $maxLength")
}
assemble.dependsOn zip

// the script start.sh unpacks OSS distribution into $buildDir/distributions/sonarqube-oss.
// This directory should be deleted when the zip is changed.
task cleanLocalUnzippedDir(dependsOn: zip) {
  def unzippedDir = file("$buildDir/distributions/sonarqube-$version")
  inputs.files(file("$buildDir/distributions/sonar-application-${version}.zip"))
  outputs.upToDateWhen { true }

  doLast {
    println("delete directory ${unzippedDir}")
    project.delete(unzippedDir)
  }
}
assemble.dependsOn cleanLocalUnzippedDir

artifacts { zip zip }

artifactoryPublish.skip = false

def bomFile = layout.buildDirectory.file('reports/bom.json')
cyclonedxBom {
  includeConfigs = ["runtimeClasspath", "web", "shutdowner", "jdbc_mssql", "jdbc_postgresql", "jdbc_h2", "bundledPlugin_deps",
                    "cyclonedx"]
  outputs.file bomFile
}
tasks.cyclonedxBom {
  inputs.files(configurations.runtimeClasspath, configurations.shutdowner, configurations.jdbc_mssql,
          configurations.jdbc_postgresql, configurations.jdbc_h2, configurations.bundledPlugin_deps, configurations.cyclonedx)
}
def bomArtifact = artifacts.add('archives', bomFile.get().asFile) {
  type 'json'
  classifier 'cyclonedx'
  builtBy 'cyclonedxBom'
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifact zip
    }
    if (enableBom) {
      mavenJava(MavenPublication) {
        artifact bomArtifact
      }
    }
  }
}