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       * @see <a href="https://maven.apache.org/maven-ci-friendly.html">CI-Friendly Versions</a>
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       * Rewrites the CI friendly properties in the given properties object based on the provided version and release descriptor.
63       * @param version
64       * @param properties
65       * @param releaseDescriptor
66       */
67      public static void rewriteCiFriendlyProperties(
68              String version, Properties properties, ReleaseDescriptor releaseDescriptor) {
69          // try to rewrite property if CI friendly expression is used
70          if (properties != null) {
71              String sha1 = resolveSha1Property(properties, releaseDescriptor);
72              // assume that everybody follows the example and properties are simply chained
73              //  and the changelist can only be '-SNAPSHOT'
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                      // drop the value for the next version
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                      // we need this to restore the revision for the next development
94                      // or release:prepare should provide sha1 after a commit
95                      // or a user should provide it as an additional `arguments` in plugin configuration
96                      // see maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module
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 }