| 1 |
/* |
| 2 |
* Capsule |
| 3 |
* Copyright (c) 2014-2016, Parallel Universe Software Co. All rights reserved. |
| 4 |
* |
| 5 |
* This program and the accompanying materials are licensed under the terms |
| 6 |
* of the Eclipse Public License v1.0, available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
*/ |
| 9 |
package capsule; |
| 10 |
|
| 11 |
import java.io.PrintStream; |
| 12 |
import java.nio.file.Path; |
| 13 |
import java.nio.file.Paths; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Arrays; |
| 16 |
import java.util.Collection; |
| 17 |
import java.util.Collections; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.List; |
| 20 |
import java.util.Map; |
| 21 |
import java.util.Objects; |
| 22 |
import java.util.regex.Matcher; |
| 23 |
import java.util.regex.Pattern; |
| 24 |
|
| 25 |
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; |
| 26 |
import org.eclipse.aether.AbstractForwardingRepositorySystemSession; |
| 27 |
import org.eclipse.aether.ConfigurationProperties; |
| 28 |
import org.eclipse.aether.DefaultRepositorySystemSession; |
| 29 |
import org.eclipse.aether.RepositoryException; |
| 30 |
import org.eclipse.aether.RepositorySystem; |
| 31 |
import org.eclipse.aether.RepositorySystemSession; |
| 32 |
import org.eclipse.aether.artifact.Artifact; |
| 33 |
import org.eclipse.aether.artifact.DefaultArtifact; |
| 34 |
import org.eclipse.aether.collection.CollectRequest; |
| 35 |
import org.eclipse.aether.collection.CollectResult; |
| 36 |
import org.eclipse.aether.collection.DependencyCollectionException; |
| 37 |
import org.eclipse.aether.graph.Dependency; |
| 38 |
import org.eclipse.aether.graph.DependencyFilter; |
| 39 |
import org.eclipse.aether.graph.DependencyNode; |
| 40 |
import org.eclipse.aether.graph.DependencyVisitor; |
| 41 |
import org.eclipse.aether.graph.Exclusion; |
| 42 |
import org.eclipse.aether.impl.DefaultServiceLocator; |
| 43 |
import org.eclipse.aether.repository.LocalRepository; |
| 44 |
import org.eclipse.aether.repository.MirrorSelector; |
| 45 |
import org.eclipse.aether.repository.Proxy; |
| 46 |
import org.eclipse.aether.repository.ProxySelector; |
| 47 |
import org.eclipse.aether.repository.RemoteRepository; |
| 48 |
import org.eclipse.aether.repository.RepositoryPolicy; |
| 49 |
import org.eclipse.aether.resolution.ArtifactResult; |
| 50 |
import org.eclipse.aether.resolution.DependencyRequest; |
| 51 |
import org.eclipse.aether.resolution.DependencyResolutionException; |
| 52 |
import org.eclipse.aether.resolution.DependencyResult; |
| 53 |
import org.eclipse.aether.resolution.VersionRangeRequest; |
| 54 |
import org.eclipse.aether.resolution.VersionRangeResult; |
| 55 |
import org.eclipse.aether.resolution.VersionRequest; |
| 56 |
import org.eclipse.aether.resolution.VersionResult; |
| 57 |
import org.eclipse.aether.util.artifact.JavaScopes; |
| 58 |
import org.eclipse.aether.util.graph.transformer.ConflictResolver; |
| 59 |
import org.eclipse.aether.version.Version; |
| 60 |
import static java.util.Collections.unmodifiableMap; |
| 61 |
|
| 62 |
/** |
| 63 |
* Uses Aether as the Maven dependency manager. |
| 64 |
*/ |
| 65 |
public class DependencyManager { |
| 66 |
/* |
| 67 |
* see http://git.eclipse.org/c/aether/aether-ant.git/tree/src/main/java/org/eclipse/aether/internal/ant/AntRepoSys.java |
| 68 |
*/ |
| 69 |
//<editor-fold desc="Constants"> |
| 70 |
/////////// Constants /////////////////////////////////// |
| 71 |
private static final String PROP_OFFLINE = "capsule.offline"; |
| 72 |
private static final String PROP_CONNECT_TIMEOUT = "capsule.connect.timeout"; |
| 73 |
private static final String PROP_REQUEST_TIMEOUT = "capsule.request.timeout"; |
| 74 |
private static final String PROP_USER_HOME = "user.home"; |
| 75 |
|
| 76 |
private static final String ENV_CONNECT_TIMEOUT = "CAPSULE_CONNECT_TIMEOUT"; |
| 77 |
private static final String ENV_REQUEST_TIMEOUT = "CAPSULE_REQUEST_TIMEOUT"; |
| 78 |
|
| 79 |
static final Path DEFAULT_LOCAL_MAVEN = Paths.get(System.getProperty(PROP_USER_HOME), ".m2"); |
| 80 |
|
| 81 |
private static final String LATEST_VERSION = "[0,)"; |
| 82 |
private static final int LOG_NONE = 0; |
| 83 |
/** @noinspection unused*/ |
| 84 |
private static final int LOG_QUIET = 1; |
| 85 |
private static final int LOG_VERBOSE = 2; |
| 86 |
private static final int LOG_DEBUG = 3; |
| 87 |
private static final String LOG_PREFIX = "CAPSULE: "; |
| 88 |
|
| 89 |
private static final UserSettings MVN_SETTINGS; |
| 90 |
static { |
| 91 |
try { |
| 92 |
MVN_SETTINGS = UserSettings.getInstance(); |
| 93 |
} catch (final Throwable t) { |
| 94 |
t.printStackTrace(); |
| 95 |
throw t; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static final Map<String, String> WELL_KNOWN_REPOS = unmodifiableMap(new HashMap<String, String>() { |
| 100 |
{ |
| 101 |
put("central", "central(https://repo1.maven.org/maven2/)"); |
| 102 |
put("central-http", "central(http://repo1.maven.org/maven2/)"); |
| 103 |
put("jcenter", "jcenter(https://jcenter.bintray.com/)"); |
| 104 |
put("jcenter-http", "jcenter(http://jcenter.bintray.com/)"); |
| 105 |
put("local", "local(file:" + MVN_SETTINGS.getRepositoryHome() + ")"); |
| 106 |
} |
| 107 |
}); |
| 108 |
//</editor-fold> |
| 109 |
|
| 110 |
private final boolean forceRefresh; |
| 111 |
private final boolean offline; |
| 112 |
protected final RepositorySystem system; |
| 113 |
private final LocalRepository localRepo; |
| 114 |
private RepositorySystemSession session; |
| 115 |
private List<RemoteRepository> repos; |
| 116 |
private final int logLevel; |
| 117 |
|
| 118 |
//<editor-fold desc="Construction and Setup"> |
| 119 |
/////////// Construction and Setup /////////////////////////////////// |
| 120 |
public DependencyManager(Path localRepoPath, boolean forceRefresh, int logLevel) { |
| 121 |
this.logLevel = logLevel; |
| 122 |
this.forceRefresh = forceRefresh; |
| 123 |
this.offline = isPropertySet(PROP_OFFLINE, false); |
| 124 |
if (localRepoPath == null) |
| 125 |
localRepoPath = MVN_SETTINGS.getRepositoryHome(); |
| 126 |
|
| 127 |
log(LOG_DEBUG, "DependencyManager - Offline: " + offline); |
| 128 |
log(LOG_DEBUG, "DependencyManager - Local repo: " + localRepoPath); |
| 129 |
|
| 130 |
this.localRepo = new LocalRepository(localRepoPath.toFile()); |
| 131 |
this.system = newRepositorySystem(); |
| 132 |
} |
| 133 |
|
| 134 |
public final void setRepos(List<String> repos, boolean allowSnapshots) { |
| 135 |
if (repos == null) |
| 136 |
//noinspection ArraysAsListWithZeroOrOneArgument |
| 137 |
repos = Arrays.asList("central"); |
| 138 |
|
| 139 |
final List<RemoteRepository> rs = new ArrayList<>(); |
| 140 |
for (final String r : repos) { |
| 141 |
RepositoryPolicy releasePolicy = makeReleasePolicy(r); |
| 142 |
RepositoryPolicy snapshotPolicy = allowSnapshots ? makeSnapshotPolicy(r) : new RepositoryPolicy(false, null, null); |
| 143 |
|
| 144 |
RemoteRepository repo = createRepo(r, releasePolicy, snapshotPolicy); |
| 145 |
MirrorSelector mirrorSelector = getSession().getMirrorSelector(); |
| 146 |
if( mirrorSelector!=null ) { |
| 147 |
RemoteRepository mirror = mirrorSelector.getMirror(repo); |
| 148 |
if( mirror != null ) { |
| 149 |
log(LOG_QUIET, "Using mirror for repository " + repo.getId() + " => " + mirror.getUrl()); |
| 150 |
repo = mirror; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
ProxySelector selector = getSession().getProxySelector(); |
| 155 |
Proxy proxy = selector.getProxy(repo); |
| 156 |
if (proxy != null) { |
| 157 |
if (isLogging(LOG_DEBUG)) |
| 158 |
log(LOG_DEBUG, String.format("Setting proxy: '%s' for dependency: %s", proxy, repo)); |
| 159 |
repo = new RemoteRepository.Builder(repo).setProxy(proxy).build(); |
| 160 |
} |
| 161 |
|
| 162 |
if (!rs.contains(repo)) |
| 163 |
rs.add(repo); |
| 164 |
} |
| 165 |
|
| 166 |
if (!Objects.equals(this.repos, rs)) { |
| 167 |
this.repos = rs; |
| 168 |
log(LOG_VERBOSE, "Dependency manager repositories: " + this.repos); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** @noinspection UnusedParameters*/ |
| 173 |
protected RepositoryPolicy makeReleasePolicy(String repo) { |
| 174 |
return new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_WARN); |
| 175 |
} |
| 176 |
|
| 177 |
protected RepositoryPolicy makeSnapshotPolicy(String repo) { |
| 178 |
return makeReleasePolicy(repo); |
| 179 |
} |
| 180 |
|
| 181 |
private static RepositorySystem newRepositorySystem() { |
| 182 |
/* |
| 183 |
* We're using DefaultServiceLocator rather than Guice/Sisu because it's more lightweight. |
| 184 |
* This method pulls together the necessary Aether components and plugins. |
| 185 |
*/ |
| 186 |
final DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); |
| 187 |
locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() { |
| 188 |
@Override |
| 189 |
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable ex) { |
| 190 |
throw new RuntimeException("Service creation failed for type " + type.getName() + " with impl " + impl, ex); |
| 191 |
} |
| 192 |
}); |
| 193 |
|
| 194 |
locator.addService(org.eclipse.aether.spi.connector.RepositoryConnectorFactory.class, org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory.class); |
| 195 |
locator.addService(org.eclipse.aether.spi.connector.transport.TransporterFactory.class, org.eclipse.aether.transport.http.HttpTransporterFactory.class); |
| 196 |
locator.addService(org.eclipse.aether.spi.connector.transport.TransporterFactory.class, org.eclipse.aether.transport.file.FileTransporterFactory.class); |
| 197 |
|
| 198 |
// Takari (support concurrent downloads) |
| 199 |
locator.setService(org.eclipse.aether.impl.SyncContextFactory.class, LockingSyncContextFactory.class); |
| 200 |
locator.setService(org.eclipse.aether.spi.io.FileProcessor.class, LockingFileProcessor.class); |
| 201 |
|
| 202 |
return locator.getService(RepositorySystem.class); |
| 203 |
} |
| 204 |
|
| 205 |
public RepositorySystemSession getSession() { |
| 206 |
if (session == null) |
| 207 |
session = newRepositorySession(system, localRepo); |
| 208 |
return session; |
| 209 |
} |
| 210 |
|
| 211 |
void setSession( RepositorySystemSession session ) { |
| 212 |
this.session = session; |
| 213 |
} |
| 214 |
|
| 215 |
protected RepositorySystemSession newRepositorySession(RepositorySystem system, LocalRepository localRepo) { |
| 216 |
final DefaultRepositorySystemSession s = MavenRepositorySystemUtils.newSession(); |
| 217 |
|
| 218 |
s.setConfigProperty(ConfigurationProperties.CONNECT_TIMEOUT, propertyOrEnv(PROP_CONNECT_TIMEOUT, ENV_CONNECT_TIMEOUT)); |
| 219 |
s.setConfigProperty(ConfigurationProperties.REQUEST_TIMEOUT, propertyOrEnv(PROP_REQUEST_TIMEOUT, ENV_REQUEST_TIMEOUT)); |
| 220 |
// WARN: `ConflictResolver.CONFIG_PROP_VERBOSE` will retain (and mark) dependency graph duplicates such as conflict resolution losers since |
| 221 |
// http://git.eclipse.org/c/aether/aether-core.git/diff/aether-util/src/main/java/org/eclipse/aether/util/graph/transformer/ConflictResolver.java?id=141a3669d23ab67846b0c3ccef14eb0cdc70cee9 |
| 222 |
s.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true); |
| 223 |
|
| 224 |
s.setOffline(offline); |
| 225 |
s.setUpdatePolicy(forceRefresh ? RepositoryPolicy.UPDATE_POLICY_ALWAYS : RepositoryPolicy.UPDATE_POLICY_NEVER); |
| 226 |
|
| 227 |
s.setLocalRepositoryManager(system.newLocalRepositoryManager(s, localRepo)); |
| 228 |
|
| 229 |
SystemProxySelector sysProxySelector = new SystemProxySelector(logLevel); |
| 230 |
s.setProxySelector(sysProxySelector.isValid() ? sysProxySelector : MVN_SETTINGS.getProxySelector()); |
| 231 |
s.setMirrorSelector(MVN_SETTINGS.getMirrorSelector()); |
| 232 |
s.setAuthenticationSelector(MVN_SETTINGS.getAuthSelector()); |
| 233 |
|
| 234 |
s.setDependencyGraphTransformer(newConflictResolver()); |
| 235 |
|
| 236 |
if (logLevel > LOG_NONE) { |
| 237 |
final PrintStream out = prefixStream(System.err, LOG_PREFIX); |
| 238 |
s.setTransferListener(new ConsoleTransferListener(isLogging(LOG_VERBOSE), out)); |
| 239 |
s.setRepositoryListener(new ConsoleRepositoryListener(isLogging(LOG_VERBOSE), out)); |
| 240 |
} |
| 241 |
|
| 242 |
return s; |
| 243 |
} |
| 244 |
|
| 245 |
private static ConflictResolver newConflictResolver() { |
| 246 |
return new ConflictResolver ( |
| 247 |
new org.eclipse.aether.util.graph.transformer.NearestVersionSelector(), |
| 248 |
new org.eclipse.aether.util.graph.transformer.JavaScopeSelector(), |
| 249 |
new org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector(), |
| 250 |
new org.eclipse.aether.util.graph.transformer.JavaScopeDeriver() |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
/** @noinspection unused*/ |
| 255 |
public void setSystemProperties(Map<String, String> properties) { |
| 256 |
final Map<String, String> ps = Collections.unmodifiableMap(properties); |
| 257 |
if (getSession() instanceof DefaultRepositorySystemSession) |
| 258 |
((DefaultRepositorySystemSession) getSession()).setSystemProperties(ps); |
| 259 |
else { |
| 260 |
final RepositorySystemSession s = getSession(); |
| 261 |
this.session = new AbstractForwardingRepositorySystemSession() { |
| 262 |
@Override |
| 263 |
protected RepositorySystemSession getSession() { |
| 264 |
return s; |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public Map<String, String> getSystemProperties() { |
| 269 |
return ps; |
| 270 |
} |
| 271 |
}; |
| 272 |
} |
| 273 |
} |
| 274 |
//</editor-fold> |
| 275 |
|
| 276 |
//<editor-fold defaultstate="collapsed" desc="Operations"> |
| 277 |
/////////// Operations /////////////////////////////////// |
| 278 |
private CollectRequest collect() { |
| 279 |
return new CollectRequest().setRepositories(repos); |
| 280 |
} |
| 281 |
|
| 282 |
public final void printDependencyTree(List<String> coords, String type, PrintStream out) { |
| 283 |
printDependencyTree(toDependencies(coords, type), out); |
| 284 |
} |
| 285 |
|
| 286 |
public final void printDependencyTree(List<Dependency> deps, PrintStream out) { |
| 287 |
printDependencyTree(collect().setDependencies(deps), out); |
| 288 |
} |
| 289 |
|
| 290 |
public final void printDependencyTree(String coords, String type, PrintStream out) { |
| 291 |
printDependencyTree(collect().setRoot(toDependency(coords, type)), out); |
| 292 |
} |
| 293 |
|
| 294 |
private void printDependencyTree(CollectRequest collectRequest, PrintStream out) { |
| 295 |
try { |
| 296 |
CollectResult collectResult = system.collectDependencies(getSession(), collectRequest); |
| 297 |
collectResult.getRoot().accept(new ConsoleDependencyGraphDumper(out)); |
| 298 |
} catch (DependencyCollectionException e) { |
| 299 |
throw new RuntimeException(e); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
public final List<Path> resolveDependencies(List<String> coords, String type) { |
| 304 |
return resolve(collect().setDependencies(toDependencies(coords, type))); |
| 305 |
} |
| 306 |
|
| 307 |
public final List<Path> resolveDependency(String coords, String type) { |
| 308 |
return resolve(collect().setRoot(toDependency(coords, type))); // resolveDependencies(Collections.singletonList(coords), type); |
| 309 |
} |
| 310 |
|
| 311 |
public final Map<Dependency, List<Path>> resolveDependencies(List<Dependency> deps) { |
| 312 |
final Map<Dependency, List<Path>> resolved = new HashMap<>(); |
| 313 |
|
| 314 |
final List<DependencyNode> children = resolve0(collect().setDependencies(deps)).getRoot().getChildren(); |
| 315 |
// if (children.size() != deps.size()) |
| 316 |
// throw new AssertionError("deps: " + deps + " -> " + children); |
| 317 |
|
| 318 |
for (DependencyNode dn : children) { |
| 319 |
final List<Path> jars = new ArrayList<>(); |
| 320 |
|
| 321 |
// if(!equalExceptVersion(deps.get(i).getArtifact(), dn.getArtifact())) |
| 322 |
// throw new AssertionError(); |
| 323 |
resolved.put(clean(dn.getDependency()), jars); |
| 324 |
dn.accept(new DependencyVisitor() { |
| 325 |
@Override |
| 326 |
public final boolean visitEnter(DependencyNode node) { |
| 327 |
if (hasArtifactPath(node)) // Conflict losers won't have it |
| 328 |
jars.add(path(node.getArtifact())); |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public final boolean visitLeave(DependencyNode node) { |
| 334 |
return true; |
| 335 |
} |
| 336 |
}); |
| 337 |
} |
| 338 |
return resolved; |
| 339 |
} |
| 340 |
|
| 341 |
private Dependency clean(Dependency d) { |
| 342 |
// necessary for dependency equality |
| 343 |
// SNAPSHOT dependencies get resolved to specific artifacts, so returned dependency is different from original |
| 344 |
final Artifact a = d.getArtifact(); |
| 345 |
return new Dependency(new DefaultArtifact(a.getGroupId(), a.getArtifactId(), a.getClassifier(), a.getExtension(), a.getBaseVersion()), |
| 346 |
d.getScope(), d.getOptional(), d.getExclusions()); |
| 347 |
} |
| 348 |
|
| 349 |
private boolean equalExceptVersion(Artifact a, Artifact b) { |
| 350 |
return Objects.equals(a.getGroupId(), b.getGroupId()) |
| 351 |
&& Objects.equals(a.getArtifactId(), b.getArtifactId()) |
| 352 |
&& Objects.equals(a.getExtension(), b.getExtension()) |
| 353 |
&& Objects.equals(a.getClassifier(), b.getClassifier()) |
| 354 |
&& Objects.equals(a.getBaseVersion(), b.getBaseVersion()); |
| 355 |
} |
| 356 |
|
| 357 |
protected List<Path> resolve(CollectRequest collectRequest) { |
| 358 |
final List<Path> jars = new ArrayList<>(); |
| 359 |
for (ArtifactResult artifactResult : resolve0(collectRequest).getArtifactResults()) |
| 360 |
jars.add(path(artifactResult.getArtifact())); |
| 361 |
return jars; |
| 362 |
} |
| 363 |
|
| 364 |
protected DependencyResult resolve0(CollectRequest collectRequest) { |
| 365 |
if (isLogging(LOG_DEBUG)) |
| 366 |
log(LOG_DEBUG, "DependencyManager.resolve " + collectRequest); |
| 367 |
try { |
| 368 |
final DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null); |
| 369 |
dependencyRequest.setFilter(new DependencyFilter() { |
| 370 |
@Override |
| 371 |
public boolean accept(DependencyNode n, List<DependencyNode> parents) { |
| 372 |
// Exclude conflict losers (which are marked but left in if verbose mode is enabled, see |
| 373 |
// http://git.eclipse.org/c/aether/aether-core.git/diff/aether-util/src/main/java/org/eclipse/aether/util/graph/transformer/ConflictResolver.java?id=141a3669d23ab67846b0c3ccef14eb0cdc70cee9t |
| 374 |
return !n.getData().containsKey(ConflictResolver.NODE_DATA_WINNER); |
| 375 |
} |
| 376 |
}); |
| 377 |
final DependencyResult result = system.resolveDependencies(getSession(), dependencyRequest); |
| 378 |
if (isLogging(LOG_DEBUG)) |
| 379 |
log(LOG_DEBUG, "DependencyManager.resolve: " + result); |
| 380 |
return result; |
| 381 |
} catch (DependencyResolutionException e) { |
| 382 |
throw new RuntimeException("Error resolving dependencies.", e); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
private static Path path(Artifact artifact) { |
| 387 |
return artifact.getFile().toPath().toAbsolutePath(); |
| 388 |
} |
| 389 |
|
| 390 |
public final String getLatestVersion(String coords, String type) { |
| 391 |
return artifactToCoords(getLatestVersion0(coords, type)); |
| 392 |
} |
| 393 |
|
| 394 |
protected Artifact getLatestVersion0(String coords, String type) { |
| 395 |
try { |
| 396 |
final Artifact artifact = coordsToArtifact(coords, type); |
| 397 |
final String version; |
| 398 |
if (isVersionRange(artifact.getVersion())) { |
| 399 |
final VersionRangeRequest request = new VersionRangeRequest().setRepositories(repos).setArtifact(artifact); |
| 400 |
final VersionRangeResult result = system.resolveVersionRange(getSession(), request); |
| 401 |
final Version highestVersion = result.getHighestVersion(); |
| 402 |
version = highestVersion != null ? highestVersion.toString() : null; |
| 403 |
} else { |
| 404 |
final VersionRequest request = new VersionRequest().setRepositories(repos).setArtifact(artifact); |
| 405 |
final VersionResult result = system.resolveVersion(getSession(), request); |
| 406 |
version = result.getVersion(); |
| 407 |
} |
| 408 |
if (version == null) |
| 409 |
throw new RuntimeException("Could not find any version of artifact " + coords + " (looking for: " + artifact + ")"); |
| 410 |
return artifact.setVersion(version); |
| 411 |
} catch (RepositoryException e) { |
| 412 |
throw new RuntimeException(e); |
| 413 |
} |
| 414 |
} |
| 415 |
//</editor-fold> |
| 416 |
|
| 417 |
//<editor-fold defaultstate="collapsed" desc="Parsing"> |
| 418 |
/////////// Parsing /////////////////////////////////// |
| 419 |
public static Dependency toDependency(String coords, String type) { |
| 420 |
return new Dependency(coordsToArtifact(coords, type), JavaScopes.RUNTIME, false, getExclusions(coords)); |
| 421 |
} |
| 422 |
|
| 423 |
protected static List<Dependency> toDependencies(List<String> coords, String type) { |
| 424 |
final List<Dependency> deps = new ArrayList<Dependency>(coords.size()); |
| 425 |
for (String c : coords) |
| 426 |
deps.add(toDependency(c, type)); |
| 427 |
return deps; |
| 428 |
} |
| 429 |
|
| 430 |
static String artifactToCoords(Artifact artifact) { |
| 431 |
if (artifact == null) |
| 432 |
return null; |
| 433 |
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() |
| 434 |
+ ((artifact.getClassifier() != null && !artifact.getClassifier().isEmpty()) ? (":" + artifact.getClassifier()) : ""); |
| 435 |
} |
| 436 |
|
| 437 |
private static boolean isVersionRange(String version) { |
| 438 |
return version.startsWith("(") || version.startsWith("["); |
| 439 |
} |
| 440 |
|
| 441 |
private static final Pattern PAT_DEPENDENCY = Pattern.compile("(?<groupId>[^:\\(]+):(?<artifactId>[^:\\(]+)(:(?<version>\\(?[^:\\(]*))?(:(?<classifier>[^:\\(]+))?(\\((?<exclusions>[^\\(\\)]*)\\))?"); |
| 442 |
|
| 443 |
private static Artifact coordsToArtifact(String depString, String type) { |
| 444 |
final Matcher m = PAT_DEPENDENCY.matcher(depString); |
| 445 |
if (!m.matches()) |
| 446 |
throw new IllegalArgumentException("Could not parse dependency: " + depString); |
| 447 |
|
| 448 |
final String groupId = m.group("groupId"); |
| 449 |
final String artifactId = m.group("artifactId"); |
| 450 |
String version = m.group("version"); |
| 451 |
if (version == null || version.isEmpty()) |
| 452 |
version = LATEST_VERSION; |
| 453 |
final String classifier = m.group("classifier"); |
| 454 |
return new DefaultArtifact(groupId, artifactId, classifier, type, version); |
| 455 |
} |
| 456 |
|
| 457 |
private static Collection<Exclusion> getExclusions(String depString) { |
| 458 |
final Matcher m = PAT_DEPENDENCY.matcher(depString); |
| 459 |
if (!m.matches()) |
| 460 |
throw new IllegalArgumentException("Could not parse dependency: " + depString); |
| 461 |
|
| 462 |
if (m.group("exclusions") == null || m.group("exclusions").isEmpty()) |
| 463 |
return null; |
| 464 |
|
| 465 |
final List<String> exclusionPatterns = Arrays.asList(m.group("exclusions").split(",")); |
| 466 |
final List<Exclusion> exclusions = new ArrayList<Exclusion>(); |
| 467 |
for (String ex : exclusionPatterns) { |
| 468 |
String[] coords = ex.trim().split(":"); |
| 469 |
if (coords.length != 2) |
| 470 |
throw new IllegalArgumentException("Illegal exclusion dependency coordinates: " + depString + " (in exclusion " + ex + ")"); |
| 471 |
exclusions.add(new Exclusion(coords[0], coords[1], "*", "*")); |
| 472 |
} |
| 473 |
return exclusions; |
| 474 |
} |
| 475 |
|
| 476 |
private static final Pattern PAT_REPO = Pattern.compile("(?<id>[^(]+)(\\((?<url>[^\\)]+)\\))?"); |
| 477 |
|
| 478 |
// visible for testing |
| 479 |
static RemoteRepository createRepo(String repo, RepositoryPolicy releasePolicy, RepositoryPolicy snapshotPolicy) { |
| 480 |
final Matcher m = PAT_REPO.matcher(repo); |
| 481 |
if (!m.matches()) |
| 482 |
throw new IllegalArgumentException("Could not parse repository: " + repo); |
| 483 |
|
| 484 |
final String id = m.group("id"); |
| 485 |
String url = m.group("url"); |
| 486 |
if (url == null && WELL_KNOWN_REPOS.containsKey(id)) |
| 487 |
return createRepo(WELL_KNOWN_REPOS.get(id), releasePolicy, snapshotPolicy); |
| 488 |
if (url == null) |
| 489 |
url = id; |
| 490 |
|
| 491 |
if (url.startsWith("file:")) { |
| 492 |
releasePolicy = new RepositoryPolicy(releasePolicy.isEnabled(), releasePolicy.getUpdatePolicy(), RepositoryPolicy.CHECKSUM_POLICY_IGNORE); |
| 493 |
snapshotPolicy = releasePolicy; |
| 494 |
} |
| 495 |
|
| 496 |
return new RemoteRepository |
| 497 |
.Builder(id, "default", url) |
| 498 |
.setReleasePolicy(releasePolicy) |
| 499 |
.setSnapshotPolicy(snapshotPolicy) |
| 500 |
.build(); |
| 501 |
} |
| 502 |
//</editor-fold> |
| 503 |
|
| 504 |
//<editor-fold defaultstate="collapsed" desc="General Utils"> |
| 505 |
/////////// General Utils /////////////////////////////////// |
| 506 |
private static PrintStream prefixStream(PrintStream out, final String prefix) { |
| 507 |
return new PrintStream(out) { |
| 508 |
@Override |
| 509 |
public void println(String x) { |
| 510 |
super.println(prefix + x); |
| 511 |
} |
| 512 |
}; |
| 513 |
} |
| 514 |
|
| 515 |
private static boolean hasArtifactPath(DependencyNode node) { |
| 516 |
Artifact a; |
| 517 |
return |
| 518 |
node != null && |
| 519 |
(a = node.getArtifact()) != null && |
| 520 |
a.getFile() != null; |
| 521 |
} |
| 522 |
|
| 523 |
private static String propertyOrEnv(String propName, String envVar) { |
| 524 |
String val = System.getProperty(propName); |
| 525 |
if (val == null) |
| 526 |
val = emptyToNull(System.getenv(envVar)); |
| 527 |
return val; |
| 528 |
} |
| 529 |
|
| 530 |
private static Boolean isPropertySet(String property, boolean defaultValue) { |
| 531 |
final String val = System.getProperty(property); |
| 532 |
if (val == null) |
| 533 |
return defaultValue; |
| 534 |
return "".equals(val) | Boolean.parseBoolean(val); |
| 535 |
} |
| 536 |
|
| 537 |
static String emptyToNull(String s) { |
| 538 |
if (s == null) |
| 539 |
return null; |
| 540 |
s = s.trim(); |
| 541 |
return s.isEmpty() ? null : s; |
| 542 |
} |
| 543 |
//</editor-fold> |
| 544 |
|
| 545 |
//<editor-fold defaultstate="collapsed" desc="Logging"> |
| 546 |
/////////// Logging /////////////////////////////////// |
| 547 |
protected final boolean isLogging(int level) { |
| 548 |
return level <= logLevel; |
| 549 |
} |
| 550 |
|
| 551 |
protected final void log(int level, String str) { |
| 552 |
if (isLogging(level)) |
| 553 |
System.err.println(LOG_PREFIX + str); |
| 554 |
} |
| 555 |
//</editor-fold> |
| 556 |
|
| 557 |
//<editor-fold defaultstate="collapsed" desc="DI Workarounds"> |
| 558 |
/////////// DI Workarounds /////////////////////////////////// |
| 559 |
// necessary if we want to forgo Guice/Sisu injection and use DefaultServiceLocator instead |
| 560 |
private static final io.takari.filemanager.FileManager takariFileManager = new io.takari.filemanager.internal.DefaultFileManager(); |
| 561 |
|
| 562 |
public static class LockingFileProcessor extends io.takari.aether.concurrency.LockingFileProcessor { |
| 563 |
public LockingFileProcessor() { |
| 564 |
super(takariFileManager); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
public static class LockingSyncContextFactory extends io.takari.aether.concurrency.LockingSyncContextFactory { |
| 569 |
public LockingSyncContextFactory() { |
| 570 |
super(takariFileManager); |
| 571 |
} |
| 572 |
} |
| 573 |
//</editor-fold> |
| 574 |
} |