From 566b9209413b072b1636d2bf0acb5c4103236ab6 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 30 Aug 2018 09:55:21 +1000 Subject: Detect and refuse to build using unsupported Java versions. --- src/main/java/org/spigotmc/builder/Bootstrap.java | 10 ++-- src/main/java/org/spigotmc/builder/BuildInfo.java | 1 + src/main/java/org/spigotmc/builder/Builder.java | 34 +++++++++++- .../java/org/spigotmc/builder/JavaVersion.java | 64 ++++++++++++++++++++++ 4 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/spigotmc/builder/JavaVersion.java diff --git a/src/main/java/org/spigotmc/builder/Bootstrap.java b/src/main/java/org/spigotmc/builder/Bootstrap.java index 34cb95e..57a2b78 100644 --- a/src/main/java/org/spigotmc/builder/Bootstrap.java +++ b/src/main/java/org/spigotmc/builder/Bootstrap.java @@ -5,24 +5,24 @@ public class Bootstrap public static void main(String[] args) throws Exception { - float javaVersion = Float.parseFloat( System.getProperty( "java.class.version" ) ); + JavaVersion javaVersion = JavaVersion.getCurrentVersion(); - if ( javaVersion < 51.0 ) + if ( javaVersion.getVersion() < JavaVersion.JAVA_7.getVersion() ) { System.err.println( "Outdated Java detected (" + javaVersion + "). BuildTools requires at least Java 7. Please update Java and try again." ); System.err.println( "You may use java -version to double check your Java version." ); return; } - if ( javaVersion < 52.0 ) + if ( javaVersion.getVersion() < JavaVersion.JAVA_8.getVersion() ) { System.err.println( "*** WARNING *** Outdated Java detected (" + javaVersion + "). Minecraft >= 1.12 requires at least Java 8." ); System.err.println( "*** WARNING *** You may use java -version to double check your Java version." ); } - if ( javaVersion > 55.0 ) + if ( javaVersion == JavaVersion.UNKNOWN ) { - System.err.println( "*** WARNING *** Unsupported Java detected (" + javaVersion + "). BuildTools has only been tested up to Java 11. Use of development Java version is not supported." ); + System.err.println( "*** WARNING *** Unsupported Java detected (" + System.getProperty( "java.class.version" ) + "). BuildTools has only been tested up to Java 11. Use of development Java version is not supported." ); System.err.println( "*** WARNING *** You may use java -version to double check your Java version." ); } diff --git a/src/main/java/org/spigotmc/builder/BuildInfo.java b/src/main/java/org/spigotmc/builder/BuildInfo.java index e0500fc..b401e67 100644 --- a/src/main/java/org/spigotmc/builder/BuildInfo.java +++ b/src/main/java/org/spigotmc/builder/BuildInfo.java @@ -11,6 +11,7 @@ public class BuildInfo private String name; private String description; private int toolsVersion = -1; + private int[] javaVersions; private Refs refs; @Data diff --git a/src/main/java/org/spigotmc/builder/Builder.java b/src/main/java/org/spigotmc/builder/Builder.java index 9d96d8b..d864956 100644 --- a/src/main/java/org/spigotmc/builder/Builder.java +++ b/src/main/java/org/spigotmc/builder/Builder.java @@ -1,6 +1,7 @@ package org.spigotmc.builder; import com.google.common.base.Charsets; +import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Throwables; import com.google.common.collect.Iterables; @@ -120,6 +121,7 @@ public class Builder OptionParser parser = new OptionParser(); OptionSpec disableCertFlag = parser.accepts( "disable-certificate-check" ); + OptionSpec disableJavaCheck = parser.accepts( "disable-java-check" ); OptionSpec dontUpdateFlag = parser.accepts( "dont-update" ); OptionSpec skipCompileFlag = parser.accepts( "skip-compile" ); OptionSpec generateSourceFlag = parser.accepts( "generate-source" ); @@ -252,7 +254,7 @@ public class Builder Git spigotGit = Git.open( spigot ); Git buildGit = Git.open( buildData ); - BuildInfo buildInfo = new BuildInfo( "Dev Build", "Development", 0, new BuildInfo.Refs( "master", "master", "master", "master" ) ); + BuildInfo buildInfo = new BuildInfo( "Dev Build", "Development", 0, null, new BuildInfo.Refs( "master", "master", "master", "master" ) ); if ( !dontUpdate ) { @@ -281,6 +283,36 @@ public class Builder System.err.println( "**** Your BuildTools is out of date and will not build the requested version. Please grab a new copy from https://www.spigotmc.org/go/buildtools-dl" ); System.exit( 1 ); } + + if ( !options.has( disableJavaCheck ) ) + { + if ( buildInfo.getJavaVersions() == null ) + { + buildInfo.setJavaVersions( new int[] + { + JavaVersion.JAVA_7.getVersion(), JavaVersion.JAVA_8.getVersion() + } ); + } + + Preconditions.checkArgument( buildInfo.getJavaVersions().length == 2, "Expected only two Java versions, got %s", JavaVersion.printVersions( buildInfo.getJavaVersions() ) ); + + JavaVersion curVersion = JavaVersion.getCurrentVersion(); + JavaVersion minVersion = JavaVersion.getByVersion( buildInfo.getJavaVersions()[0] ); + JavaVersion maxVersion = JavaVersion.getByVersion( buildInfo.getJavaVersions()[1] ); + + if ( minVersion == JavaVersion.UNKNOWN || maxVersion == JavaVersion.UNKNOWN ) + { + System.err.println( "*** This BuildTools doesn't know how to use Java versions " + JavaVersion.printVersions( buildInfo.getJavaVersions() ) + ", please update" ); + System.exit( 1 ); + } + + if ( curVersion.getVersion() < minVersion.getVersion() || curVersion.getVersion() > maxVersion.getVersion() ) + { + System.err.println( "*** The version you have requested to build requires Java versions between " + JavaVersion.printVersions( buildInfo.getJavaVersions() ) + ", but you are using " + curVersion ); + System.err.println( "*** Please rerun BuildTools using an appropriate Java version. For obvious reasons outdated MC versions do not support Java versions that did not exist at their release." ); + System.exit( 1 ); + } + } } pull( buildGit, buildInfo.getRefs().getBuildData() ); diff --git a/src/main/java/org/spigotmc/builder/JavaVersion.java b/src/main/java/org/spigotmc/builder/JavaVersion.java new file mode 100644 index 0000000..d86a1ae --- /dev/null +++ b/src/main/java/org/spigotmc/builder/JavaVersion.java @@ -0,0 +1,64 @@ +package org.spigotmc.builder; + +import java.util.HashMap; +import java.util.Map; +import lombok.Getter; + +@Getter +public enum JavaVersion +{ + + UNKNOWN( -1 ), + JAVA_5( 49 ), + JAVA_6( 50 ), + JAVA_7( 51 ), + JAVA_8( 52 ), + JAVA_9( 53 ), + JAVA_10( 54 ), + JAVA_11( 55 ); + + private final int version; + + private JavaVersion(int version) + { + this.version = version; + } + + private static final Map byVersion = new HashMap(); + + static + { + for ( JavaVersion version : values() ) + { + + byVersion.put( version.version, version ); + } + } + + public static JavaVersion getByVersion(int version) + { + return byVersion.containsKey( version ) ? byVersion.get( version ) : JavaVersion.UNKNOWN; + } + + public static JavaVersion getCurrentVersion() + { + return getByVersion( (int) Float.parseFloat( System.getProperty( "java.class.version" ) ) ); + } + + public static String printVersions(int[] versions) + { + StringBuilder sb = new StringBuilder(); + + sb.append( '[' ); + for ( int v : versions ) + { + JavaVersion found = getByVersion( v ); + sb.append( found == JavaVersion.UNKNOWN ? v : found ); + sb.append( ", " ); + } + sb.setLength( sb.length() - 2 ); + sb.append( ']' ); + + return sb.toString(); + } +} -- cgit v1.2.3