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