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 ( int i = 0; i < tokens.length; i++ )
50 {
51 String token = tokens[i].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 static String getMavenVersion()
98 {
99 try
100 {
101
102
103
104
105 Properties properties = new Properties();
106 properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
107 "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
200 static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
201 {
202 if ( versionPatterns != null && !versionPatterns.isEmpty() )
203 {
204 for ( String versionPattern : versionPatterns )
205 {
206 if ( isJreVersion( jreVersion, versionPattern ) )
207 {
208 return true;
209 }
210 }
211
212 return false;
213 }
214 else
215 {
216 return defaultMatch;
217 }
218 }
219
220 static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
221 {
222 List<Integer> checkVersion = parseVersion( versionPattern );
223
224 if ( versionPattern.endsWith( "+" ) )
225 {
226
227 return compareVersions( jreVersion, checkVersion ) >= 0;
228 }
229 else if ( versionPattern.endsWith( "-" ) )
230 {
231
232 return compareVersions( jreVersion, checkVersion ) < 0;
233 }
234 else
235 {
236
237 return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
238 jreVersion.subList( 0, checkVersion.size() ) );
239 }
240 }
241
242 static List<Integer> parseVersion( String version )
243 {
244 version = version.replaceAll( "[^0-9]", "." );
245
246 String[] tokens = StringUtils.split( version, "." );
247
248 List<Integer> numbers = new ArrayList<Integer>();
249
250 for ( int i = 0; i < tokens.length; i++ )
251 {
252 numbers.add( Integer.valueOf( tokens[i] ) );
253 }
254
255 return numbers;
256 }
257
258 static int compareVersions( List<Integer> version1, List<Integer> version2 )
259 {
260 for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
261 {
262 if ( !it1.hasNext() )
263 {
264 return it2.hasNext() ? -1 : 0;
265 }
266 if ( !it2.hasNext() )
267 {
268 return it1.hasNext() ? 1 : 0;
269 }
270
271 Integer num1 = it1.next();
272 Integer num2 = it2.next();
273
274 int rel = num1.compareTo( num2 );
275 if ( rel != 0 )
276 {
277 return rel;
278 }
279 }
280 }
281
282 }