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 org.apache.maven.artifact.versioning.VersionRange;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.LinkedHashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.regex.Matcher;
31  
32  public final class ArtifactUtils
33  {
34  
35      private ArtifactUtils()
36      {
37      }
38  
39      public static boolean isSnapshot( String version )
40      {
41          return version != null &&
42              ( version.toUpperCase().endsWith( Artifact.SNAPSHOT_VERSION ) || Artifact.VERSION_FILE_PATTERN.matcher( version )
43                  .matches() );
44      }
45  
46      public static String toSnapshotVersion( String version )
47      {
48          Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
49          if ( m.matches() )
50          {
51              return m.group( 1 ) + '-' + Artifact.SNAPSHOT_VERSION;
52          }
53          else
54          {
55              return version;
56          }
57      }
58  
59      public static String versionlessKey( Artifact artifact )
60      {
61          return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
62      }
63  
64      public static String versionlessKey( String groupId, String artifactId )
65      {
66          if ( groupId == null )
67          {
68              throw new NullPointerException( "groupId was null" );
69          }
70          if ( artifactId == null )
71          {
72              throw new NullPointerException( "artifactId was null" );
73          }
74          return groupId + ":" + artifactId;
75      }
76  
77      public static String artifactId( String groupId, String artifactId, String type, String version )
78      {
79          return artifactId( groupId, artifactId, type, null, version );
80      }
81  
82      public static String artifactId( String groupId, String artifactId, String type, String classifier,
83                                       String baseVersion )
84      {
85          return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" ) + ":" +
86              baseVersion;
87      }
88  
89      public static Map artifactMapByVersionlessId( Collection artifacts )
90      {
91          Map artifactMap = new LinkedHashMap();
92  
93          if ( artifacts != null )
94          {
95              for ( Iterator it = artifacts.iterator(); it.hasNext(); )
96              {
97                  Artifact artifact = (Artifact) it.next();
98  
99                  artifactMap.put( versionlessKey( artifact ), artifact );
100             }
101         }
102 
103         return artifactMap;
104     }
105 
106     public static Map artifactMapByArtifactId( Collection artifacts )
107     {
108         Map artifactMap = new LinkedHashMap();
109 
110         if ( artifacts != null )
111         {
112             for ( Iterator it = artifacts.iterator(); it.hasNext(); )
113             {
114                 Artifact artifact = (Artifact) it.next();
115 
116                 artifactMap.put( artifact.getId(), artifact );
117             }
118         }
119 
120         return artifactMap;
121     }
122 
123     public static Artifact copyArtifact( Artifact artifact )
124     {
125         VersionRange range = artifact.getVersionRange();
126         DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
127                                                      artifact.getScope(), artifact.getType(), artifact.getClassifier(),
128                                                      artifact.getArtifactHandler(), artifact.isOptional() );
129         clone.setRelease( artifact.isRelease() );
130         clone.setResolvedVersion( artifact.getVersion() );
131         clone.setResolved( artifact.isResolved() );
132         clone.setFile( artifact.getFile() );
133 
134         clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
135         clone.setBaseVersion( artifact.getBaseVersion() );
136         clone.setDependencyFilter( artifact.getDependencyFilter() );
137         clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
138         clone.setDownloadUrl( artifact.getDownloadUrl() );
139         clone.setRepository( artifact.getRepository() );
140 
141         return clone;
142     }
143     
144     private static List copyList( List original )
145     {
146         List copy = null;
147         
148         if ( original != null )
149         {
150             copy = new ArrayList();
151             
152             if ( !original.isEmpty() )
153             {
154                 copy.addAll( original );
155             }
156         }
157         
158         return copy;
159     }
160 
161 }