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.commons.lang3.Validate; 030import org.apache.maven.artifact.versioning.VersionRange; 031 032public final class ArtifactUtils 033{ 034 035 public static boolean isSnapshot( String version ) 036 { 037 if ( version != null ) 038 { 039 if ( version.regionMatches( true, version.length() - Artifact.SNAPSHOT_VERSION.length(), 040 Artifact.SNAPSHOT_VERSION, 0, Artifact.SNAPSHOT_VERSION.length() ) ) 041 { 042 return true; 043 } 044 else if ( Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() ) 045 { 046 return true; 047 } 048 } 049 return false; 050 } 051 052 public static String toSnapshotVersion( String version ) 053 { 054 Validate.notBlank( version, "version can neither be null, empty nor blank" ); 055 056 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version ); 057 if ( m.matches() ) 058 { 059 return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION; 060 } 061 else 062 { 063 return version; 064 } 065 } 066 067 public static String versionlessKey( Artifact artifact ) 068 { 069 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); 070 } 071 072 public static String versionlessKey( String groupId, String artifactId ) 073 { 074 Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" ); 075 Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" ); 076 077 return groupId + ":" + artifactId; 078 } 079 080 public static String key( Artifact artifact ) 081 { 082 return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); 083 } 084 085 public static String key( String groupId, String artifactId, String version ) 086 { 087 Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" ); 088 Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" ); 089 Validate.notBlank( version, "version can neither be null, empty nor blank" ); 090 091 return groupId + ":" + artifactId + ":" + version; 092 } 093 094 public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts ) 095 { 096 Map<String, Artifact> artifactMap = new LinkedHashMap<>(); 097 098 if ( artifacts != null ) 099 { 100 for ( Artifact artifact : artifacts ) 101 { 102 artifactMap.put( versionlessKey( artifact ), artifact ); 103 } 104 } 105 106 return artifactMap; 107 } 108 109 public static Artifact copyArtifactSafe( Artifact artifact ) 110 { 111 return ( artifact != null ) ? copyArtifact( artifact ) : null; 112 } 113 114 public static Artifact copyArtifact( Artifact artifact ) 115 { 116 VersionRange range = artifact.getVersionRange(); 117 118 // For some reason with the introduction of MNG-1577 we have the case in Yoko where a depMan section has 119 // something like the following: 120 // 121 // <dependencyManagement> 122 // <dependencies> 123 // <!-- Yoko modules --> 124 // <dependency> 125 // <groupId>org.apache.yoko</groupId> 126 // <artifactId>yoko-core</artifactId> 127 // <version>${version}</version> 128 // </dependency> 129 // ... 130 // 131 // And the range is not set so we'll check here and set it. jvz. 132 133 if ( range == null ) 134 { 135 range = VersionRange.createFromVersion( artifact.getVersion() ); 136 } 137 138 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(), 139 artifact.getScope(), artifact.getType(), artifact.getClassifier(), 140 artifact.getArtifactHandler(), artifact.isOptional() ); 141 clone.setRelease( artifact.isRelease() ); 142 clone.setResolvedVersion( artifact.getVersion() ); 143 clone.setResolved( artifact.isResolved() ); 144 clone.setFile( artifact.getFile() ); 145 146 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) ); 147 if ( artifact.getVersion() != null ) 148 { 149 clone.setBaseVersion( artifact.getBaseVersion() ); 150 } 151 clone.setDependencyFilter( artifact.getDependencyFilter() ); 152 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) ); 153 clone.setDownloadUrl( artifact.getDownloadUrl() ); 154 clone.setRepository( artifact.getRepository() ); 155 156 return clone; 157 } 158 159 /** Returns <code>to</code> collection */ 160 public static <T extends Collection<Artifact>> T copyArtifacts( Collection<Artifact> from, T to ) 161 { 162 for ( Artifact artifact : from ) 163 { 164 to.add( ArtifactUtils.copyArtifact( artifact ) ); 165 } 166 return to; 167 } 168 169 public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to ) 170 { 171 if ( from != null ) 172 { 173 for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() ) 174 { 175 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) ); 176 } 177 } 178 179 return to; 180 } 181 182 private static <T> List<T> copyList( List<T> original ) 183 { 184 List<T> copy = null; 185 186 if ( original != null ) 187 { 188 copy = new ArrayList<>(); 189 190 if ( !original.isEmpty() ) 191 { 192 copy.addAll( original ); 193 } 194 } 195 196 return copy; 197 } 198 199}