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      public Model getSuperModel( String version )
57      {
58          if ( superModel == null )
59          {
60              String resource = "/org/apache/maven/model/pom-" + version + ".xml";
61  
62              InputStream is = getClass().getResourceAsStream( resource );
63  
64              if ( is == null )
65              {
66                  throw new IllegalStateException( "The super POM " + resource + " was not found"
67                      + ", please verify the integrity of your Maven installation" );
68              }
69  
70              try
71              {
72                  Map<String, String> options = new HashMap<String, String>();
73                  options.put( "xml:4.0.0", "xml:4.0.0" );
74                  superModel = modelProcessor.read( is, options );
75              }
76              catch ( IOException e )
77              {
78                  throw new IllegalStateException( "The super POM " + resource + " is damaged"
79                      + ", please verify the integrity of your Maven installation", e );
80              }
81          }
82  
83          return superModel;
84      }
85  
86  }