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