001 package 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
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.util.Properties;
025
026 import org.apache.maven.rtinfo.RuntimeInformation;
027 import org.codehaus.plexus.component.annotations.Component;
028 import org.codehaus.plexus.component.annotations.Requirement;
029 import org.codehaus.plexus.logging.Logger;
030 import org.codehaus.plexus.util.IOUtil;
031 import org.codehaus.plexus.util.StringUtils;
032 import org.sonatype.aether.util.version.GenericVersionScheme;
033 import org.sonatype.aether.version.InvalidVersionSpecificationException;
034 import org.sonatype.aether.version.Version;
035 import org.sonatype.aether.version.VersionConstraint;
036 import org.sonatype.aether.version.VersionScheme;
037
038 /**
039 * Provides information about the current Maven runtime.
040 */
041 @Component( role = RuntimeInformation.class )
042 public 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 InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource );
060 if ( is != null )
061 {
062 try
063 {
064 props.load( is );
065 }
066 catch ( IOException e )
067 {
068 String msg = "Could not parse " + resource + ", Maven runtime information not available";
069 if ( logger.isDebugEnabled() )
070 {
071 logger.warn( msg, e );
072 }
073 else
074 {
075 logger.warn( msg );
076 }
077 }
078 finally
079 {
080 IOUtil.close( is );
081 }
082 }
083 else
084 {
085 logger.warn( "Could not locate " + resource + " on classpath, Maven runtime information not available" );
086 }
087
088 String version = props.getProperty( "version", "" ).trim();
089
090 if ( !version.startsWith( "${" ) )
091 {
092 mavenVersion = version;
093 }
094 else
095 {
096 mavenVersion = "";
097 }
098 }
099
100 return mavenVersion;
101 }
102
103 public boolean isMavenVersion( String versionRange )
104 {
105 VersionScheme versionScheme = new GenericVersionScheme();
106
107 if ( versionRange == null )
108 {
109 throw new IllegalArgumentException( "Version range must not be null" );
110 }
111 if ( StringUtils.isBlank( versionRange ) )
112 {
113 throw new IllegalArgumentException( "Version range must not be empty" );
114 }
115
116 VersionConstraint constraint;
117 try
118 {
119 constraint = versionScheme.parseVersionConstraint( versionRange );
120 }
121 catch ( InvalidVersionSpecificationException e )
122 {
123 throw new IllegalArgumentException( e.getMessage(), e );
124 }
125
126 Version current;
127 try
128 {
129 String mavenVersion = getMavenVersion();
130 if ( mavenVersion.length() <= 0 )
131 {
132 throw new IllegalStateException( "Could not determine current Maven version" );
133 }
134
135 current = versionScheme.parseVersion( mavenVersion );
136 }
137 catch ( InvalidVersionSpecificationException e )
138 {
139 throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
140 }
141
142 if ( constraint.getRanges().isEmpty() )
143 {
144 return constraint.getVersion().compareTo( current ) <= 0;
145 }
146 return constraint.containsVersion( current );
147 }
148
149 }