View Javadoc
1   package org.apache.maven.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.regex.Matcher;
28  
29  import org.apache.commons.lang3.Validate;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  
32  /**
33   * ArtifactUtils
34   */
35  public final class ArtifactUtils
36  {
37  
38      public static boolean isSnapshot( String version )
39      {
40          if ( version != null )
41          {
42              if ( version.regionMatches( true, version.length() - Artifact.SNAPSHOT_VERSION.length(),
43                                          Artifact.SNAPSHOT_VERSION, 0, Artifact.SNAPSHOT_VERSION.length() ) )
44              {
45                  return true;
46              }
47              else if ( Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() )
48              {
49                  return true;
50              }
51          }
52          return false;
53      }
54  
55      public static String toSnapshotVersion( String version )
56      {
57          Validate.notBlank( version, "version can neither be null, empty nor blank" );
58  
59          Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
60          if ( m.matches() )
61          {
62              return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION;
63          }
64          else
65          {
66              return version;
67          }
68      }
69  
70      public static String versionlessKey( Artifact artifact )
71      {
72          return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
73      }
74  
75      public static String versionlessKey( String groupId, String artifactId )
76      {
77          Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
78          Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
79  
80          return groupId + ":" + artifactId;
81      }
82  
83      public static String key( Artifact artifact )
84      {
85          return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
86      }
87  
88      public static String key( String groupId, String artifactId, String version )
89      {
90          Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
91          Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
92          Validate.notBlank( version, "version can neither be null, empty nor blank" );
93  
94          return groupId + ":" + artifactId + ":" + version;
95      }
96  
97      public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts )
98      {
99          Map<String, Artifact> artifactMap = new LinkedHashMap<>();
100 
101         if ( artifacts != null )
102         {
103             for ( Artifact artifact : artifacts )
104             {
105                 artifactMap.put( versionlessKey( artifact ), artifact );
106             }
107         }
108 
109         return artifactMap;
110     }
111 
112     public static Artifact copyArtifactSafe( Artifact artifact )
113     {
114         return ( artifact != null ) ? copyArtifact( artifact ) : null;
115     }
116 
117     public static Artifact copyArtifact( Artifact artifact )
118     {
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         {
138             range = VersionRange.createFromVersion( artifact.getVersion() );
139         }
140 
141         DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
142             artifact.getScope(), artifact.getType(), artifact.getClassifier(),
143             artifact.getArtifactHandler(), artifact.isOptional() );
144         clone.setRelease( artifact.isRelease() );
145         clone.setResolvedVersion( artifact.getVersion() );
146         clone.setResolved( artifact.isResolved() );
147         clone.setFile( artifact.getFile() );
148 
149         clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
150         if ( artifact.getVersion() != null )
151         {
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     {
165         for ( Artifact artifact : from )
166         {
167             to.add( ArtifactUtils.copyArtifact( artifact ) );
168         }
169         return to;
170     }
171 
172     public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to )
173     {
174         if ( from != null )
175         {
176             for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() )
177             {
178                 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) );
179             }
180         }
181 
182         return to;
183     }
184 
185     private static <T> List<T> copyList( List<T> original )
186     {
187         List<T> copy = null;
188 
189         if ( original != null )
190         {
191             copy = new ArrayList<>();
192 
193             if ( !original.isEmpty() )
194             {
195                 copy.addAll( original );
196             }
197         }
198 
199         return copy;
200     }
201 
202 }