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
25 import org.apache.maven.plugin.MavenPluginPrerequisitesChecker;
26 import org.apache.maven.plugin.descriptor.PluginDescriptor;
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 private final VersionScheme versionScheme;
36
37 @Inject
38 public MavenPluginJavaPrerequisiteChecker(VersionScheme versionScheme) {
39 this.versionScheme = versionScheme;
40 }
41
42 @Override
43 public void accept(PluginDescriptor pluginDescriptor) {
44 String requiredJavaVersion = pluginDescriptor.getRequiredJavaVersion();
45 if (requiredJavaVersion != null && !requiredJavaVersion.isEmpty()) {
46 String currentJavaVersion = System.getProperty("java.version");
47 if (!matchesVersion(requiredJavaVersion, currentJavaVersion)) {
48 throw new IllegalStateException("Required Java version " + requiredJavaVersion
49 + " is not met by current version: " + currentJavaVersion);
50 }
51 }
52 }
53
54 boolean matchesVersion(String requiredVersion, String currentVersion) {
55 VersionConstraint constraint;
56 try {
57 constraint = versionScheme.parseVersionConstraint(requiredVersion);
58 } catch (InvalidVersionSpecificationException e) {
59 throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e);
60 }
61 Version current;
62 try {
63 current = versionScheme.parseVersion(currentVersion);
64 } catch (InvalidVersionSpecificationException e) {
65 throw new IllegalStateException("Could not parse current Java version", e);
66 }
67 if (constraint.getRange() == null) {
68 return constraint.getVersion().compareTo(current) <= 0;
69 }
70 return constraint.containsVersion(current);
71 }
72 }