|
| 1 | +package org.jfrog.build |
| 2 | + |
| 3 | +import org.gradle.api.tasks.bundling.Jar |
| 4 | + |
| 5 | +import org.gradle.BuildAdapter |
| 6 | +import org.gradle.api.Action |
| 7 | +import org.gradle.api.Plugin |
| 8 | +import org.gradle.api.Project |
| 9 | +import org.gradle.api.artifacts.Dependency |
| 10 | +import org.gradle.api.invocation.Gradle |
| 11 | +import org.gradle.api.plugins.PluginContainer |
| 12 | +import org.gradle.api.specs.Spec |
| 13 | +import org.gradle.api.tasks.Upload |
| 14 | +import org.jfrog.build.api.constants.BuildInfoProperties |
| 15 | +import org.jfrog.build.extractor.gradle.BuildInfoRecorderTask |
| 16 | +import org.slf4j.Logger |
| 17 | + |
| 18 | +class ArtifactoryPlugin implements Plugin<Project> { |
| 19 | + private static final Logger log = org.slf4j.LoggerFactory.getLogger(ArtifactoryPlugin.class); |
| 20 | + public static final String ENCODING = "UTF-8" |
| 21 | + |
| 22 | + List<String> compatiblePlugins = ['java', 'scala', 'groovy'] as List |
| 23 | + |
| 24 | + def void apply(Project project) { |
| 25 | + |
| 26 | + log.debug("Using Artifactory Plugin") |
| 27 | + def artifactoryUrl = getProperty('artifactory.url', project) ?: 'http://gradle.artifactoryonline.com/gradle/' |
| 28 | + def downloadId = getProperty('artifactory.downloadId', project) |
| 29 | + if (!downloadId) { |
| 30 | + // take the target repository from the full url |
| 31 | + String[] pathParts = artifactoryUrl.split("/") |
| 32 | + if (pathParts.size() >= 3) { |
| 33 | + downloadId = pathParts[2] |
| 34 | + } |
| 35 | + //TODO: [by ys] why plugins releases in the default? |
| 36 | + downloadId = downloadId ?: 'plugins-releases' |
| 37 | + } |
| 38 | + def artifactoryDownloadUrl = getProperty('artifactory.downloadUrl', project) ?: "${artifactoryUrl}/${downloadId}" |
| 39 | + log.debug("Artifactory URL: $artifactoryUrl") |
| 40 | + log.debug("Artifactory Download ID: $downloadId") |
| 41 | + log.debug("Artifactory Download URL: $artifactoryDownloadUrl") |
| 42 | + // add artifactory url to the list of repositories |
| 43 | + project.repositories { |
| 44 | + mavenRepo urls: [artifactoryDownloadUrl] |
| 45 | + } |
| 46 | + |
| 47 | + log.debug("Configuring BuildInfo task") |
| 48 | + |
| 49 | + PluginContainer plugins = project.getPlugins(); |
| 50 | + def isRoot = project.equals(project.getRootProject()) |
| 51 | + log.debug "Configuring project ${project.name}: is root? ${isRoot}" |
| 52 | + // add the build info task only to supported projects (always include the root) |
| 53 | + plugins.matching(new Spec<Plugin>() { |
| 54 | + boolean isSatisfiedBy(Plugin plugin) { |
| 55 | + log.debug "Has plugin ${plugin.class} Is isSatisfied? ${compatiblePlugins.contains(plugin.class) || isRoot}" |
| 56 | + for (String pluginId: compatiblePlugins) { |
| 57 | + if (plugins.hasPlugin(pluginId)) { |
| 58 | + return true |
| 59 | + } |
| 60 | + } |
| 61 | + if (isRoot) { |
| 62 | + return true |
| 63 | + } |
| 64 | + return false |
| 65 | + } |
| 66 | + }).allPlugins(new Action<Plugin>() { |
| 67 | + public void execute(Plugin plugin) { |
| 68 | + log.debug "Configuring BuildInfoTask for project ${project.name} because of plugin ${plugin.class}" |
| 69 | + configureBuildInfoTask(project); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + def uploadId = getProperty("artifactory.uploadId", project) |
| 74 | + if (uploadId) { |
| 75 | + // configure upload repository for maven deployer or ivy publisher |
| 76 | + log.debug "ArtifactoryURL before setting upload is ${artifactoryUrl}" |
| 77 | + log.debug("Upload ID was declared: ${uploadId} and so deployArchives task reconfigured") |
| 78 | + String uploadRootUrl = getProperty("artifactory.uploadRootUrl", project) |
| 79 | + String uploadUrl = "${artifactoryUrl}/${uploadId}" |
| 80 | + if (uploadRootUrl) { |
| 81 | + log.debug "Using Artifactory Upload Root URL: $uploadRootUrl" |
| 82 | + uploadUrl = "${uploadRootUrl}/${uploadId}" |
| 83 | + } |
| 84 | + |
| 85 | + log.debug("Configure Upload URL to ${uploadUrl}") |
| 86 | + uploadUrl = appendProperties(uploadUrl, project) |
| 87 | + |
| 88 | + def user = getProperty("artifactory.username", project) ?: "anonymous" |
| 89 | + def password = getProperty("artifactory.password", project) ?: "" |
| 90 | + def host = new URI(uploadUrl).getHost() |
| 91 | + project.tasks.withType(Upload.class).allObjects { uploadTask -> |
| 92 | + project.configure(uploadTask) { |
| 93 | + boolean deployIvy |
| 94 | + def deployIvyProp = getProperty("artifactory.deployIvy", project); |
| 95 | + if (deployIvyProp != null) { |
| 96 | + deployIvy = Boolean.parseBoolean(deployIvyProp); |
| 97 | + } else { |
| 98 | + deployIvy = true |
| 99 | + } |
| 100 | + if (deployIvy) { |
| 101 | + log.debug "Configuring Ivy repository" |
| 102 | + repositories { |
| 103 | + add(new org.apache.ivy.plugins.resolver.URLResolver()) { |
| 104 | + org.apache.ivy.util.url.CredentialsStore.INSTANCE.addCredentials("Artifactory Realm", host, user, password) |
| 105 | + name = 'artifactory' |
| 106 | + addIvyPattern "$uploadUrl/[organisation]/[module]/[revision]/ivy-[revision].xml" |
| 107 | + addArtifactPattern "$uploadUrl/[organisation]/[module]/[revision]/[module]-[revision].[ext]" |
| 108 | + descriptor = 'optional' |
| 109 | + checkmodified = true |
| 110 | + m2compatible = true |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + boolean deployMaven |
| 115 | + def deployMavenProp = getProperty("artifactory.deployMaven", project); |
| 116 | + if (deployMavenProp != null) { |
| 117 | + deployMaven = Boolean.parseBoolean(deployMavenProp); |
| 118 | + } else { |
| 119 | + deployMaven = true |
| 120 | + } |
| 121 | + if (deployMaven) { |
| 122 | + log.debug "Configuring Maven repository" |
| 123 | + repositories.mavenDeployer { |
| 124 | + repository(url: uploadUrl) { |
| 125 | + authentication(userName: user, password: password) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } else { |
| 132 | + if (project.getRootProject().equals(project)) { |
| 133 | + log.warn "Upload ID was not declared, no actual deployment will be performed." |
| 134 | + } |
| 135 | + } |
| 136 | + project.getGradle().addBuildListener(new BuildAdapter() { |
| 137 | + def void projectsEvaluated(Gradle gradle) { |
| 138 | + String buildStart = System.getProperty("build.start"); |
| 139 | + if (!buildStart) { |
| 140 | + System.setProperty("build.start", Long.toString(System.currentTimeMillis())); |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | + |
| 146 | + //TODO: [by ys] support additional matrix params by reading plugin configuration + system properties |
| 147 | + |
| 148 | + String appendProperties(String uploadUrl, Project project) { |
| 149 | + String buildNumber = getProperty(BuildInfoProperties.PROP_BUILD_NUMBER, project) |
| 150 | + String buildName = getProperty(BuildInfoProperties.PROP_BUILD_NAME, project) |
| 151 | + String buildParentNumber = getProperty(BuildInfoProperties.PROP_PARENT_BUILD_NUMBER, project) |
| 152 | + String buildParentName = getProperty(BuildInfoProperties.PROP_PARENT_BUILD_NAME, project) |
| 153 | + StringBuilder builder = new StringBuilder(uploadUrl); |
| 154 | + if (buildNumber) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NUMBER}=${URLEncoder.encode(buildNumber, ENCODING)}") |
| 155 | + if (buildName) builder.append(";${BuildInfoProperties.PROP_BUILD_NAME}=${URLEncoder.encode(buildName, ENCODING)}") |
| 156 | + if (buildParentNumber) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NUMBER}=${URLEncoder.encode(buildParentNumber, ENCODING)}") |
| 157 | + if (buildParentName) builder.append(";${BuildInfoProperties.PROP_PARENT_BUILD_NAME}=${URLEncoder.encode(buildParentName, ENCODING)}") |
| 158 | + project.properties.keySet().each {String key -> |
| 159 | + if (key.startsWith('artifactory.ci.matrix')) { |
| 160 | + builder.append(";$key=${URLEncoder.encode(getProperty(key, project), ENCODING)}") |
| 161 | + } |
| 162 | + } |
| 163 | + return builder.toString() |
| 164 | + } |
| 165 | + |
| 166 | + private void configureBuildInfoTask(Project project) { |
| 167 | + if (project.tasks.findByName("buildInfo")) { |
| 168 | + return |
| 169 | + } |
| 170 | + BuildInfoRecorderTask buildInfo = project.getTasks().add("buildInfo", BuildInfoRecorderTask.class) |
| 171 | + buildInfo.dependsOn({ |
| 172 | + (project.getTasks().withType(Jar)).all |
| 173 | + }) |
| 174 | + |
| 175 | + def archivesConfiguration = project.getConfigurations().findByName(Dependency.ARCHIVES_CONFIGURATION) |
| 176 | + if (archivesConfiguration != null) { |
| 177 | + buildInfo.setConfiguration(archivesConfiguration) |
| 178 | + } |
| 179 | + buildInfo.setDescription("Generates build info from build artifacts"); |
| 180 | + } |
| 181 | + |
| 182 | + private String getProperty(String propertyName, Project project) { |
| 183 | + if (System.getProperty(propertyName) != null) { |
| 184 | + return System.getProperty(propertyName) |
| 185 | + } |
| 186 | + if (project.hasProperty(propertyName)) { |
| 187 | + return project.property(propertyName); |
| 188 | + } else { |
| 189 | + project = project.getParent(); |
| 190 | + if (project == null) { |
| 191 | + return null; |
| 192 | + } else { |
| 193 | + return getProperty(propertyName, project); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments