001package org.apache.maven.rtinfo.internal; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.commons.lang3.StringUtils; 023import org.apache.commons.lang3.Validate; 024import org.apache.maven.rtinfo.RuntimeInformation; 025import org.codehaus.plexus.component.annotations.Component; 026import org.codehaus.plexus.component.annotations.Requirement; 027import org.codehaus.plexus.logging.Logger; 028import org.eclipse.aether.util.version.GenericVersionScheme; 029import org.eclipse.aether.version.InvalidVersionSpecificationException; 030import org.eclipse.aether.version.Version; 031import org.eclipse.aether.version.VersionConstraint; 032import org.eclipse.aether.version.VersionScheme; 033 034import java.io.IOException; 035import java.io.InputStream; 036import java.util.Properties; 037 038/** 039 * Provides information about the current Maven runtime. 040 */ 041@Component( role = RuntimeInformation.class ) 042public class DefaultRuntimeInformation 043 implements RuntimeInformation 044{ 045 046 @Requirement 047 private Logger logger; 048 049 private String mavenVersion; 050 051 public String getMavenVersion() 052 { 053 if ( mavenVersion == null ) 054 { 055 Properties props = new Properties(); 056 057 String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties"; 058 059 try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) ) 060 { 061 if ( is != null ) 062 { 063 props.load( is ); 064 } 065 else 066 { 067 logger.warn( 068 "Could not locate " + resource + " on classpath, Maven runtime information not available" ); 069 } 070 } 071 catch ( IOException e ) 072 { 073 String msg = "Could not parse " + resource + ", Maven runtime information not available"; 074 if ( logger.isDebugEnabled() ) 075 { 076 logger.warn( msg, e ); 077 } 078 else 079 { 080 logger.warn( msg ); 081 } 082 } 083 084 String version = props.getProperty( "version", "" ).trim(); 085 086 if ( !version.startsWith( "${" ) ) 087 { 088 mavenVersion = version; 089 } 090 else 091 { 092 mavenVersion = ""; 093 } 094 } 095 096 return mavenVersion; 097 } 098 099 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}