1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.release.util;
20
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.Properties;
24 import java.util.Set;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.artifact.ArtifactUtils;
28 import org.apache.maven.shared.release.config.ReleaseDescriptor;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class CiFriendlyVersion {
33 private static final Logger LOGGER = LoggerFactory.getLogger(CiFriendlyVersion.class);
34
35
36
37
38
39
40 public static final String REVISION = "revision";
41
42 public static final String SHA1 = "sha1";
43 public static final String CHANGELIST = "changelist";
44
45 private static final Set<String> CI_FRIENDLY_PROPERTIES = new HashSet<>(Arrays.asList(REVISION, SHA1, CHANGELIST));
46
47 private static final String SNAPSHOT = "-SNAPSHOT";
48
49 private CiFriendlyVersion() {}
50
51 public static boolean isCiFriendlyVersion(String version) {
52 if (StringUtils.isEmpty(version)) {
53 return false;
54 }
55 return isCiFriendlyProperty(MavenExpression.extractPropertyFromExpression(version));
56 }
57
58 public static boolean isCiFriendlyProperty(String property) {
59 return CI_FRIENDLY_PROPERTIES.contains(property);
60 }
61
62
63
64
65
66
67
68
69 public static void rewriteCiFriendlyProperties(
70 String version, Properties properties, ReleaseDescriptor releaseDescriptor) {
71
72 if (properties != null) {
73 String sha1 = resolveSha1Property(properties, releaseDescriptor);
74
75
76 if (ArtifactUtils.isSnapshot(version)) {
77 if (properties.containsKey(CHANGELIST)) {
78 String revision = version.replace(sha1, "").replace(SNAPSHOT, "");
79 setAndLogPropertyChange(properties, REVISION, revision);
80 setAndLogPropertyChange(properties, CHANGELIST, SNAPSHOT);
81 } else {
82 String revision = version.replace(sha1, "");
83 setAndLogPropertyChange(properties, REVISION, revision);
84 }
85 if (properties.containsKey(SHA1)) {
86
87 setAndLogPropertyChange(properties, SHA1, "");
88 }
89 } else {
90 properties.setProperty(REVISION, version.replace(sha1, ""));
91 if (properties.containsKey(CHANGELIST)) {
92 setAndLogPropertyChange(properties, CHANGELIST, "");
93 }
94 if (properties.containsKey(SHA1) && !sha1.isEmpty()) {
95
96
97
98
99 setAndLogPropertyChange(properties, SHA1, sha1);
100 }
101 }
102 }
103 }
104
105 private static void setAndLogPropertyChange(Properties properties, String key, String value) {
106 LOGGER.info("Updating CI friendly property {} to {}", key, value);
107 properties.setProperty(key, value);
108 }
109
110 public static String resolveSha1Property(Properties properties, ReleaseDescriptor releaseDescriptor) {
111 String sha1 = properties.getProperty(SHA1);
112 String scmVersion = releaseDescriptor.getScmReleasedPomRevision();
113 String systemSha1 = System.getProperty(SHA1);
114 String result = StringUtils.isNotEmpty(systemSha1)
115 ? systemSha1
116 : StringUtils.isNotEmpty(sha1) ? sha1 : scmVersion != null ? scmVersion : "";
117 LOGGER.info("Resolved SHA1 property value {}", result);
118 return result;
119 }
120 }