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