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 org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.lang3.Validate;
24  import org.apache.maven.rtinfo.RuntimeInformation;
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.component.annotations.Requirement;
27  import org.codehaus.plexus.logging.Logger;
28  import org.eclipse.aether.util.version.GenericVersionScheme;
29  import org.eclipse.aether.version.InvalidVersionSpecificationException;
30  import org.eclipse.aether.version.Version;
31  import org.eclipse.aether.version.VersionConstraint;
32  import org.eclipse.aether.version.VersionScheme;
33  
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.util.Properties;
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              try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) )
60              {
61                  if ( is != null )
62                  {
63                      props.load( is );
64                  }
65                  else
66                  {
67                      logger.warn(
68                          "Could not locate " + resource + " on classpath, Maven runtime information not available" );
69                  }
70              }
71              catch ( IOException e )
72              {
73                  String msg = "Could not parse " + resource + ", Maven runtime information not available";
74                  if ( logger.isDebugEnabled() )
75                  {
76                      logger.warn( msg, e );
77                  }
78                  else
79                  {
80                      logger.warn( msg );
81                  }
82              }
83  
84              String version = props.getProperty( "version", "" ).trim();
85  
86              if ( !version.startsWith( "${" ) )
87              {
88                  mavenVersion = version;
89              }
90              else
91              {
92                  mavenVersion = "";
93              }
94          }
95  
96          return mavenVersion;
97      }
98  
99      public boolean isMavenVersion( String versionRange )
100     {
101         VersionScheme versionScheme = new GenericVersionScheme();
102 
103         Validate.notBlank( versionRange, "versionRange can neither be null, empty nor blank" );
104 
105         VersionConstraint constraint;
106         try
107         {
108             constraint = versionScheme.parseVersionConstraint( versionRange );
109         }
110         catch ( InvalidVersionSpecificationException e )
111         {
112             throw new IllegalArgumentException( e.getMessage(), e );
113         }
114 
115         Version current;
116         try
117         {
118             String mavenVersion = getMavenVersion();
119             Validate.validState( StringUtils.isNotEmpty( mavenVersion ), "Could not determine current Maven version" );
120 
121             current = versionScheme.parseVersion( mavenVersion );
122         }
123         catch ( InvalidVersionSpecificationException e )
124         {
125             throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
126         }
127 
128         if ( constraint.getRange() == null )
129         {
130             return constraint.getVersion().compareTo( current ) <= 0;
131         }
132         return constraint.containsVersion( current );
133     }
134 
135 }