|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.loader.thin; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.apache.maven.model.Model; |
| 25 | +import org.apache.maven.model.Parent; |
| 26 | +import org.apache.maven.model.building.DefaultModelProcessor; |
| 27 | +import org.apache.maven.model.io.DefaultModelReader; |
| 28 | +import org.apache.maven.model.locator.DefaultModelLocator; |
| 29 | +import org.eclipse.aether.artifact.Artifact; |
| 30 | +import org.eclipse.aether.artifact.DefaultArtifact; |
| 31 | +import org.eclipse.aether.graph.Dependency; |
| 32 | + |
| 33 | +import org.springframework.core.io.Resource; |
| 34 | + |
| 35 | +/** |
| 36 | + * Utility class to help with reading and extracting dependencies from a physical pom. |
| 37 | + * |
| 38 | + * @author Dave Syer |
| 39 | + * |
| 40 | + */ |
| 41 | +public class PomLoader { |
| 42 | + |
| 43 | + public List<Dependency> getDependencies(Resource pom) { |
| 44 | + if (!pom.exists()) { |
| 45 | + return Collections.emptyList(); |
| 46 | + } |
| 47 | + Model model = readModel(pom); |
| 48 | + return convert(model.getDependencies()); |
| 49 | + } |
| 50 | + |
| 51 | + public List<Dependency> getDependencyManagement(Resource pom) { |
| 52 | + if (!pom.exists()) { |
| 53 | + return Collections.emptyList(); |
| 54 | + } |
| 55 | + List<Dependency> list = new ArrayList<Dependency>(); |
| 56 | + Model model = readModel(pom); |
| 57 | + if (model.getParent() != null) { |
| 58 | + list.add(new Dependency(getParentArtifact(model), "import")); |
| 59 | + } |
| 60 | + if (model.getDependencyManagement() != null) { |
| 61 | + list.addAll(convert(model.getDependencyManagement().getDependencies())); |
| 62 | + } |
| 63 | + return list; |
| 64 | + } |
| 65 | + |
| 66 | + private Artifact getParentArtifact(Model model) { |
| 67 | + Parent parent = model.getParent(); |
| 68 | + return new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), "pom", |
| 69 | + parent.getVersion()); |
| 70 | + } |
| 71 | + |
| 72 | + private List<Dependency> convert( |
| 73 | + List<org.apache.maven.model.Dependency> dependencies) { |
| 74 | + List<Dependency> result = new ArrayList<Dependency>(); |
| 75 | + for (org.apache.maven.model.Dependency dependency : dependencies) { |
| 76 | + String scope = dependency.getScope(); |
| 77 | + if (!"test".equals(scope) && !"provided".equals(scope)) { |
| 78 | + result.add(new Dependency(artifact(dependency), dependency.getScope())); |
| 79 | + } |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + private Artifact artifact(org.apache.maven.model.Dependency dependency) { |
| 85 | + return new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), |
| 86 | + dependency.getClassifier(), dependency.getType(), |
| 87 | + dependency.getVersion()); |
| 88 | + } |
| 89 | + |
| 90 | + private static Model readModel(Resource resource) { |
| 91 | + DefaultModelProcessor modelProcessor = new DefaultModelProcessor(); |
| 92 | + modelProcessor.setModelLocator(new DefaultModelLocator()); |
| 93 | + modelProcessor.setModelReader(new DefaultModelReader()); |
| 94 | + |
| 95 | + try { |
| 96 | + return modelProcessor.read(resource.getInputStream(), null); |
| 97 | + } |
| 98 | + catch (IOException ex) { |
| 99 | + throw new IllegalStateException("Failed to build model from effective pom", |
| 100 | + ex); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments