1 package org.apache.maven.rtinfo.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.sonatype.aether.util.version.GenericVersionScheme;
33 import org.sonatype.aether.version.InvalidVersionSpecificationException;
34 import org.sonatype.aether.version.Version;
35 import org.sonatype.aether.version.VersionConstraint;
36 import org.sonatype.aether.version.VersionScheme;
37
38
39
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 + " on classpath, Maven runtime information not available" );
86 }
87
88 String version = props.getProperty( "version", "" ).trim();
89
90 if ( !version.startsWith( "${" ) )
91 {
92 mavenVersion = version;
93 }
94 else
95 {
96 mavenVersion = "";
97 }
98 }
99
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 }