]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22167 Add Gradle tasks to bundle JREs
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 2 May 2024 12:57:00 +0000 (14:57 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 3 May 2024 20:02:50 +0000 (20:02 +0000)
gradle.properties
sonar-application/build.gradle
sonar-application/src/main/resources/jres-metadata.json [new file with mode: 0644]

index 0907094bbf9532e918ac28d2176ede641deffa2d..39c2bd3bf9fcf805415579d0cd534d7c251a11e1 100644 (file)
@@ -16,3 +16,4 @@ org.gradle.vfs.watch=true
 elasticsearchVersion=8.11.0
 projectType=application
 artifactoryUrl=https://repox.jfrog.io/repox
+jre_release_name=jdk-17.0.11+9
index 7e23c4fe0b912eb36af93e2305e0fd90e2ad6f2a..53c0ec9590f60f3023cf6202bbe4b51b3c8e7d16 100644 (file)
@@ -1,3 +1,5 @@
+import groovy.json.JsonBuilder
+import groovy.json.JsonSlurper
 import org.apache.tools.ant.filters.ReplaceTokens
 import org.sonar.build.LicenseReader
 
@@ -106,10 +108,24 @@ downloadLicenses {
   dependencyConfiguration = 'appLicenses'
 }
 
+tasks.register('downloadJres') {
+  doLast {
+    def jresMetadata = new JsonSlurper().parse(file(layout.projectDirectory.dir('src/main/resources/jres-metadata.json').asFile))
+    jresMetadata.each { jre ->
+      downloadJreFromAdoptium(jre.os, jre.arch, jre.filename, jre.sha256)
+    }
+  }
+}
+
 task zip(type: Zip, dependsOn: [configurations.compileClasspath]) {
   duplicatesStrategy DuplicatesStrategy.EXCLUDE
   def archiveDir = "sonarqube-$project.version"
 
+  dependsOn tasks.downloadJres
+  into("${archiveDir}/jres") {
+    from(layout.buildDirectory.dir('jres'))
+  }
+
   if(release) {
     dependsOn tasks.downloadLicenses
     into("${archiveDir}/") {
@@ -322,7 +338,7 @@ task zip(type: Zip, dependsOn: [configurations.compileClasspath]) {
 // Check the size of the archive
 zip.doLast {
   def minLength = 340000000
-  def maxLength = 476000000
+  def maxLength = 727000000
 
   def length = archiveFile.get().asFile.length()
   if (length < minLength)
@@ -378,3 +394,59 @@ publishing {
     }
   }
 }
+
+String.metaClass.urlEncode = { -> URLEncoder.encode(delegate as String, 'UTF-8') }
+ext {
+  WINDOWS = 'windows'
+  MAC = 'mac'
+  ALPINE_ADOPTIUM = 'alpine-linux'
+  ALPINE = 'alpine'
+}
+
+/**
+ * Run this task only when you want to update embedded JREs metadata. Please double check the generated file jres-metadata.json before committing.
+ */
+tasks.register('updateJresMetadata') {
+  doLast {
+    // download JSON file from Adoptium API
+    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")
+    def response = new JsonSlurper().parse(apiUrl)
+
+    def metadataContent = []
+
+    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']]
+
+    for (jreFlavor in supportedOsAndArch) {
+      var candidates = response.binaries.findAll {
+        it.os == jreFlavor.os && it.architecture == jreFlavor.arch
+      }
+      assert candidates.size() == 1, "Expected one JRE package for ${jreFlavor.os} ${jreFlavor.arch} but got ${candidates.size()}"
+      var jre = candidates[0]
+      metadataContent <<  [
+          'id': UUID.randomUUID(),
+          'filename': jre.package.name,
+          'sha256': jre.package.checksum,
+          'javaPath': "${jre_release_name}-jre" + (jreFlavor.os == ext.MAC ? '/Contents/Home' : '') + '/bin/java' + (jreFlavor.os == ext.WINDOWS ? '.exe' : ''),
+          'os': jreFlavor.os.replace(ext.ALPINE_ADOPTIUM, ext.ALPINE),
+          'arch': jreFlavor.arch
+        ]
+    }
+
+    def jresMetadata = file(layout.projectDirectory.dir('src/main/resources/jres-metadata.json').asFile)
+    jresMetadata.text = new JsonBuilder(metadataContent).toPrettyString()
+  }
+}
+
+def downloadJreFromAdoptium(os, arch, filename, sha256) {
+  def jreFile = layout.buildDirectory.file("jres/${filename}")
+  download.run {
+    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"
+    dest jreFile
+    overwrite false
+  }
+  verifyChecksum.run {
+    src jreFile
+    algorithm 'SHA-256'
+    checksum sha256
+  }
+}
\ No newline at end of file
diff --git a/sonar-application/src/main/resources/jres-metadata.json b/sonar-application/src/main/resources/jres-metadata.json
new file mode 100644 (file)
index 0000000..911d4f1
--- /dev/null
@@ -0,0 +1,50 @@
+[
+    {
+        "id": "9fe68398-f6a1-4c70-993f-72253d2704b0",
+        "filename": "OpenJDK17U-jre_x64_linux_hotspot_17.0.11_9.tar.gz",
+        "sha256": "bcb1b7b8ad68c93093f09b591b7cb17161d39891f7d29d33a586f5a328603707",
+        "javaPath": "jdk-17.0.11+9-jre/bin/java",
+        "os": "linux",
+        "arch": "x64"
+    },
+    {
+        "id": "a9b05fc0-56eb-4a8e-917a-535cf885e34e",
+        "filename": "OpenJDK17U-jre_aarch64_linux_hotspot_17.0.11_9.tar.gz",
+        "sha256": "ccfa23c25790475c84df983cc5f729b94c04d9ea9863912deb15c6266782cf16",
+        "javaPath": "jdk-17.0.11+9-jre/bin/java",
+        "os": "linux",
+        "arch": "aarch64"
+    },
+    {
+        "id": "9e1c89a4-fa86-453c-8fa4-716c1be6ceae",
+        "filename": "OpenJDK17U-jre_x64_alpine-linux_hotspot_17.0.11_9.tar.gz",
+        "sha256": "b5dffd0be08c464d9c3903e2947508c1a5c21804ea1cff5556991a2a47d617d8",
+        "javaPath": "jdk-17.0.11+9-jre/bin/java",
+        "os": "alpine",
+        "arch": "x64"
+    },
+    {
+        "id": "29a1a704-026c-4c58-9b81-ae9bc0ab2961",
+        "filename": "OpenJDK17U-jre_x64_windows_hotspot_17.0.11_9.zip",
+        "sha256": "4bafe2e94439c8193fc8c68247cb0dbaf4e80265b903288f63f128304f129bbe",
+        "javaPath": "jdk-17.0.11+9-jre/bin/java.exe",
+        "os": "windows",
+        "arch": "x64"
+    },
+    {
+        "id": "8efcd6d9-3552-45a1-84aa-8ccf00057ae6",
+        "filename": "OpenJDK17U-jre_x64_mac_hotspot_17.0.11_9.tar.gz",
+        "sha256": "232c40bebd6ddbb673862e86e7e6e09bcfe399e5a53c8a6b77bf1ceab8edefd0",
+        "javaPath": "jdk-17.0.11+9-jre/Contents/Home/bin/java",
+        "os": "mac",
+        "arch": "x64"
+    },
+    {
+        "id": "54f81f6e-212c-4cb5-a9cf-7c146a593351",
+        "filename": "OpenJDK17U-jre_aarch64_mac_hotspot_17.0.11_9.tar.gz",
+        "sha256": "003d3e0a65a2f0633b8bfed42be133724b490acb323c174c708d3a446d5fc660",
+        "javaPath": "jdk-17.0.11+9-jre/Contents/Home/bin/java",
+        "os": "mac",
+        "arch": "aarch64"
+    }
+]
\ No newline at end of file