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 org.apache.commons.lang3.StringUtils;
23 import org.apache.commons.lang3.Validate;
24 import org.apache.maven.rtinfo.RuntimeInformation;
25 import org.codehaus.plexus.component.annotations.Component;
26 import org.codehaus.plexus.component.annotations.Requirement;
27 import org.codehaus.plexus.logging.Logger;
28 import org.eclipse.aether.util.version.GenericVersionScheme;
29 import org.eclipse.aether.version.InvalidVersionSpecificationException;
30 import org.eclipse.aether.version.Version;
31 import org.eclipse.aether.version.VersionConstraint;
32 import org.eclipse.aether.version.VersionScheme;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.Properties;
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 try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) )
60 {
61 if ( is != null )
62 {
63 props.load( is );
64 }
65 else
66 {
67 logger.warn(
68 "Could not locate " + resource + " on classpath, Maven runtime information not available" );
69 }
70 }
71 catch ( IOException e )
72 {
73 String msg = "Could not parse " + resource + ", Maven runtime information not available";
74 if ( logger.isDebugEnabled() )
75 {
76 logger.warn( msg, e );
77 }
78 else
79 {
80 logger.warn( msg );
81 }
82 }
83
84 String version = props.getProperty( "version", "" ).trim();
85
86 if ( !version.startsWith( "${" ) )
87 {
88 mavenVersion = version;
89 }
90 else
91 {
92 mavenVersion = "";
93 }
94 }
95
96 return mavenVersion;
97 }
98
99 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 }