View Javadoc

1   package org.apache.maven.plugin.invoker;
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 org.apache.maven.project.MavenProject;
23  import org.codehaus.plexus.util.Os;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Properties;
31  
32  /**
33   * Provides utility methods for selecting build jobs based on environmental conditions.
34   *
35   * @author Benjamin Bentmann
36   */
37  class SelectorUtils
38  {
39  
40      static void parseList( String list, Collection<String> includes, Collection<String> excludes )
41      {
42          String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
43  
44          for ( int i = 0; i < tokens.length; i++ )
45          {
46              String token = tokens[i].trim();
47  
48              if ( token.startsWith( "!" ) )
49              {
50                  excludes.add( token.substring( 1 ) );
51              }
52              else
53              {
54                  includes.add( token );
55              }
56          }
57      }
58  
59      static boolean isOsFamily( String osSpec )
60      {
61          List<String> includes = new ArrayList<String>();
62          List<String> excludes = new ArrayList<String>();
63          parseList( osSpec, includes, excludes );
64  
65          return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
66      }
67  
68      static boolean isOsFamily( List<String> families, boolean defaultMatch )
69      {
70          if ( families != null && !families.isEmpty() )
71          {
72              for ( String family : families )
73              {
74                  if ( Os.isFamily( family ) )
75                  {
76                      return true;
77                  }
78              }
79  
80              return false;
81          }
82          else
83          {
84              return defaultMatch;
85          }
86      }
87  
88      /**
89       * Retrieves the current Maven version.
90       * @return The current Maven version.
91       */
92      static String getMavenVersion()
93      {
94          try
95          {
96              // This relies on the fact that MavenProject is the in core classloader
97              // and that the core classloader is for the maven-core artifact
98              // and that should have a pom.properties file
99              // if this ever changes, we will have to revisit this code.
100             Properties properties = new Properties();
101             properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
102                 "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
103             return StringUtils.trim( properties.getProperty( "version" ) );
104         }
105         catch ( Exception e )
106         {
107             return null;
108         }
109     }
110 
111     static boolean isMavenVersion( String mavenSpec )
112     {
113         List<String> includes = new ArrayList<String>();
114         List<String> excludes = new ArrayList<String>();
115         parseList( mavenSpec, includes, excludes );
116 
117         List<Integer> mavenVersionList = parseVersion( getMavenVersion() );
118 
119         return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false );
120     }
121 
122     static boolean isJreVersion( String jreSpec )
123     {
124         List<String> includes = new ArrayList<String>();
125         List<String> excludes = new ArrayList<String>();
126         parseList( jreSpec, includes, excludes );
127 
128         List<Integer> jreVersion = parseVersion( System.getProperty( "java.version", "" ) );
129 
130         return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
131     }
132 
133     static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
134     {
135         if ( versionPatterns != null && !versionPatterns.isEmpty() )
136         {
137             for ( String versionPattern : versionPatterns )
138             {
139                 if ( isJreVersion( jreVersion, versionPattern ) )
140                 {
141                     return true;
142                 }
143             }
144 
145             return false;
146         }
147         else
148         {
149             return defaultMatch;
150         }
151     }
152 
153     static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
154     {
155         List<Integer> checkVersion = parseVersion( versionPattern );
156 
157         if ( versionPattern.endsWith( "+" ) )
158         {
159             // 1.5+ <=> [1.5,)
160             return compareVersions( jreVersion, checkVersion ) >= 0;
161         }
162         else if ( versionPattern.endsWith( "-" ) )
163         {
164             // 1.5- <=> (,1.5)
165             return compareVersions( jreVersion, checkVersion ) < 0;
166         }
167         else
168         {
169             // 1.5 <=> [1.5,1.6)
170             return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
171                 jreVersion.subList( 0, checkVersion.size() ) );
172         }
173     }
174 
175     static List<Integer> parseVersion( String version )
176     {
177         version = version.replaceAll( "[^0-9]", "." );
178 
179         String[] tokens = StringUtils.split( version, "." );
180 
181         List<Integer> numbers = new ArrayList<Integer>();
182 
183         for ( int i = 0; i < tokens.length; i++ )
184         {
185             numbers.add( Integer.valueOf( tokens[i] ) );
186         }
187 
188         return numbers;
189     }
190 
191     static int compareVersions( List<Integer> version1, List<Integer> version2 )
192     {
193         for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
194         {
195             if ( !it1.hasNext() )
196             {
197                 return it2.hasNext() ? -1 : 0;
198             }
199             if ( !it2.hasNext() )
200             {
201                 return it1.hasNext() ? 1 : 0;
202             }
203 
204             Integer num1 = it1.next();
205             Integer num2 = it2.next();
206 
207             int rel = num1.compareTo( num2 );
208             if ( rel != 0 )
209             {
210                 return rel;
211             }
212         }
213     }
214 
215 }