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.buildcache.checksum;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.maven.buildcache.xml.build.Artifact;
23  import org.apache.maven.project.MavenProject;
24  
25  /**
26   * KeyUtils
27   */
28  public class KeyUtils {
29  
30      private static final String SEPARATOR = ":";
31  
32      public static String getProjectKey(MavenProject project) {
33          return getProjectKey(project.getGroupId(), project.getArtifactId(), project.getVersion());
34      }
35  
36      public static String getProjectKey(String groupId, String artifactId, String version) {
37          return StringUtils.joinWith(SEPARATOR, groupId, artifactId, version);
38      }
39  
40      public static String getVersionlessProjectKey(MavenProject project) {
41          return StringUtils.joinWith(SEPARATOR, project.getGroupId(), project.getArtifactId());
42      }
43  
44      public static String getArtifactKey(Artifact artifact) {
45          return getArtifactKey(
46                  artifact.getGroupId(),
47                  artifact.getArtifactId(),
48                  artifact.getType(),
49                  artifact.getClassifier(),
50                  artifact.getVersion());
51      }
52  
53      public static String getVersionlessArtifactKey(org.apache.maven.artifact.Artifact artifact) {
54          return getVersionlessArtifactKey(
55                  artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), artifact.getClassifier());
56      }
57  
58      /**
59       * Computes the key for the given artifact, using the given type instead of the one defined in the artifact.
60       */
61      public static String getArtifactKey(Artifact artifact, String type) {
62          return getArtifactKey(
63                  artifact.getGroupId(), artifact.getArtifactId(), type, artifact.getClassifier(), artifact.getVersion());
64      }
65  
66      public static String getArtifactKey(org.apache.maven.artifact.Artifact artifact) {
67          return getArtifactKey(
68                  artifact.getGroupId(),
69                  artifact.getArtifactId(),
70                  artifact.getType(),
71                  artifact.getClassifier(),
72                  artifact.getVersion());
73      }
74  
75      public static String getArtifactKey(
76              String groupId, String artifactId, String type, String classifier, String version) {
77          if (classifier != null) {
78              return StringUtils.joinWith(SEPARATOR, groupId, artifactId, type, classifier, version);
79          } else {
80              return StringUtils.joinWith(SEPARATOR, groupId, artifactId, type, version);
81          }
82      }
83  
84      public static String getVersionlessArtifactKey(String groupId, String artifactId, String type, String classifier) {
85          if (classifier != null) {
86              return StringUtils.joinWith(SEPARATOR, groupId, artifactId, type, classifier);
87          } else {
88              return StringUtils.joinWith(SEPARATOR, groupId, artifactId, type);
89          }
90      }
91  }