|
| 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 | +} |
0 commit comments