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.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   * ArtifactUtils
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         // For some reason with the introduction of MNG-1577 we have the case in Yoko where a depMan section has
118         // something like the following:
119         //
120         // <dependencyManagement>
121         //     <dependencies>
122         //         <!--  Yoko modules -->
123         //         <dependency>
124         //             <groupId>org.apache.yoko</groupId>
125         //             <artifactId>yoko-core</artifactId>
126         //             <version>${version}</version>
127         //         </dependency>
128         // ...
129         //
130         // And the range is not set so we'll check here and set it. jvz.
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     /** Returns <code>to</code> collection */
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 }