1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.superpom;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.apache.maven.api.model.InputSource;
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.model.building.ModelProcessor;
33
34
35
36
37
38
39 @Named
40 @Singleton
41 public class DefaultSuperPomProvider implements SuperPomProvider {
42
43 private final ModelProcessor modelProcessor;
44
45
46
47
48 private Model superModel;
49
50 @Inject
51 public DefaultSuperPomProvider(ModelProcessor modelProcessor) {
52 this.modelProcessor = modelProcessor;
53 }
54
55 @Override
56 public Model getSuperModel(String version) {
57 if (superModel == null) {
58 String resource = "/org/apache/maven/model/pom-" + version + ".xml";
59
60 InputStream is = getClass().getResourceAsStream(resource);
61
62 if (is == null) {
63 throw new IllegalStateException("The super POM " + resource + " was not found"
64 + ", please verify the integrity of your Maven installation");
65 }
66
67 try {
68 Map<String, Object> options = new HashMap<>(2);
69 options.put("xml:4.0.0", "xml:4.0.0");
70
71 String modelId = "org.apache.maven:maven-model-builder:"
72 + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
73 InputSource inputSource = new InputSource(
74 modelId, getClass().getResource(resource).toExternalForm());
75 options.put(ModelProcessor.INPUT_SOURCE, new org.apache.maven.model.InputSource(inputSource));
76
77 superModel = modelProcessor.read(is, options).getDelegate();
78 } catch (IOException e) {
79 throw new IllegalStateException(
80 "The super POM " + resource + " is damaged"
81 + ", please verify the integrity of your Maven installation",
82 e);
83 }
84 }
85
86 return superModel;
87 }
88 }