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