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