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.model.InputSource;
31 import org.apache.maven.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
44
45
46 private Model superModel;
47
48 @Inject
49 private ModelProcessor modelProcessor;
50
51 public DefaultSuperPomProvider setModelProcessor(ModelProcessor modelProcessor) {
52 this.modelProcessor = modelProcessor;
53 return this;
54 }
55
56 @Override
57 public Model getSuperModel(String version) {
58 if (superModel == null) {
59 String resource = "/org/apache/maven/model/pom-" + version + ".xml";
60
61 InputStream is = getClass().getResourceAsStream(resource);
62
63 if (is == null) {
64 throw new IllegalStateException("The super POM " + resource + " was not found"
65 + ", please verify the integrity of your Maven installation");
66 }
67
68 try {
69 Map<String, Object> options = new HashMap<>();
70 options.put("xml:4.0.0", "xml:4.0.0");
71
72 String modelId = "org.apache.maven:maven-model-builder:"
73 + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
74 InputSource inputSource = new InputSource();
75 inputSource.setModelId(modelId);
76 inputSource.setLocation(getClass().getResource(resource).toExternalForm());
77 options.put(ModelProcessor.INPUT_SOURCE, inputSource);
78
79 superModel = modelProcessor.read(is, options);
80 } catch (IOException e) {
81 throw new IllegalStateException(
82 "The super POM " + resource + " is damaged"
83 + ", please verify the integrity of your Maven installation",
84 e);
85 }
86 }
87
88 return superModel;
89 }
90 }