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 org.apache.maven.model.InputSource;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.building.ModelProcessor;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  
33  /**
34   * Provides the super POM that all models implicitly inherit from.
35   *
36   * @author Benjamin Bentmann
37   */
38  @Component( role = SuperPomProvider.class )
39  public class DefaultSuperPomProvider
40      implements SuperPomProvider
41  {
42  
43      /**
44       * The cached super POM, lazily created.
45       */
46      private Model superModel;
47  
48      @Requirement
49      private ModelProcessor modelProcessor;
50  
51      public DefaultSuperPomProvider setModelProcessor( ModelProcessor modelProcessor )
52      {
53          this.modelProcessor = modelProcessor;
54          return this;
55      }
56  
57      @Override
58      public Model getSuperModel( String version )
59      {
60          if ( superModel == null )
61          {
62              String resource = "/org/apache/maven/model/pom-" + version + ".xml";
63  
64              InputStream is = getClass().getResourceAsStream( resource );
65  
66              if ( is == null )
67              {
68                  throw new IllegalStateException( "The super POM " + resource + " was not found"
69                      + ", please verify the integrity of your Maven installation" );
70              }
71  
72              try
73              {
74                  Map<String, Object> options = new HashMap<>();
75                  options.put( "xml:4.0.0", "xml:4.0.0" );
76  
77                  String modelId = "org.apache.maven:maven-model-builder:"
78                      + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
79                  InputSource inputSource = new InputSource();
80                  inputSource.setModelId( modelId );
81                  inputSource.setLocation( getClass().getResource( resource ).toExternalForm() );
82                  options.put( ModelProcessor.INPUT_SOURCE, inputSource );
83  
84                  superModel = modelProcessor.read( is, options );
85              }
86              catch ( IOException e )
87              {
88                  throw new IllegalStateException( "The super POM " + resource + " is damaged"
89                      + ", please verify the integrity of your Maven installation", e );
90              }
91          }
92  
93          return superModel;
94      }
95  
96  }