|
| 1 | +/* |
| 2 | + * Copyright (C) 2010 JFrog Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.jfrog.build.extractor.maven; |
| 18 | + |
| 19 | +import com.google.common.collect.Sets; |
| 20 | +import org.apache.commons.lang.StringUtils; |
| 21 | +import org.codehaus.plexus.component.annotations.Component; |
| 22 | +import org.codehaus.plexus.component.annotations.Requirement; |
| 23 | +import org.codehaus.plexus.logging.Logger; |
| 24 | +import org.jfrog.build.api.Artifact; |
| 25 | +import org.jfrog.build.api.Build; |
| 26 | +import org.jfrog.build.api.BuildInfoProperties; |
| 27 | +import org.jfrog.build.api.Module; |
| 28 | +import org.jfrog.build.api.util.FileChecksumCalculator; |
| 29 | +import org.jfrog.build.client.ArtifactoryBuildInfoClient; |
| 30 | +import org.jfrog.build.client.ClientProperties; |
| 31 | +import org.jfrog.build.client.DeployDetails; |
| 32 | +import org.jfrog.build.client.IncludeExcludePatterns; |
| 33 | +import org.jfrog.build.client.PatternMatcher; |
| 34 | +import org.jfrog.build.extractor.BuildInfoExtractorUtils; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Properties; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Noam Y. Tenne |
| 45 | + */ |
| 46 | +@Component(role = BuildDeploymentHelper.class) |
| 47 | +public class BuildDeploymentHelper { |
| 48 | + |
| 49 | + @Requirement |
| 50 | + private Logger logger; |
| 51 | + |
| 52 | + @Requirement |
| 53 | + private ClientPropertyResolver clientPropertyResolver; |
| 54 | + |
| 55 | + public void deploy(Build build, boolean isPublishArtifacts, Properties allProps, |
| 56 | + Map<Artifact, DeployDetails> deployableArtifactBuilders) { |
| 57 | + Set<DeployDetails> deployableArtifacts = prepareDeployableArtifacts(build, deployableArtifactBuilders); |
| 58 | + |
| 59 | + String outputFile = allProps.getProperty(BuildInfoProperties.PROP_BUILD_INFO_OUTPUT_FILE); |
| 60 | + logger.debug( |
| 61 | + "Build Info Recorder: " + BuildInfoProperties.PROP_BUILD_INFO_OUTPUT_FILE + " = " + outputFile); |
| 62 | + if (StringUtils.isNotBlank(outputFile)) { |
| 63 | + try { |
| 64 | + logger.info("Artifactory Build Info Recorder: Saving build info to " + outputFile); |
| 65 | + BuildInfoExtractorUtils.saveBuildInfoToFile(build, new File(outputFile)); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new RuntimeException("Error occurred while persisting Build Info to file.", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + boolean publishBuildInfo = isPublishBuildInfo(allProps); |
| 72 | + logger.debug("Build Info Recorder: " + ClientProperties.PROP_PUBLISH_BUILD_INFO + " = " + publishBuildInfo); |
| 73 | + logger.debug("Build Info Recorder: " + ClientProperties.PROP_PUBLISH_ARTIFACT + " = " + isPublishArtifacts); |
| 74 | + if (publishBuildInfo || isPublishArtifacts) { |
| 75 | + ArtifactoryBuildInfoClient client = clientPropertyResolver.resolveProperties(allProps); |
| 76 | + try { |
| 77 | + if (isPublishArtifacts && (deployableArtifacts != null) && !deployableArtifacts.isEmpty()) { |
| 78 | + deployArtifacts(allProps, deployableArtifacts, client); |
| 79 | + } |
| 80 | + |
| 81 | + if (publishBuildInfo) { |
| 82 | + try { |
| 83 | + logger.info("Artifactory Build Info Recorder: Deploying build info ..."); |
| 84 | + client.sendBuildInfo(build); |
| 85 | + } catch (IOException e) { |
| 86 | + throw new RuntimeException("Error occurred while publishing Build Info to Artifactory.", e); |
| 87 | + } |
| 88 | + } |
| 89 | + } finally { |
| 90 | + client.shutdown(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private Set<DeployDetails> prepareDeployableArtifacts(Build build, |
| 96 | + Map<Artifact, DeployDetails> deployableArtifactBuilders) { |
| 97 | + Set<DeployDetails> deployableArtifacts = Sets.newLinkedHashSet(); |
| 98 | + List<Module> modules = build.getModules(); |
| 99 | + for (Module module : modules) { |
| 100 | + List<Artifact> artifacts = module.getArtifacts(); |
| 101 | + for (Artifact artifact : artifacts) { |
| 102 | + DeployDetails deployable = deployableArtifactBuilders.get(artifact); |
| 103 | + if (deployable != null) { |
| 104 | + File file = deployable.getFile(); |
| 105 | + setArtifactChecksums(file, artifact); |
| 106 | + deployableArtifacts.add(new DeployDetails.Builder().artifactPath(deployable.getArtifactPath()). |
| 107 | + file(file).md5(artifact.getMd5()).sha1(artifact.getSha1()). |
| 108 | + addProperties(deployable.getProperties()). |
| 109 | + targetRepository(deployable.getTargetRepository()).build()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return deployableArtifacts; |
| 114 | + } |
| 115 | + |
| 116 | + private void deployArtifacts(Properties allProps, Set<DeployDetails> deployableArtifacts, |
| 117 | + ArtifactoryBuildInfoClient client) { |
| 118 | + logger.info("Artifactory Build Info Recorder: Deploying artifacts to " + |
| 119 | + allProps.getProperty(ClientProperties.PROP_CONTEXT_URL)); |
| 120 | + |
| 121 | + IncludeExcludePatterns includeExcludePatterns = getArtifactDeploymentPatterns(allProps); |
| 122 | + for (DeployDetails artifact : deployableArtifacts) { |
| 123 | + String artifactPath = artifact.getArtifactPath(); |
| 124 | + if (PatternMatcher.pathConflicts(artifactPath, includeExcludePatterns)) { |
| 125 | + logger.info("Artifactory Build Info Recorder: Skipping the deployment of '" + |
| 126 | + artifactPath + "' due to the defined include-exclude patterns."); |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + try { |
| 131 | + client.deployArtifact(artifact); |
| 132 | + } catch (IOException e) { |
| 133 | + throw new RuntimeException("Error occurred while publishing artifact to Artifactory: " + |
| 134 | + artifact.getFile() + |
| 135 | + ".\n Skipping deployment of remaining artifacts (if any) and build info.", e); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void setArtifactChecksums(File artifactFile, org.jfrog.build.api.Artifact artifact) { |
| 141 | + if ((artifactFile != null) && (artifactFile.isFile())) { |
| 142 | + try { |
| 143 | + Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(artifactFile, "md5", "sha1"); |
| 144 | + artifact.setMd5(checksums.get("md5")); |
| 145 | + artifact.setSha1(checksums.get("sha1")); |
| 146 | + } catch (Exception e) { |
| 147 | + logger.error("Could not set checksum values on '" + artifact.getName() + "': " + e.getMessage(), e); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private boolean isPublishBuildInfo(Properties allProps) { |
| 153 | + return Boolean.valueOf(allProps.getProperty(ClientProperties.PROP_PUBLISH_BUILD_INFO)); |
| 154 | + } |
| 155 | + |
| 156 | + private IncludeExcludePatterns getArtifactDeploymentPatterns(Properties allProps) { |
| 157 | + return new IncludeExcludePatterns(allProps.getProperty(ClientProperties.PROP_PUBLISH_ARTIFACT_INCLUDE_PATTERNS), |
| 158 | + allProps.getProperty(ClientProperties.PROP_PUBLISH_ARTIFACT_EXCLUDE_PATTERNS)); |
| 159 | + } |
| 160 | +} |
0 commit comments