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.eclipse.aether.version.InvalidVersionSpecificationException;
26 import org.eclipse.aether.version.Version;
27 import org.eclipse.aether.version.VersionConstraint;
28 import org.eclipse.aether.version.VersionScheme;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.Properties;
35
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import javax.inject.Singleton;
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultRuntimeInformation
46 implements RuntimeInformation
47 {
48 private final Logger logger = LoggerFactory.getLogger( getClass() );
49
50 private final VersionScheme versionScheme;
51
52 private final String mavenVersion;
53
54 @Inject
55 public DefaultRuntimeInformation( final VersionScheme versionScheme )
56 {
57 this.versionScheme = versionScheme;
58 this.mavenVersion = loadMavenVersion();
59 }
60
61 @Override
62 public String getMavenVersion()
63 {
64 return mavenVersion;
65 }
66
67 private String loadMavenVersion()
68 {
69 Properties props = new Properties();
70
71 String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
72
73 try ( InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource ) )
74 {
75 if ( is != null )
76 {
77 props.load( is );
78 }
79 else
80 {
81 logger.warn(
82 "Could not locate " + resource + " on classpath, Maven runtime information not available" );
83 }
84 }
85 catch ( IOException e )
86 {
87 String msg = "Could not parse " + resource + ", Maven runtime information not available";
88 if ( logger.isDebugEnabled() )
89 {
90 logger.warn( msg, e );
91 }
92 else
93 {
94 logger.warn( msg );
95 }
96 }
97
98 String version = props.getProperty( "version", "" ).trim();
99
100 if ( !version.startsWith( "${" ) )
101 {
102 return version;
103 }
104 else
105 {
106 return "";
107 }
108 }
109
110 @Override
111 public boolean isMavenVersion( String versionRange )
112 {
113 Validate.notBlank( versionRange, "versionRange can neither be null, empty nor blank" );
114
115 VersionConstraint constraint;
116 try
117 {
118 constraint = versionScheme.parseVersionConstraint( versionRange );
119 }
120 catch ( InvalidVersionSpecificationException e )
121 {
122 throw new IllegalArgumentException( e.getMessage(), e );
123 }
124
125 Version current;
126 try
127 {
128 String mavenVersion = getMavenVersion();
129 Validate.validState( StringUtils.isNotEmpty( mavenVersion ), "Could not determine current Maven version" );
130
131 current = versionScheme.parseVersion( mavenVersion );
132 }
133 catch ( InvalidVersionSpecificationException e )
134 {
135 throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
136 }
137
138 if ( constraint.getRange() == null )
139 {
140 return constraint.getVersion().compareTo( current ) <= 0;
141 }
142 return constraint.containsVersion( current );
143 }
144
145 }