View Javadoc
1   package org.codehaus.mojo.siteskinner;
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.File;
23  import java.io.FilenameFilter;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.Properties;
28  
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * Based on the SelectorUtils from the maven-invoker-plugin
34   * 
35   * @author Robert Scholte
36   * @since 1.1
37   */
38  public final class SelectorUtils
39  {
40  
41      private SelectorUtils()
42      {
43      }
44      
45      /**
46       * Retrieves the current Maven version.
47       * @return The current Maven version.
48       */
49      static String getMavenVersion()
50      {
51          try
52          {
53              // This relies on the fact that MavenProject is the in core classloader
54              // and that the core classloader is for the maven-core artifact
55              // and that should have a pom.properties file
56              // if this ever changes, we will have to revisit this code.
57              Properties properties = new Properties();
58              properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
59                  "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
60              return StringUtils.trim( properties.getProperty( "version" ) );
61          }
62          catch ( Exception e )
63          {
64              return null;
65          }
66      }
67      
68      static String getMavenVersion( File mavenHome )
69      {
70          File mavenLib = new File( mavenHome, "lib" );
71          File[] jarFiles = mavenLib.listFiles( new FilenameFilter()
72          {
73              public boolean accept( File dir, String name )
74              {
75                  return name.endsWith( ".jar" );
76              }
77          } );
78  
79          for ( File file : jarFiles )
80          {
81              try
82              {
83                  @SuppressWarnings( "deprecation" )
84                  URL url =
85                      new URL( "jar:" + file.toURL().toExternalForm()
86                          + "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );
87  
88                  Properties properties = new Properties();
89                  properties.load( url.openStream() );
90                  String version = StringUtils.trim( properties.getProperty( "version" ) );
91                  if ( version != null )
92                  {
93                      return version;
94                  }
95              }
96              catch ( MalformedURLException e )
97              {
98                  // ignore
99              }
100             catch ( IOException e )
101             {
102                 // ignore
103             }
104         }
105         return null;
106     }
107 }