1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.artifact;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Matcher;
27  
28  import org.apache.commons.lang3.Validate;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  
31  
32  
33  
34  public final class ArtifactUtils {
35  
36      public static boolean isSnapshot(String version) {
37          if (version != null) {
38              if (version.regionMatches(
39                      true,
40                      version.length() - Artifact.SNAPSHOT_VERSION.length(),
41                      Artifact.SNAPSHOT_VERSION,
42                      0,
43                      Artifact.SNAPSHOT_VERSION.length())) {
44                  return true;
45              } else if (Artifact.VERSION_FILE_PATTERN.matcher(version).matches()) {
46                  return true;
47              }
48          }
49          return false;
50      }
51  
52      public static String toSnapshotVersion(String version) {
53          notBlank(version, "version can neither be null, empty nor blank");
54  
55          int lastHyphen = version.lastIndexOf('-');
56          if (lastHyphen > 0) {
57              int prevHyphen = version.lastIndexOf('-', lastHyphen - 1);
58              if (prevHyphen > 0) {
59                  Matcher m = Artifact.VERSION_FILE_PATTERN.matcher(version);
60                  if (m.matches()) {
61                      return m.group(1) + "-" + Artifact.SNAPSHOT_VERSION;
62                  }
63              }
64          }
65          return version;
66      }
67  
68      public static String versionlessKey(Artifact artifact) {
69          return versionlessKey(artifact.getGroupId(), artifact.getArtifactId());
70      }
71  
72      public static String versionlessKey(String groupId, String artifactId) {
73          notBlank(groupId, "groupId can neither be null, empty nor blank");
74          notBlank(artifactId, "artifactId can neither be null, empty nor blank");
75  
76          return groupId + ":" + artifactId;
77      }
78  
79      public static String key(Artifact artifact) {
80          return key(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
81      }
82  
83      public static String key(String groupId, String artifactId, String version) {
84          notBlank(groupId, "groupId can neither be null, empty nor blank");
85          notBlank(artifactId, "artifactId can neither be null, empty nor blank");
86          notBlank(version, "version can neither be null, empty nor blank");
87  
88          return groupId + ":" + artifactId + ":" + version;
89      }
90  
91      private static void notBlank(String str, String message) {
92          int c = str != null && str.length() > 0 ? str.charAt(0) : 0;
93          if ((c < '0' || c > '9') && (c < 'a' || c > 'z')) {
94              Validate.notBlank(str, message);
95          }
96      }
97  
98      public static Map<String, Artifact> artifactMapByVersionlessId(Collection<Artifact> artifacts) {
99          Map<String, Artifact> artifactMap = new LinkedHashMap<>();
100 
101         if (artifacts != null) {
102             for (Artifact artifact : artifacts) {
103                 artifactMap.put(versionlessKey(artifact), artifact);
104             }
105         }
106 
107         return artifactMap;
108     }
109 
110     public static Artifact copyArtifactSafe(Artifact artifact) {
111         return (artifact != null) ? copyArtifact(artifact) : null;
112     }
113 
114     public static Artifact copyArtifact(Artifact artifact) {
115         VersionRange range = artifact.getVersionRange();
116 
117         
118         
119         
120         
121         
122         
123         
124         
125         
126         
127         
128         
129         
130         
131 
132         if (range == null) {
133             range = VersionRange.createFromVersion(artifact.getVersion());
134         }
135 
136         DefaultArtifact clone = new DefaultArtifact(
137                 artifact.getGroupId(),
138                 artifact.getArtifactId(),
139                 range,
140                 artifact.getScope(),
141                 artifact.getType(),
142                 artifact.getClassifier(),
143                 artifact.getArtifactHandler(),
144                 artifact.isOptional());
145         clone.setRelease(artifact.isRelease());
146         clone.setResolvedVersion(artifact.getVersion());
147         clone.setResolved(artifact.isResolved());
148         clone.setFile(artifact.getFile());
149 
150         clone.setAvailableVersions(copyList(artifact.getAvailableVersions()));
151         if (artifact.getVersion() != null) {
152             clone.setBaseVersion(artifact.getBaseVersion());
153         }
154         clone.setDependencyFilter(artifact.getDependencyFilter());
155         clone.setDependencyTrail(copyList(artifact.getDependencyTrail()));
156         clone.setDownloadUrl(artifact.getDownloadUrl());
157         clone.setRepository(artifact.getRepository());
158 
159         return clone;
160     }
161 
162     
163     public static <T extends Collection<Artifact>> T copyArtifacts(Collection<Artifact> from, T to) {
164         for (Artifact artifact : from) {
165             to.add(ArtifactUtils.copyArtifact(artifact));
166         }
167         return to;
168     }
169 
170     public static <K, T extends Map<K, Artifact>> T copyArtifacts(Map<K, ? extends Artifact> from, T to) {
171         if (from != null) {
172             for (Map.Entry<K, ? extends Artifact> entry : from.entrySet()) {
173                 to.put(entry.getKey(), ArtifactUtils.copyArtifact(entry.getValue()));
174             }
175         }
176 
177         return to;
178     }
179 
180     private static <T> List<T> copyList(List<T> original) {
181         List<T> copy = null;
182 
183         if (original != null) {
184             copy = new ArrayList<>();
185 
186             if (!original.isEmpty()) {
187                 copy.addAll(original);
188             }
189         }
190 
191         return copy;
192     }
193 }