001package org.apache.maven.artifact; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.LinkedHashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.regex.Matcher; 028 029import org.apache.maven.artifact.versioning.VersionRange; 030 031public final class ArtifactUtils 032{ 033 034 public static boolean isSnapshot( String version ) 035 { 036 if ( version != null ) 037 { 038 if ( version.regionMatches( true, version.length() - Artifact.SNAPSHOT_VERSION.length(), 039 Artifact.SNAPSHOT_VERSION, 0, Artifact.SNAPSHOT_VERSION.length() ) ) 040 { 041 return true; 042 } 043 else if ( Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() ) 044 { 045 return true; 046 } 047 } 048 return false; 049 } 050 051 public static String toSnapshotVersion( String version ) 052 { 053 if ( version == null ) 054 { 055 throw new IllegalArgumentException( "version: null" ); 056 } 057 058 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version ); 059 if ( m.matches() ) 060 { 061 return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION; 062 } 063 else 064 { 065 return version; 066 } 067 } 068 069 public static String versionlessKey( Artifact artifact ) 070 { 071 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); 072 } 073 074 public static String versionlessKey( String groupId, String artifactId ) 075 { 076 if ( groupId == null ) 077 { 078 throw new NullPointerException( "groupId is null" ); 079 } 080 if ( artifactId == null ) 081 { 082 throw new NullPointerException( "artifactId is null" ); 083 } 084 return groupId + ":" + artifactId; 085 } 086 087 public static String key( Artifact artifact ) 088 { 089 return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); 090 } 091 092 public static String key( String groupId, String artifactId, String version ) 093 { 094 if ( groupId == null ) 095 { 096 throw new NullPointerException( "groupId is null" ); 097 } 098 if ( artifactId == null ) 099 { 100 throw new NullPointerException( "artifactId is null" ); 101 } 102 if ( version == null ) 103 { 104 throw new NullPointerException( "version is null" ); 105 } 106 107 return groupId + ":" + artifactId + ":" + version; 108 } 109 110 public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts ) 111 { 112 Map<String, Artifact> artifactMap = new LinkedHashMap<String, Artifact>(); 113 114 if ( artifacts != null ) 115 { 116 for ( Artifact artifact : artifacts ) 117 { 118 artifactMap.put( versionlessKey( artifact ), artifact ); 119 } 120 } 121 122 return artifactMap; 123 } 124 125 public static Artifact copyArtifactSafe( Artifact artifact ) 126 { 127 return ( artifact != null ) ? copyArtifact( artifact ) : null; 128 } 129 130 public static Artifact copyArtifact( Artifact artifact ) 131 { 132 VersionRange range = artifact.getVersionRange(); 133 134 // For some reason with the introduction of MNG-1577 we have the case in Yoko where a depMan section has 135 // something like the following: 136 // 137 // <dependencyManagement> 138 // <dependencies> 139 // <!-- Yoko modules --> 140 // <dependency> 141 // <groupId>org.apache.yoko</groupId> 142 // <artifactId>yoko-core</artifactId> 143 // <version>${version}</version> 144 // </dependency> 145 // ... 146 // 147 // And the range is not set so we'll check here and set it. jvz. 148 149 if ( range == null ) 150 { 151 range = VersionRange.createFromVersion( artifact.getVersion() ); 152 } 153 154 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(), 155 artifact.getScope(), artifact.getType(), artifact.getClassifier(), 156 artifact.getArtifactHandler(), artifact.isOptional() ); 157 clone.setRelease( artifact.isRelease() ); 158 clone.setResolvedVersion( artifact.getVersion() ); 159 clone.setResolved( artifact.isResolved() ); 160 clone.setFile( artifact.getFile() ); 161 162 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) ); 163 if ( artifact.getVersion() != null ) 164 { 165 clone.setBaseVersion( artifact.getBaseVersion() ); 166 } 167 clone.setDependencyFilter( artifact.getDependencyFilter() ); 168 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) ); 169 clone.setDownloadUrl( artifact.getDownloadUrl() ); 170 clone.setRepository( artifact.getRepository() ); 171 172 return clone; 173 } 174 175 /** Returns <code>to</code> collection */ 176 public static <T extends Collection<Artifact>> T copyArtifacts( Collection<Artifact> from, T to ) 177 { 178 for ( Artifact artifact : from ) 179 { 180 to.add( ArtifactUtils.copyArtifact( artifact ) ); 181 } 182 return to; 183 } 184 185 public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to ) 186 { 187 if ( from != null ) 188 { 189 for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() ) 190 { 191 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) ); 192 } 193 } 194 195 return to; 196 } 197 198 private static <T> List<T> copyList( List<T> original ) 199 { 200 List<T> copy = null; 201 202 if ( original != null ) 203 { 204 copy = new ArrayList<T>(); 205 206 if ( !original.isEmpty() ) 207 { 208 copy.addAll( original ); 209 } 210 } 211 212 return copy; 213 } 214 215}