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.maven.artifact.versioning.VersionRange;
29  
30  /**
31   * ArtifactUtils
32   */
33  public final class ArtifactUtils {
34  
35      public static boolean isSnapshot(String version) {
36          if (version != null) {
37              if (version.regionMatches(
38                      true,
39                      version.length() - Artifact.SNAPSHOT_VERSION.length(),
40                      Artifact.SNAPSHOT_VERSION,
41                      0,
42                      Artifact.SNAPSHOT_VERSION.length())) {
43                  return true;
44              } else {
45                  return Artifact.VERSION_FILE_PATTERN.matcher(version).matches();
46              }
47          }
48          return false;
49      }
50  
51      public static String toSnapshotVersion(String version) {
52          notBlank(version, "version can neither be null, empty nor blank");
53  
54          int lastHyphen = version.lastIndexOf('-');
55          if (lastHyphen > 0) {
56              int prevHyphen = version.lastIndexOf('-', lastHyphen - 1);
57              if (prevHyphen > 0) {
58                  Matcher m = Artifact.VERSION_FILE_PATTERN.matcher(version);
59                  if (m.matches()) {
60                      return m.group(1) + "-" + Artifact.SNAPSHOT_VERSION;
61                  }
62              }
63          }
64          return version;
65      }
66  
67      public static String versionlessKey(Artifact artifact) {
68          return versionlessKey(artifact.getGroupId(), artifact.getArtifactId());
69      }
70  
71      public static String versionlessKey(String groupId, String artifactId) {
72          notBlank(groupId, "groupId can neither be null, empty nor blank");
73          notBlank(artifactId, "artifactId can neither be null, empty nor blank");
74  
75          return groupId + ":" + artifactId;
76      }
77  
78      public static String key(Artifact artifact) {
79          return key(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
80      }
81  
82      public static String key(String groupId, String artifactId, String version) {
83          notBlank(groupId, "groupId can neither be null, empty nor blank");
84          notBlank(artifactId, "artifactId can neither be null, empty nor blank");
85          notBlank(version, "version can neither be null, empty nor blank");
86  
87          return groupId + ":" + artifactId + ":" + version;
88      }
89  
90      private static void notBlank(String str, String message) {
91          final int strLen = str != null ? str.length() : 0;
92          if (strLen > 0) {
93              for (int i = 0; i < strLen; i++) {
94                  if (!Character.isWhitespace(str.charAt(i))) {
95                      return;
96                  }
97              }
98          }
99          throw new IllegalArgumentException(message);
100     }
101 
102     public static Map<String, Artifact> artifactMapByVersionlessId(Collection<Artifact> artifacts) {
103         Map<String, Artifact> artifactMap = new LinkedHashMap<>();
104 
105         if (artifacts != null) {
106             for (Artifact artifact : artifacts) {
107                 artifactMap.put(versionlessKey(artifact), artifact);
108             }
109         }
110 
111         return artifactMap;
112     }
113 
114     public static Artifact copyArtifactSafe(Artifact artifact) {
115         return (artifact != null) ? copyArtifact(artifact) : null;
116     }
117 
118     public static Artifact copyArtifact(Artifact artifact) {
119         VersionRange range = artifact.getVersionRange();
120 
121         // For some reason with the introduction of MNG-1577 we have the case in Yoko where a depMan section has
122         // something like the following:
123         //
124         // <dependencyManagement>
125         //     <dependencies>
126         //         <!--  Yoko modules -->
127         //         <dependency>
128         //             <groupId>org.apache.yoko</groupId>
129         //             <artifactId>yoko-core</artifactId>
130         //             <version>${version}</version>
131         //         </dependency>
132         // ...
133         //
134         // And the range is not set so we'll check here and set it. jvz.
135 
136         if (range == null) {
137             range = VersionRange.createFromVersion(artifact.getVersion());
138         }
139 
140         DefaultArtifact clone = new DefaultArtifact(
141                 artifact.getGroupId(),
142                 artifact.getArtifactId(),
143                 range,
144                 artifact.getScope(),
145                 artifact.getType(),
146                 artifact.getClassifier(),
147                 artifact.getArtifactHandler(),
148                 artifact.isOptional());
149         clone.setRelease(artifact.isRelease());
150         clone.setResolvedVersion(artifact.getVersion());
151         clone.setResolved(artifact.isResolved());
152         clone.setFile(artifact.getFile());
153 
154         clone.setAvailableVersions(copyList(artifact.getAvailableVersions()));
155         if (artifact.getVersion() != null) {
156             clone.setBaseVersion(artifact.getBaseVersion());
157         }
158         clone.setDependencyFilter(artifact.getDependencyFilter());
159         clone.setDependencyTrail(copyList(artifact.getDependencyTrail()));
160         clone.setDownloadUrl(artifact.getDownloadUrl());
161         clone.setRepository(artifact.getRepository());
162 
163         return clone;
164     }
165 
166     /**
167      * Copy artifact to a collection.
168      *
169      * @param <T> the target collection type
170      * @param from an artifact collection
171      * @param to the target artifact collection
172      * @return <code>to</code> collection
173      */
174     public static <T extends Collection<Artifact>> T copyArtifacts(Collection<Artifact> from, T to) {
175         for (Artifact artifact : from) {
176             to.add(ArtifactUtils.copyArtifact(artifact));
177         }
178         return to;
179     }
180 
181     public static <K, T extends Map<K, Artifact>> T copyArtifacts(Map<K, ? extends Artifact> from, T to) {
182         if (from != null) {
183             for (Map.Entry<K, ? extends Artifact> entry : from.entrySet()) {
184                 to.put(entry.getKey(), ArtifactUtils.copyArtifact(entry.getValue()));
185             }
186         }
187 
188         return to;
189     }
190 
191     private static <T> List<T> copyList(List<T> original) {
192         List<T> copy = null;
193 
194         if (original != null) {
195             copy = new ArrayList<>();
196 
197             if (!original.isEmpty()) {
198                 copy.addAll(original);
199             }
200         }
201 
202         return copy;
203     }
204 }