Skip to content

Commit 564fa8c

Browse files
authored
Streamline configuration of a sonar plugin (#17)
1 parent 148ff23 commit 564fa8c

6 files changed

Lines changed: 96 additions & 3 deletions

File tree

gradle-modules/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
implementation(libs.jfrog.buildinfo.gradle) {
2828
exclude("ch.qos.logback", "logback-core")
2929
}
30+
implementation(libs.shadow)
3031
}
3132

3233
gradlePlugin {

gradle-modules/src/main/kotlin/org.sonarsource.cloud-native.artifactory-configuration.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ plugins {
2222
id("com.jfrog.artifactory")
2323
}
2424

25-
if (project.path != ":") {
26-
throw GradleException("This build script must be applied to the root project only")
25+
if (project.path != ":" && "-plugin" !in project.path) {
26+
throw GradleException(
27+
"This build script configures Artifactory connection for a build of a SonarQube plugin. " +
28+
"This script must be applied either to the root project or in a \"-plugin\" project."
29+
)
2730
}
2831

2932
// this value is present on CI
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
plugins {
18+
id("org.sonarsource.cloud-native.java-conventions")
19+
id("org.sonarsource.cloud-native.code-style-conventions")
20+
id("org.sonarsource.cloud-native.publishing-configuration")
21+
id("com.gradleup.shadow")
22+
}
23+
24+
val cleanupTask = tasks.register<Delete>("cleanupOldVersion") {
25+
group = "build"
26+
description = "Clean up jars of old plugin version"
27+
28+
delete(
29+
fileTree(project.layout.buildDirectory.dir("libs")).matching {
30+
include("${project.name}-*.jar")
31+
exclude("${project.name}-${project.version}-*.jar")
32+
}
33+
)
34+
}
35+
36+
artifacts {
37+
archives(tasks.shadowJar)
38+
}
39+
40+
tasks.shadowJar {
41+
dependsOn(cleanupTask)
42+
}

gradle-modules/src/main/kotlin/org/sonarsource/cloudnative/gradle/BuildUtils.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
package org.sonarsource.cloudnative.gradle
1818

19+
import java.io.File
20+
import java.util.HashSet
21+
import java.util.jar.JarInputStream
22+
import org.gradle.api.GradleException
1923
import org.gradle.api.Project
2024
import org.gradle.api.artifacts.dsl.RepositoryHandler
2125
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
@@ -65,3 +69,38 @@ fun Exec.callMake(arg: String) {
6569
commandLine("./make.sh", arg)
6670
}
6771
}
72+
73+
fun enforceJarSize(
74+
file: File,
75+
minSize: Long,
76+
maxSize: Long,
77+
) {
78+
val size = file.length()
79+
if (size < minSize) {
80+
throw GradleException("${file.path} size ($size) too small. Min is $minSize")
81+
} else if (size > maxSize) {
82+
throw GradleException("${file.path} size ($size) too large. Max is $maxSize")
83+
}
84+
}
85+
86+
fun checkJarEntriesPathUniqueness(file: File) {
87+
val allNames = HashSet<String>()
88+
val duplicatedNames = HashSet<String>()
89+
file.inputStream().use { input ->
90+
JarInputStream(input).use { jarInput ->
91+
for (jarEntry in generateSequence { jarInput.nextJarEntry }) {
92+
if (!allNames.add(jarEntry.name)) {
93+
duplicatedNames.add(jarEntry.name)
94+
}
95+
}
96+
}
97+
}
98+
if (duplicatedNames.isNotEmpty()) {
99+
throw GradleException("Duplicated entries in the jar: '${file.path}': ${duplicatedNames.joinToString(", ")}")
100+
}
101+
}
102+
103+
fun Project.commitHashProvider(ref: String = "HEAD") =
104+
providers.exec {
105+
commandLine("git", "rev-parse", ref)
106+
}.standardOutput.asText

gradle-modules/src/main/kotlin/org/sonarsource/cloudnative/gradle/CommonSettingsPlugin.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,17 @@ open class CommonSettingsPlugin
8181

8282
private fun Settings.configureDevelocity() {
8383
val develocity = extensions.getByType<DevelocityConfiguration>()
84+
val isCI = System.getenv("CI") != null
8485

8586
extensions.configure<DevelocityConfiguration> {
8687
server = "https://develocity.sonar.build"
8788
buildScan {
89+
publishing {
90+
onlyIf {
91+
isCI
92+
}
93+
}
94+
8895
tag(if (System.getenv("CI").isNullOrEmpty()) "local" else "CI")
8996
tag(System.getProperty("os.name"))
9097
if (System.getenv("CIRRUS_BRANCH") == "master") {
@@ -105,7 +112,6 @@ open class CommonSettingsPlugin
105112
}
106113
}
107114

108-
val isCI = System.getenv("CI") != null
109115
buildCache {
110116
local {
111117
isEnabled = !isCI

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ blowdryer-gradle = "1.7.1"
44
develocity = "3.18.2"
55
jfrog-gradle = "5.2.5"
66
rule-api = "2.9.0.4061"
7+
gradle-plugin-shadow = "8.3.5"
78

89
[libraries]
910
diffplug-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless-gradle" }
1011
diffplug-blowdryer-setup = { module = "com.diffplug.blowdryerSetup:com.diffplug.blowdryerSetup.gradle.plugin", version.ref = "blowdryer-gradle" }
1112
develocity = { module = "com.gradle.develocity:com.gradle.develocity.gradle.plugin", version.ref = "develocity" }
1213
jfrog-buildinfo-gradle = { module = "org.jfrog.buildinfo:build-info-extractor-gradle", version.ref = "jfrog-gradle" }
1314
sonar-rule-api = { module = "org.sonarsource.rule-api:rule-api", version.ref = "rule-api" }
15+
shadow = { module = "com.gradleup.shadow:com.gradleup.shadow.gradle.plugin", version.ref = "gradle-plugin-shadow" }
1416

1517
[plugins]
1618
spotless = { id = "com.diffplug.spotless", version.ref = "spotless-gradle" }

0 commit comments

Comments
 (0)