View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * All Maven properties allowed to be referenced in parent versions via expressions.
37       *
38       * @see <a href="https://maven.apache.org/maven-ci-friendly.html">CI-Friendly Versions</a>
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       * Rewrites the CI friendly properties in the given properties object based on the provided version and release descriptor.
64       *
65       * @param version
66       * @param properties
67       * @param releaseDescriptor
68       */
69      public static void rewriteCiFriendlyProperties(
70              String version, Properties properties, ReleaseDescriptor releaseDescriptor) {
71          // try to rewrite property if CI friendly expression is used
72          if (properties != null) {
73              String sha1 = resolveSha1Property(properties, releaseDescriptor);
74              // assume that everybody follows the example and properties are simply chained
75              //  and the changelist can only be '-SNAPSHOT'
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                      // drop the value for the next version
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                      // we need this to restore the revision for the next development
96                      // or release:prepare should provide sha1 after a commit
97                      // or a user should provide it as an additional `arguments` in plugin configuration
98                      // see maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module
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 }