001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.compiler.grape; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.eclipse.aether.artifact.DefaultArtifact; 026import org.eclipse.aether.graph.Dependency; 027import org.eclipse.aether.graph.Exclusion; 028import org.eclipse.aether.util.artifact.JavaScopes; 029 030import org.springframework.boot.cli.compiler.dependencies.ArtifactCoordinatesResolver; 031import org.springframework.boot.cli.compiler.dependencies.CompositeDependencyManagement; 032import org.springframework.boot.cli.compiler.dependencies.DependencyManagement; 033import org.springframework.boot.cli.compiler.dependencies.DependencyManagementArtifactCoordinatesResolver; 034 035/** 036 * Context used when resolving dependencies. 037 * 038 * @author Andy Wilkinson 039 * @since 1.1.0 040 */ 041public class DependencyResolutionContext { 042 043 private final Map<String, Dependency> managedDependencyByGroupAndArtifact = new HashMap<String, Dependency>(); 044 045 private final List<Dependency> managedDependencies = new ArrayList<Dependency>(); 046 047 private DependencyManagement dependencyManagement = null; 048 049 private ArtifactCoordinatesResolver artifactCoordinatesResolver; 050 051 private String getIdentifier(Dependency dependency) { 052 return getIdentifier(dependency.getArtifact().getGroupId(), 053 dependency.getArtifact().getArtifactId()); 054 } 055 056 private String getIdentifier(String groupId, String artifactId) { 057 return groupId + ":" + artifactId; 058 } 059 060 public ArtifactCoordinatesResolver getArtifactCoordinatesResolver() { 061 return this.artifactCoordinatesResolver; 062 } 063 064 public String getManagedVersion(String groupId, String artifactId) { 065 Dependency dependency = getManagedDependency(groupId, artifactId); 066 if (dependency == null) { 067 dependency = this.managedDependencyByGroupAndArtifact 068 .get(getIdentifier(groupId, artifactId)); 069 } 070 return dependency != null ? dependency.getArtifact().getVersion() : null; 071 } 072 073 public List<Dependency> getManagedDependencies() { 074 return Collections.unmodifiableList(this.managedDependencies); 075 } 076 077 private Dependency getManagedDependency(String group, String artifact) { 078 return this.managedDependencyByGroupAndArtifact 079 .get(getIdentifier(group, artifact)); 080 } 081 082 public void addManagedDependencies(List<Dependency> dependencies) { 083 this.managedDependencies.addAll(dependencies); 084 for (Dependency dependency : dependencies) { 085 this.managedDependencyByGroupAndArtifact.put(getIdentifier(dependency), 086 dependency); 087 } 088 } 089 090 public void addDependencyManagement(DependencyManagement dependencyManagement) { 091 for (org.springframework.boot.cli.compiler.dependencies.Dependency dependency : dependencyManagement 092 .getDependencies()) { 093 List<Exclusion> aetherExclusions = new ArrayList<Exclusion>(); 094 for (org.springframework.boot.cli.compiler.dependencies.Dependency.Exclusion exclusion : dependency 095 .getExclusions()) { 096 aetherExclusions.add(new Exclusion(exclusion.getGroupId(), 097 exclusion.getArtifactId(), "*", "*")); 098 } 099 Dependency aetherDependency = new Dependency( 100 new DefaultArtifact(dependency.getGroupId(), 101 dependency.getArtifactId(), "jar", dependency.getVersion()), 102 JavaScopes.COMPILE, false, aetherExclusions); 103 this.managedDependencies.add(0, aetherDependency); 104 this.managedDependencyByGroupAndArtifact.put(getIdentifier(aetherDependency), 105 aetherDependency); 106 } 107 this.dependencyManagement = this.dependencyManagement == null 108 ? dependencyManagement 109 : new CompositeDependencyManagement(dependencyManagement, 110 this.dependencyManagement); 111 this.artifactCoordinatesResolver = new DependencyManagementArtifactCoordinatesResolver( 112 this.dependencyManagement); 113 } 114 115}