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.eclipse.aether.util.version.GenericVersionScheme;
33 import org.eclipse.aether.version.InvalidVersionSpecificationException;
34 import org.eclipse.aether.version.Version;
35 import org.eclipse.aether.version.VersionConstraint;
36 import org.eclipse.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
86 + " on classpath, Maven runtime information not available" );
87 }
88
89 String version = props.getProperty( "version", "" ).trim();
90
91 if ( !version.startsWith( "${" ) )
92 {
93 mavenVersion = version;
94 }
95 else
96 {
97 mavenVersion = "";
98 }
99 }
100
101 return mavenVersion;
102 }
103
104 public boolean isMavenVersion( String versionRange )
105 {
106 VersionScheme versionScheme = new GenericVersionScheme();
107
108 if ( versionRange == null )
109 {
110 throw new IllegalArgumentException( "Version range must not be null" );
111 }
112 if ( StringUtils.isBlank( versionRange ) )
113 {
114 throw new IllegalArgumentException( "Version range must not be empty" );
115 }
116
117 VersionConstraint constraint;
118 try
119 {
120 constraint = versionScheme.parseVersionConstraint( versionRange );
121 }
122 catch ( InvalidVersionSpecificationException e )
123 {
124 throw new IllegalArgumentException( e.getMessage(), e );
125 }
126
127 Version current;
128 try
129 {
130 String mavenVersion = getMavenVersion();
131 if ( mavenVersion.length() <= 0 )
132 {
133 throw new IllegalStateException( "Could not determine current Maven version" );
134 }
135
136 current = versionScheme.parseVersion( mavenVersion );
137 }
138 catch ( InvalidVersionSpecificationException e )
139 {
140 throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
141 }
142
143 if ( constraint.getRange() == null )
144 {
145 return constraint.getVersion().compareTo( current ) <= 0;
146 }
147 return constraint.containsVersion( current );
148 }
149
150 }