Skip to content

Commit 46a3607

Browse files
authored
SONARGO-82 Add "artifactory-configuration" to common Gradle modules (#6)
1 parent 3a87c25 commit 46a3607

7 files changed

Lines changed: 207 additions & 4 deletions

File tree

gradle-modules/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ dependencies {
2424
implementation(libs.develocity)
2525
implementation(libs.diffplug.spotless)
2626
implementation(libs.diffplug.blowdryer.setup)
27+
implementation(libs.jfrog.buildinfo.gradle) {
28+
exclude("ch.qos.logback", "logback-core")
29+
}
2730
}
2831

2932
gradlePlugin {
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 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+
import org.sonarsource.cloudnative.gradle.ArtifactoryConfiguration
18+
import org.sonarsource.cloudnative.gradle.signingCondition
19+
20+
plugins {
21+
id("com.jfrog.artifactory")
22+
signing
23+
`maven-publish`
24+
}
25+
26+
// this value is present on CI
27+
val buildNumber: String? = System.getProperty("buildNumber")
28+
if (project.version.toString().endsWith("-SNAPSHOT") && buildNumber != null) {
29+
val versionSuffix = if (project.version.toString().count { it == '.' } == 1) ".0.$buildNumber" else ".$buildNumber"
30+
project.version = project.version.toString().replace("-SNAPSHOT", versionSuffix)
31+
logger.lifecycle("Project version set to $version")
32+
}
33+
34+
val artifactoryConfiguration = extensions.create<ArtifactoryConfiguration>("artifactoryConfiguration")
35+
36+
publishing {
37+
publications.create<MavenPublication>("mavenJava") {
38+
pom {
39+
name = artifactoryConfiguration.pomName
40+
description = project.description
41+
url = "http://www.sonarqube.org/"
42+
organization {
43+
name = "SonarSource"
44+
url = "http://www.sonarsource.com/"
45+
}
46+
licenses {
47+
license {
48+
name = artifactoryConfiguration.license.name
49+
url = artifactoryConfiguration.license.url
50+
distribution = artifactoryConfiguration.license.distribution
51+
comments = artifactoryConfiguration.license.comments
52+
}
53+
}
54+
scm {
55+
url = artifactoryConfiguration.scmUrl
56+
}
57+
developers {
58+
developer {
59+
id = "sonarsource-team"
60+
name = "SonarSource Team"
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
signing {
68+
val signingKeyId: String? by project
69+
val signingKey: String? by project
70+
val signingPassword: String? by project
71+
useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
72+
setRequired {
73+
project.signingCondition()
74+
}
75+
sign(publishing.publications)
76+
}
77+
78+
tasks.withType<Sign> {
79+
onlyIf {
80+
val artifactorySkip: Boolean = tasks.artifactoryPublish.get().skip
81+
!artifactorySkip && project.signingCondition()
82+
}
83+
}
84+
85+
// `afterEvaluate` is required to inject configurable properties; see https://github.com/jfrog/artifactory-gradle-plugin/issues/71#issuecomment-1734977528
86+
project.afterEvaluate {
87+
artifactory {
88+
if (artifactoryConfiguration.artifactsToPublish.isPresent) {
89+
clientConfig.info.addEnvironmentProperty(
90+
"ARTIFACTS_TO_PUBLISH",
91+
artifactoryConfiguration.artifactsToPublish.get()
92+
)
93+
clientConfig.info.addEnvironmentProperty(
94+
"ARTIFACTS_TO_DOWNLOAD",
95+
artifactoryConfiguration.artifactsToDownload.getOrElse("")
96+
)
97+
}
98+
99+
setContextUrl(System.getenv("ARTIFACTORY_URL"))
100+
// Note: `publish` should only be called once: https://github.com/jfrog/artifactory-gradle-plugin/issues/111
101+
publish {
102+
if (artifactoryConfiguration.repoKeyEnv.isPresent) {
103+
repository {
104+
repoKey = System.getenv(artifactoryConfiguration.repoKeyEnv.get())
105+
username = System.getenv(artifactoryConfiguration.usernameEnv.get())
106+
password = System.getenv(artifactoryConfiguration.passwordEnv.get())
107+
}
108+
}
109+
defaults {
110+
publications("mavenJava")
111+
setProperties(
112+
mapOf(
113+
"build.name" to artifactoryConfiguration.buildName.get(),
114+
"version" to project.version.toString(),
115+
"build.number" to buildNumber,
116+
"pr.branch.target" to System.getenv("PULL_REQUEST_BRANCH_TARGET"),
117+
"pr.number" to System.getenv("PULL_REQUEST_NUMBER"),
118+
"vcs.branch" to System.getenv("GIT_BRANCH"),
119+
"vcs.revision" to System.getenv("GIT_COMMIT")
120+
)
121+
)
122+
setPublishArtifacts(true)
123+
setPublishPom(true)
124+
setPublishIvy(false)
125+
}
126+
}
127+
128+
clientConfig.info.addEnvironmentProperty("PROJECT_VERSION", project.version.toString())
129+
clientConfig.info.buildName = artifactoryConfiguration.buildName.get()
130+
clientConfig.info.buildNumber = buildNumber
131+
clientConfig.isIncludeEnvVars = true
132+
clientConfig.envVarsExcludePatterns =
133+
"*password*,*PASSWORD*,*secret*,*MAVEN_CMD_LINE_ARGS*,sun.java.command," +
134+
"*token*,*TOKEN*,*LOGIN*,*login*,*key*,*KEY*,*PASSPHRASE*,*signing*"
135+
}
136+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 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+
package org.sonarsource.cloudnative.gradle
18+
19+
import org.gradle.api.model.ObjectFactory
20+
import org.gradle.api.provider.Property
21+
import org.gradle.api.publish.maven.MavenPomLicense
22+
import org.gradle.kotlin.dsl.newInstance
23+
import org.gradle.kotlin.dsl.property
24+
25+
open class ArtifactoryConfiguration(
26+
objects: ObjectFactory,
27+
) {
28+
val pomName: Property<String> = objects.property()
29+
val buildName: Property<String> = objects.property()
30+
val scmUrl: Property<String> = objects.property()
31+
val artifactsToPublish: Property<String> = objects.property()
32+
val artifactsToDownload: Property<String> = objects.property()
33+
val repoKeyEnv: Property<String> = objects.property()
34+
val usernameEnv: Property<String> = objects.property()
35+
val passwordEnv: Property<String> = objects.property()
36+
internal val license: MavenPomLicense = objects.newInstance()
37+
38+
fun license(action: MavenPomLicense.() -> Unit) {
39+
action.invoke(license)
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* SonarSource Cloud Native Gradle Modules
3+
* Copyright (C) 2024-2024 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+
package org.sonarsource.cloudnative.gradle
18+
19+
import org.gradle.api.Project
20+
21+
fun Project.signingCondition(): Boolean {
22+
val branch = System.getenv()["CIRRUS_BRANCH"] ?: ""
23+
return (branch == "master" || branch.matches("branch-[\\d.]+".toRegex())) &&
24+
gradle.taskGraph.hasTask(":artifactoryPublish")
25+
}

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

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

19-
import com.diffplug.gradle.spotless.SpotlessExtension
20-
import org.gradle.api.Project
2119
import org.gradle.api.provider.Property
22-
import org.gradle.kotlin.dsl.getByType
2320

2421
interface CodeStyleConvention {
2522
val editorConfigPath: Property<String>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import org.gradle.api.artifacts.repositories.MavenArtifactRepository
2424
import org.gradle.api.initialization.Settings
2525
import org.gradle.api.internal.file.FileOperations
2626
import org.gradle.api.provider.ProviderFactory
27-
import org.gradle.internal.extensions.core.extra
2827
import org.gradle.kotlin.dsl.assign
2928
import org.gradle.kotlin.dsl.configure
3029
import org.gradle.kotlin.dsl.getByType

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
spotless-gradle = "6.25.0"
33
blowdryer-gradle = "1.7.1"
44
develocity = "3.18.2"
5+
jfrog-gradle = "5.2.5"
56

67
[libraries]
78
diffplug-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless-gradle" }
89
diffplug-blowdryer-setup = { module = "com.diffplug.blowdryerSetup:com.diffplug.blowdryerSetup.gradle.plugin", version.ref = "blowdryer-gradle" }
910
develocity = { module = "com.gradle.develocity:com.gradle.develocity.gradle.plugin", version.ref = "develocity" }
11+
jfrog-buildinfo-gradle = { module = "org.jfrog.buildinfo:build-info-extractor-gradle", version.ref = "jfrog-gradle" }
1012

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

0 commit comments

Comments
 (0)