| 1 |
package org.apache.maven.shared.io.location; |
| 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.List; |
| 23 |
|
| 24 |
import org.apache.maven.artifact.Artifact; |
| 25 |
import org.apache.maven.artifact.factory.ArtifactFactory; |
| 26 |
import org.apache.maven.artifact.repository.ArtifactRepository; |
| 27 |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
| 28 |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
| 29 |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
| 30 |
import org.apache.maven.shared.io.logging.MessageHolder; |
| 31 |
|
| 32 |
/** |
| 33 |
* The locator strategy. |
| 34 |
* |
| 35 |
*/ |
| 36 |
public class ArtifactLocatorStrategy |
| 37 |
implements LocatorStrategy |
| 38 |
{ |
| 39 |
private final ArtifactFactory factory; |
| 40 |
|
| 41 |
private final ArtifactResolver resolver; |
| 42 |
|
| 43 |
private String defaultArtifactType = "jar"; |
| 44 |
|
| 45 |
private final ArtifactRepository localRepository; |
| 46 |
|
| 47 |
private final List<ArtifactRepository> remoteRepositories; |
| 48 |
|
| 49 |
private String defaultClassifier = null; |
| 50 |
|
| 51 |
/** |
| 52 |
* @param factory {@link ArtifactFactory} |
| 53 |
* @param resolver {@link ArtifactResolver} |
| 54 |
* @param localRepository {@link ArtifactRepository} |
| 55 |
* @param remoteRepositories {@link ArtifactRepository} |
| 56 |
*/ |
| 57 |
public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver, |
| 58 |
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories ) |
| 59 |
{ |
| 60 |
this.factory = factory; |
| 61 |
this.resolver = resolver; |
| 62 |
this.localRepository = localRepository; |
| 63 |
this.remoteRepositories = remoteRepositories; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param factory {@link ArtifactFactory} |
| 68 |
* @param resolver {@link ArtifactResolver} |
| 69 |
* @param localRepository {@link ArtifactRepository} |
| 70 |
* @param remoteRepositories {@link ArtifactRepository} |
| 71 |
* @param defaultArtifactType default artifact type. |
| 72 |
*/ |
| 73 |
public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver, |
| 74 |
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories, |
| 75 |
String defaultArtifactType ) |
| 76 |
{ |
| 77 |
this.factory = factory; |
| 78 |
this.resolver = resolver; |
| 79 |
this.localRepository = localRepository; |
| 80 |
this.remoteRepositories = remoteRepositories; |
| 81 |
this.defaultArtifactType = defaultArtifactType; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param factory {@link ArtifactFactory} |
| 86 |
* @param resolver {@link ArtifactResolver} |
| 87 |
* @param localRepository {@link ArtifactRepository} |
| 88 |
* @param remoteRepositories {@link ArtifactRepository} |
| 89 |
* @param defaultArtifactType default artifact type. |
| 90 |
* @param defaultClassifier default classifier. |
| 91 |
*/ |
| 92 |
public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver, |
| 93 |
ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories, |
| 94 |
String defaultArtifactType, String defaultClassifier ) |
| 95 |
{ |
| 96 |
this.factory = factory; |
| 97 |
this.resolver = resolver; |
| 98 |
this.localRepository = localRepository; |
| 99 |
this.remoteRepositories = remoteRepositories; |
| 100 |
this.defaultArtifactType = defaultArtifactType; |
| 101 |
this.defaultClassifier = defaultClassifier; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Assumes artifact identity is given in a set of comma-delimited tokens of |
| 106 |
* the form: <code>groupId:artifactId:version:type:classifier</code>, where |
| 107 |
* type and classifier are optional. |
| 108 |
* @param locationSpecification location spec. |
| 109 |
* @param messageHolder {@link MessageHolder} |
| 110 |
* @return location. |
| 111 |
*/ |
| 112 |
public Location resolve( String locationSpecification, MessageHolder messageHolder ) |
| 113 |
{ |
| 114 |
String[] parts = locationSpecification.split( ":" ); |
| 115 |
|
| 116 |
Location location = null; |
| 117 |
|
| 118 |
if ( parts.length > 2 ) |
| 119 |
{ |
| 120 |
String groupId = parts[0]; |
| 121 |
String artifactId = parts[1]; |
| 122 |
String version = parts[2]; |
| 123 |
|
| 124 |
String type = defaultArtifactType; |
| 125 |
if ( parts.length > 3 ) |
| 126 |
{ |
| 127 |
if ( parts[3].trim().length() > 0 ) |
| 128 |
{ |
| 129 |
type = parts[3]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
String classifier = defaultClassifier; |
| 134 |
if ( parts.length > 4 ) |
| 135 |
{ |
| 136 |
classifier = parts[4]; |
| 137 |
} |
| 138 |
|
| 139 |
if ( parts.length > 5 ) |
| 140 |
{ |
| 141 |
messageHolder.newMessage().append( "Location specification has unused tokens: \'" ); |
| 142 |
|
| 143 |
for ( int i = 5; i < parts.length; i++ ) |
| 144 |
{ |
| 145 |
messageHolder.append( ":" + parts[i] ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
Artifact artifact; |
| 150 |
if ( classifier == null ) |
| 151 |
{ |
| 152 |
artifact = factory.createArtifact( groupId, artifactId, version, null, type ); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); |
| 157 |
} |
| 158 |
|
| 159 |
try |
| 160 |
{ |
| 161 |
resolver.resolve( artifact, remoteRepositories, localRepository ); |
| 162 |
|
| 163 |
if ( artifact.getFile() != null ) |
| 164 |
{ |
| 165 |
location = new ArtifactLocation( artifact, locationSpecification ); |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId() |
| 170 |
+ " does not have an associated file." ); |
| 171 |
} |
| 172 |
} |
| 173 |
catch ( ArtifactResolutionException e ) |
| 174 |
{ |
| 175 |
messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: " |
| 176 |
+ locationSpecification, e ); |
| 177 |
} |
| 178 |
catch ( ArtifactNotFoundException e ) |
| 179 |
{ |
| 180 |
messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: " |
| 181 |
+ locationSpecification, e ); |
| 182 |
} |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification |
| 187 |
+ "\'. Must contain at least three fields, separated by ':'." ); |
| 188 |
} |
| 189 |
|
| 190 |
return location; |
| 191 |
} |
| 192 |
|
| 193 |
} |