1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24 import org.apache.maven.plugin.MavenPluginPrerequisitesChecker;
25 import org.apache.maven.plugin.descriptor.PluginDescriptor;
26 import org.codehaus.plexus.util.StringUtils;
27 import org.eclipse.aether.version.InvalidVersionSpecificationException;
28 import org.eclipse.aether.version.Version;
29 import org.eclipse.aether.version.VersionConstraint;
30 import org.eclipse.aether.version.VersionScheme;
31
32 @Named
33 @Singleton
34 public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker {
35
36 private final VersionScheme versionScheme;
37
38 @Inject
39 public MavenPluginJavaPrerequisiteChecker(final VersionScheme versionScheme) {
40 this.versionScheme = versionScheme;
41 }
42
43 @Override
44 public void accept(PluginDescriptor pluginDescriptor) {
45 String requiredJavaVersion = pluginDescriptor.getRequiredJavaVersion();
46 if (StringUtils.isNotBlank(requiredJavaVersion)) {
47 String currentJavaVersion = System.getProperty("java.version");
48 if (!matchesVersion(requiredJavaVersion, currentJavaVersion)) {
49 throw new IllegalStateException("Required Java version " + requiredJavaVersion
50 + " is not met by current version: " + currentJavaVersion);
51 }
52 }
53 }
54
55 boolean matchesVersion(String requiredVersion, String currentVersion) {
56 VersionConstraint constraint;
57 try {
58 constraint = versionScheme.parseVersionConstraint(requiredVersion);
59 } catch (InvalidVersionSpecificationException e) {
60 throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e);
61 }
62 Version current;
63 try {
64 current = versionScheme.parseVersion(currentVersion);
65 } catch (InvalidVersionSpecificationException e) {
66 throw new IllegalStateException("Could not parse current Java version", e);
67 }
68 if (constraint.getRange() == null) {
69 return constraint.getVersion().compareTo(current) <= 0;
70 }
71 return constraint.containsVersion(current);
72 }
73 }