View Javadoc
1   package org.apache.maven.rtinfo.internal;
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 java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Properties;
25  
26  import org.apache.maven.rtinfo.RuntimeInformation;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.codehaus.plexus.component.annotations.Requirement;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.util.IOUtil;
31  import org.codehaus.plexus.util.StringUtils;
32  import org.eclipse.aether.util.version.GenericVersionScheme;
33  import org.eclipse.aether.version.InvalidVersionSpecificationException;
34  import org.eclipse.aether.version.Version;
35  import org.eclipse.aether.version.VersionConstraint;
36  import org.eclipse.aether.version.VersionScheme;
37  
38  /**
39   * Provides information about the current Maven runtime.
40   */
41  @Component( role = RuntimeInformation.class )
42  public class DefaultRuntimeInformation
43      implements RuntimeInformation
44  {
45  
46      @Requirement
47      private Logger logger;
48  
49      private String mavenVersion;
50  
51      public String getMavenVersion()
52      {
53          if ( mavenVersion == null )
54          {
55              Properties props = new Properties();
56  
57              String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
58  
59              InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource );
60              if ( is != null )
61              {
62                  try
63                  {
64                      props.load( is );
65                  }
66                  catch ( IOException e )
67                  {
68                      String msg = "Could not parse " + resource + ", Maven runtime information not available";
69                      if ( logger.isDebugEnabled() )
70                      {
71                          logger.warn( msg, e );
72                      }
73                      else
74                      {
75                          logger.warn( msg );
76                      }
77                  }
78                  finally
79                  {
80                      IOUtil.close( is );
81                  }
82              }
83              else
84              {
85                  logger.warn( "Could not locate " + resource
86                               + " on classpath, Maven runtime information not available" );
87              }
88  
89              String version = props.getProperty( "version", "" ).trim();
90  
91              if ( !version.startsWith( "${" ) )
92              {
93                  mavenVersion = version;
94              }
95              else
96              {
97                  mavenVersion = "";
98              }
99          }
100 
101         return mavenVersion;
102     }
103 
104     public boolean isMavenVersion( String versionRange )
105     {
106         VersionScheme versionScheme = new GenericVersionScheme();
107 
108         if ( versionRange == null )
109         {
110             throw new IllegalArgumentException( "Version range must not be null" );
111         }
112         if ( StringUtils.isBlank( versionRange ) )
113         {
114             throw new IllegalArgumentException( "Version range must not be empty" );
115         }
116 
117         VersionConstraint constraint;
118         try
119         {
120             constraint = versionScheme.parseVersionConstraint( versionRange );
121         }
122         catch ( InvalidVersionSpecificationException e )
123         {
124             throw new IllegalArgumentException( e.getMessage(), e );
125         }
126 
127         Version current;
128         try
129         {
130             String mavenVersion = getMavenVersion();
131             if ( mavenVersion.length() <= 0 )
132             {
133                 throw new IllegalStateException( "Could not determine current Maven version" );
134             }
135 
136             current = versionScheme.parseVersion( mavenVersion );
137         }
138         catch ( InvalidVersionSpecificationException e )
139         {
140             throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
141         }
142 
143         if ( constraint.getRange() == null )
144         {
145             return constraint.getVersion().compareTo( current ) <= 0;
146         }
147         return constraint.containsVersion( current );
148     }
149 
150 }