001/*
002 * Copyright 2012-2015 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.dependencies;
018
019import java.util.ArrayList;
020import java.util.LinkedHashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.maven.model.Model;
025
026import org.springframework.boot.cli.compiler.dependencies.Dependency.Exclusion;
027
028/**
029 * {@link DependencyManagement} derived from a Maven {@link Model}.
030 *
031 * @author Andy Wilkinson
032 * @since 1.3.0
033 */
034public class MavenModelDependencyManagement implements DependencyManagement {
035
036        private final List<Dependency> dependencies;
037
038        private final Map<String, Dependency> byArtifactId = new LinkedHashMap<String, Dependency>();
039
040        public MavenModelDependencyManagement(Model model) {
041                this.dependencies = extractDependenciesFromModel(model);
042                for (Dependency dependency : this.dependencies) {
043                        this.byArtifactId.put(dependency.getArtifactId(), dependency);
044                }
045        }
046
047        private static List<Dependency> extractDependenciesFromModel(Model model) {
048                List<Dependency> dependencies = new ArrayList<Dependency>();
049                for (org.apache.maven.model.Dependency mavenDependency : model
050                                .getDependencyManagement().getDependencies()) {
051                        List<Exclusion> exclusions = new ArrayList<Exclusion>();
052                        for (org.apache.maven.model.Exclusion mavenExclusion : mavenDependency
053                                        .getExclusions()) {
054                                exclusions.add(new Exclusion(mavenExclusion.getGroupId(),
055                                                mavenExclusion.getArtifactId()));
056                        }
057                        Dependency dependency = new Dependency(mavenDependency.getGroupId(),
058                                        mavenDependency.getArtifactId(), mavenDependency.getVersion(),
059                                        exclusions);
060                        dependencies.add(dependency);
061                }
062                return dependencies;
063        }
064
065        @Override
066        public List<Dependency> getDependencies() {
067                return this.dependencies;
068        }
069
070        @Override
071        public String getSpringBootVersion() {
072                return find("spring-boot").getVersion();
073        }
074
075        @Override
076        public Dependency find(String artifactId) {
077                return this.byArtifactId.get(artifactId);
078        }
079
080}