View Javadoc
1   package org.apache.maven.model.superpom;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Provides the super POM that all models implicitly inherit from.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Named
41  @Singleton
42  public class DefaultSuperPomProvider
43      implements SuperPomProvider
44  {
45  
46      /**
47       * The cached super POM, lazily created.
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  }