1 package org.codehaus.mojo.siteskinner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38 public final class SelectorUtils
39 {
40
41 private SelectorUtils()
42 {
43 }
44
45
46
47
48
49 static String getMavenVersion()
50 {
51 try
52 {
53
54
55
56
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
99 }
100 catch ( IOException e )
101 {
102
103 }
104 }
105 return null;
106 }
107 }