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 public static final String REVISION = "revision";
40
41 public static final String SHA1 = "sha1";
42 public static final String CHANGELIST = "changelist";
43
44 private static final Set<String> CI_FRIENDLY_PROPERTIES = new HashSet<>(Arrays.asList(REVISION, SHA1, CHANGELIST));
45
46 private static final String SNAPSHOT = "-SNAPSHOT";
47
48 private CiFriendlyVersion() {}
49
50 public static boolean isCiFriendlyVersion(String version) {
51 if (StringUtils.isEmpty(version)) {
52 return false;
53 }
54 return isCiFriendlyProperty(MavenExpression.extractPropertyFromExpression(version));
55 }
56
57 public static boolean isCiFriendlyProperty(String property) {
58 return CI_FRIENDLY_PROPERTIES.contains(property);
59 }
60
61
62
63
64
65
66
67 public static void rewriteCiFriendlyProperties(
68 String version, Properties properties, ReleaseDescriptor releaseDescriptor) {
69
70 if (properties != null) {
71 String sha1 = resolveSha1Property(properties, releaseDescriptor);
72
73
74 if (ArtifactUtils.isSnapshot(version)) {
75 if (properties.containsKey(CHANGELIST)) {
76 String revision = version.replace(sha1, "").replace(SNAPSHOT, "");
77 setAndLogPropertyChange(properties, REVISION, revision);
78 setAndLogPropertyChange(properties, CHANGELIST, SNAPSHOT);
79 } else {
80 String revision = version.replace(sha1, "");
81 setAndLogPropertyChange(properties, REVISION, revision);
82 }
83 if (properties.containsKey(SHA1)) {
84
85 setAndLogPropertyChange(properties, SHA1, "");
86 }
87 } else {
88 properties.setProperty(REVISION, version.replace(sha1, ""));
89 if (properties.containsKey(CHANGELIST)) {
90 setAndLogPropertyChange(properties, CHANGELIST, "");
91 }
92 if (properties.containsKey(SHA1) && !sha1.isEmpty()) {
93
94
95
96
97 setAndLogPropertyChange(properties, SHA1, sha1);
98 }
99 }
100 }
101 }
102
103 private static void setAndLogPropertyChange(Properties properties, String key, String value) {
104 LOGGER.info("Updating CI friendly property {} to {}", key, value);
105 properties.setProperty(key, value);
106 }
107
108 public static String resolveSha1Property(Properties properties, ReleaseDescriptor releaseDescriptor) {
109 String sha1 = properties.getProperty(SHA1);
110 String scmVersion = releaseDescriptor.getScmReleasedPomRevision();
111 String systemSha1 = System.getProperty(SHA1);
112 String result = StringUtils.isNotEmpty(systemSha1)
113 ? systemSha1
114 : StringUtils.isNotEmpty(sha1) ? sha1 : scmVersion != null ? scmVersion : "";
115 LOGGER.info("Resolved SHA1 property value {}", result);
116 return result;
117 }
118 }