1 package org.apache.maven.plugin.invoker;
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.ArrayList;
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Properties;
32
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.util.Os;
35 import org.codehaus.plexus.util.StringUtils;
36
37
38
39
40
41
42 class SelectorUtils
43 {
44
45 static void parseList( String list, Collection<String> includes, Collection<String> excludes )
46 {
47 String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
48
49 for ( String token1 : tokens )
50 {
51 String token = token1.trim();
52
53 if ( token.startsWith( "!" ) )
54 {
55 excludes.add( token.substring( 1 ) );
56 }
57 else
58 {
59 includes.add( token );
60 }
61 }
62 }
63
64 static boolean isOsFamily( String osSpec )
65 {
66 List<String> includes = new ArrayList<String>();
67 List<String> excludes = new ArrayList<String>();
68 parseList( osSpec, includes, excludes );
69
70 return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
71 }
72
73 static boolean isOsFamily( List<String> families, boolean defaultMatch )
74 {
75 if ( families != null && !families.isEmpty() )
76 {
77 for ( String family : families )
78 {
79 if ( Os.isFamily( family ) )
80 {
81 return true;
82 }
83 }
84
85 return false;
86 }
87 else
88 {
89 return defaultMatch;
90 }
91 }
92
93
94
95
96
97
98 static String getMavenVersion()
99 {
100 try
101 {
102
103
104
105
106 Properties properties = new Properties();
107 properties.load( MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
108 return StringUtils.trim( properties.getProperty( "version" ) );
109 }
110 catch ( Exception e )
111 {
112 return null;
113 }
114 }
115
116 static String getMavenVersion( File mavenHome )
117 {
118 File mavenLib = new File( mavenHome, "lib" );
119 File[] jarFiles = mavenLib.listFiles( new FilenameFilter()
120 {
121 public boolean accept( File dir, String name )
122 {
123 return name.endsWith( ".jar" );
124 }
125 } );
126
127 for ( File file : jarFiles )
128 {
129 try
130 {
131 @SuppressWarnings( "deprecation" )
132 URL url =
133 new URL( "jar:" + file.toURL().toExternalForm()
134 + "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );
135
136 Properties properties = new Properties();
137 properties.load( url.openStream() );
138 String version = StringUtils.trim( properties.getProperty( "version" ) );
139 if ( version != null )
140 {
141 return version;
142 }
143 }
144 catch ( MalformedURLException e )
145 {
146
147 }
148 catch ( IOException e )
149 {
150
151 }
152 }
153 return null;
154 }
155
156 static boolean isMavenVersion( String mavenSpec )
157 {
158 return isMavenVersion( mavenSpec, getMavenVersion() );
159 }
160
161 static boolean isMavenVersion( String mavenSpec, String actualVersion )
162 {
163 List<String> includes = new ArrayList<String>();
164 List<String> excludes = new ArrayList<String>();
165 parseList( mavenSpec, includes, excludes );
166
167 List<Integer> mavenVersionList = parseVersion( actualVersion );
168
169 return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false );
170 }
171
172 static String getJreVersion()
173 {
174 return System.getProperty( "java.version", "" );
175 }
176
177 static String getJreVersion( File javaHome )
178 {
179
180 return null;
181 }
182
183 static boolean isJreVersion( String jreSpec )
184 {
185 return isJreVersion( jreSpec, getJreVersion() );
186 }
187
188 static boolean isJreVersion( String jreSpec, String actualJreVersion )
189 {
190 List<String> includes = new ArrayList<String>();
191 List<String> excludes = new ArrayList<String>();
192 parseList( jreSpec, includes, excludes );
193
194 List<Integer> jreVersion = parseVersion( actualJreVersion );
195
196 return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
197 }
198
199 static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
200 {
201 if ( versionPatterns != null && !versionPatterns.isEmpty() )
202 {
203 for ( String versionPattern : versionPatterns )
204 {
205 if ( isJreVersion( jreVersion, versionPattern ) )
206 {
207 return true;
208 }
209 }
210
211 return false;
212 }
213 else
214 {
215 return defaultMatch;
216 }
217 }
218
219 static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
220 {
221 List<Integer> checkVersion = parseVersion( versionPattern );
222
223 if ( versionPattern.endsWith( "+" ) )
224 {
225
226 return compareVersions( jreVersion, checkVersion ) >= 0;
227 }
228 else if ( versionPattern.endsWith( "-" ) )
229 {
230
231 return compareVersions( jreVersion, checkVersion ) < 0;
232 }
233 else
234 {
235
236 return checkVersion.size() <= jreVersion.size()
237 && checkVersion.equals( jreVersion.subList( 0, checkVersion.size() ) );
238 }
239 }
240
241 static List<Integer> parseVersion( String version )
242 {
243 version = version.replaceAll( "[^0-9]", "." );
244
245 String[] tokens = StringUtils.split( version, "." );
246
247 List<Integer> numbers = new ArrayList<Integer>();
248
249 for ( String token : tokens )
250 {
251 numbers.add( Integer.valueOf( token ) );
252 }
253
254 return numbers;
255 }
256
257 static int compareVersions( List<Integer> version1, List<Integer> version2 )
258 {
259 for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator();; )
260 {
261 if ( !it1.hasNext() )
262 {
263 return it2.hasNext() ? -1 : 0;
264 }
265 if ( !it2.hasNext() )
266 {
267 return it1.hasNext() ? 1 : 0;
268 }
269
270 Integer num1 = it1.next();
271 Integer num2 = it2.next();
272
273 int rel = num1.compareTo( num2 );
274 if ( rel != 0 )
275 {
276 return rel;
277 }
278 }
279 }
280
281 }