|
| 1 | +import java.time.ZoneOffset |
| 2 | +import java.time.ZonedDateTime |
| 3 | +import java.time.temporal.ChronoField |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright (c) 2011-Present Pivotal Software Inc, All Rights Reserved. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +if (rootProject.hasProperty("releaserDryRun") && rootProject.findProperty("releaserDryRun") != "false") { |
| 22 | + println "Adding MavenLocal() for benefit of releaser dry run" |
| 23 | + rootProject.repositories { |
| 24 | + mavenLocal() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * return a specific property value, assuming all the provided projects have it with the |
| 30 | + * same value, or throw if multiple values are found |
| 31 | + */ |
| 32 | +private static def getUniquePropertyPerProject(Set<Project> projects, String property) { |
| 33 | + //"multimap": dictionary of lists |
| 34 | + def props = [:].withDefault {[]} |
| 35 | + projects.each { props.get(it.findProperty(property)).add(it.name) } |
| 36 | + |
| 37 | + if (props.size() != 1) { |
| 38 | + throw new InvalidUserDataException("build defines multiple values for property `${property}`: ${props}") |
| 39 | + } |
| 40 | + return props.keySet().find() |
| 41 | +} |
| 42 | + |
| 43 | +//NOTE: this task is intended for rootProject with submodules |
| 44 | +task groupId(group: "releaser helpers", description: "output the group id of submodules, checking there is only one") { |
| 45 | + doLast { |
| 46 | + println getUniquePropertyPerProject(rootProject.subprojects, "group") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +task copyReadme(type: Copy, group: "releaser helpers", description: "copies the README in preparation for search and replace") { |
| 51 | + from(rootProject.rootDir) { |
| 52 | + include "README.md" |
| 53 | + } |
| 54 | + into rootProject.buildDir |
| 55 | +} |
| 56 | + |
| 57 | +task bumpVersionsInReadme(type: Copy, group: "releaser helpers", description: "replaces versions in README") { |
| 58 | + def oldVersion = rootProject.findProperty("oldVersion") |
| 59 | + def currentVersion = rootProject.findProperty("currentVersion") |
| 60 | + def nextVersion = rootProject.findProperty("nextVersion") |
| 61 | + def oldSnapshot = currentVersion?.replace("RELEASE", "BUILD-SNAPSHOT") |
| 62 | + |
| 63 | + onlyIf { oldVersion != null && currentVersion != null && nextVersion != null } |
| 64 | + dependsOn copyReadme |
| 65 | + |
| 66 | + doLast { |
| 67 | + println "Will replace $oldVersion with $currentVersion and $oldSnapshot with $nextVersion" |
| 68 | + } |
| 69 | + from(rootProject.buildDir) { |
| 70 | + include 'README.md' |
| 71 | + } |
| 72 | + into rootProject.rootDir |
| 73 | + filter { line -> line |
| 74 | + .replace(oldVersion, currentVersion) |
| 75 | + .replace(oldSnapshot, nextVersion) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +String getOrGenerateBuildNumber() { |
| 80 | + if (project.hasProperty("buildNumber")) { |
| 81 | + return project.findProperty("buildNumber") |
| 82 | + } |
| 83 | + def jenkinsNumber = System.getenv("BUILD_NUMBER") |
| 84 | + if (jenkinsNumber != null) { |
| 85 | + return jenkinsNumber |
| 86 | + } |
| 87 | + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC) |
| 88 | + long secondsInDay = now.toEpochSecond() - ZonedDateTime.now(ZoneOffset.UTC).withSecond(0).withHour(0).withMinute(0).toEpochSecond() |
| 89 | + String buildNumber = "$version-${now.get(ChronoField.YEAR)}${now.get(ChronoField.MONTH_OF_YEAR).toString().padLeft(2,'0')}${now.get(ChronoField.DAY_OF_MONTH).toString().padLeft(2,'0')}@${secondsInDay}" |
| 90 | + println "No buildNumber set, generated: $buildNumber" |
| 91 | + return buildNumber |
| 92 | +} |
| 93 | + |
| 94 | +task printBuildNumber() { |
| 95 | + doLast { |
| 96 | + println getOrGenerateBuildNumber() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +if (project.hasProperty("artifactory_publish_password")) { |
| 101 | + configure(rootProject) { p -> |
| 102 | + apply plugin: "com.jfrog.artifactory" |
| 103 | + def buildNumber = getOrGenerateBuildNumber() |
| 104 | + artifactory { |
| 105 | + contextUrl = "${artifactory_publish_contextUrl}" |
| 106 | + publish { |
| 107 | + repository { |
| 108 | + repoKey = "${artifactory_publish_repoKey}" |
| 109 | + username = "${artifactory_publish_username}" |
| 110 | + password = "${artifactory_publish_password}" |
| 111 | + } |
| 112 | + } |
| 113 | + clientConfig.setIncludeEnvVars(false) |
| 114 | + clientConfig.info.setBuildName('Reactor - Releaser - Rabbitmq') |
| 115 | + clientConfig.info.setBuildNumber(buildNumber) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments