diff --git a/build.gradle b/build.gradle index 4ffc147e0..822832aad 100755 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,13 @@ allprojects { } group = rootProject.maven_group - sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = targetCompatibility = JavaVersion.toVersion(project.java_version) + + java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(sourceCompatibility.majorVersion.toInteger())) + } + } repositories { maven { @@ -93,7 +99,7 @@ allprojects { tasks.withType(JavaCompile).configureEach { it.options.encoding = "UTF-8" - def targetVersion = 17 + def targetVersion = project.java_version.toInteger() if (JavaVersion.current().isJava9Compatible()) { it.options.release = targetVersion } diff --git a/buildSrc/src/main/java/baritone/gradle/task/BaritoneGradleTask.java b/buildSrc/src/main/java/baritone/gradle/task/BaritoneGradleTask.java index 2dad551f2..f27df9aaa 100644 --- a/buildSrc/src/main/java/baritone/gradle/task/BaritoneGradleTask.java +++ b/buildSrc/src/main/java/baritone/gradle/task/BaritoneGradleTask.java @@ -36,8 +36,8 @@ class BaritoneGradleTask extends DefaultTask { protected static final String - PROGUARD_ZIP = "proguard.zip", - PROGUARD_JAR = "proguard.jar", + PROGUARD_ZIP = "proguard-%s.zip", + PROGUARD_JAR = "proguard-%s.jar", PROGUARD_CONFIG_TEMPLATE = "scripts/proguard.pro", PROGUARD_CONFIG_DEST = "template.pro", PROGUARD_API_CONFIG = "api.pro", diff --git a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java index a9c7f94ea..d5e05a19c 100644 --- a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java +++ b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java @@ -26,6 +26,9 @@ import org.gradle.api.tasks.compile.ForkOptions; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.internal.jvm.Jvm; +import org.gradle.jvm.toolchain.JavaLanguageVersion; +import org.gradle.jvm.toolchain.JavaLauncher; +import org.gradle.jvm.toolchain.JavaToolchainService; import xyz.wagyourtail.unimined.api.UniminedExtension; import xyz.wagyourtail.unimined.api.minecraft.MinecraftConfig; @@ -47,17 +50,10 @@ public class ProguardTask extends BaritoneGradleTask { @Input - private String url; + private String proguardVersion; - public String getUrl() { - return url; - } - - @Input - private String extract; - - public String getExtract() { - return extract; + public String getProguardVersion() { + return proguardVersion; } private List requiredLibraries; @@ -99,98 +95,33 @@ private void processArtifact() throws Exception { } private void downloadProguard() throws Exception { - Path proguardZip = getTemporaryFile(PROGUARD_ZIP); + Path proguardZip = getTemporaryFile(String.format(PROGUARD_ZIP, proguardVersion)); if (!Files.exists(proguardZip)) { - write(new URL(this.url).openStream(), proguardZip); + write(new URL(String.format("https://github.com/Guardsquare/proguard/releases/download/v%s/proguard-%s.zip", proguardVersion, proguardVersion)).openStream(), proguardZip); } } private void extractProguard() throws Exception { - Path proguardJar = getTemporaryFile(PROGUARD_JAR); + Path proguardJar = getTemporaryFile(String.format(PROGUARD_JAR, proguardVersion)); if (!Files.exists(proguardJar)) { - ZipFile zipFile = new ZipFile(getTemporaryFile(PROGUARD_ZIP).toFile()); - ZipEntry zipJarEntry = zipFile.getEntry(this.extract); + ZipFile zipFile = new ZipFile(getTemporaryFile(String.format(PROGUARD_ZIP, proguardVersion)).toFile()); + ZipEntry zipJarEntry = zipFile.getEntry(String.format("proguard-%s/lib/proguard.jar", proguardVersion)); write(zipFile.getInputStream(zipJarEntry), proguardJar); zipFile.close(); } } - private String getJavaBinPathForProguard() throws Exception { - String path; - try { - path = findJavaPathByGradleConfig(); - if (path != null) return path; - } catch (Exception ex) { - System.err.println("Unable to find java by javaCompile options"); - ex.printStackTrace(); - } - - path = findJavaByGradleCurrentRuntime(); - if (path != null) return path; - - try { - path = findJavaByJavaHome(); - if (path != null) return path; - } catch (Exception ex) { - System.err.println("Unable to find java by JAVA_HOME"); - ex.printStackTrace(); - } - - throw new Exception("Unable to find java to determine ProGuard libraryjars. Please specify forkOptions.executable in javaCompile," + - " JAVA_HOME environment variable, or make sure to run Gradle with the correct JDK (a v1.8 only)"); - } - - private String findJavaByGradleCurrentRuntime() { - String path = Jvm.current().getJavaExecutable().getAbsolutePath(); - System.out.println("Using Gradle's runtime Java for ProGuard"); - return path; - } - - private String findJavaByJavaHome() { - final String javaHomeEnv = System.getenv("JAVA_HOME"); - if (javaHomeEnv != null) { - String path = Jvm.forHome(new File(javaHomeEnv)).getJavaExecutable().getAbsolutePath(); - System.out.println("Detected Java path by JAVA_HOME"); - return path; - } - return null; - } + private JavaLauncher getJavaLauncherForProguard() { + var toolchains = getProject().getExtensions().getByType(JavaToolchainService.class); + var toolchain = toolchains.launcherFor((spec) -> { + spec.getLanguageVersion().set(JavaLanguageVersion.of(getProject().findProperty("java_version").toString())); + }).getOrNull(); - private String findJavaPathByGradleConfig() { - final TaskCollection javaCompiles = super.getProject().getTasks().withType(JavaCompile.class); - - final JavaCompile compileTask = javaCompiles.iterator().next(); - final ForkOptions forkOptions = compileTask.getOptions().getForkOptions(); - - if (forkOptions != null) { - String javacPath = forkOptions.getExecutable(); - if (javacPath != null) { - File javacFile = new File(javacPath); - if (javacFile.exists()) { - File[] maybeJava = javacFile.getParentFile().listFiles((dir, name) -> name.equals("java")); - if (maybeJava != null && maybeJava.length > 0) { - String path = maybeJava[0].getAbsolutePath(); - System.out.println("Detected Java path by forkOptions"); - return path; - } - } - } + if (toolchain == null) { + throw new IllegalStateException("Java toolchain not found"); } - return null; - } - private boolean validateJavaVersion(String java) { - //TODO: fix for j16 -// final JavaVersion javaVersion = new DefaultJvmVersionDetector(new DefaultExecActionFactory(new IdentityFileResolver())).getJavaVersion(java); -// -// if (!javaVersion.getMajorVersion().equals("8")) { -// System.out.println("Failed to validate Java version " + javaVersion.toString() + " [" + java + "] for ProGuard libraryjars"); -// // throw new RuntimeException("Java version incorrect: " + javaVersion.getMajorVersion() + " for " + java); -// return false; -// } -// -// System.out.println("Validated Java version " + javaVersion.toString() + " [" + java + "] for ProGuard libraryjars"); - return true; + return toolchain; } private void generateConfigs() throws Exception { @@ -284,13 +215,8 @@ private void cleanup() { } catch (IOException ignored) {} } - public void setUrl(String url) { - this.url = url; - } - - - public void setExtract(String extract) { - this.extract = extract; + public void setProguardVersion(String url) { + this.proguardVersion = url; } private void runProguard(Path config) throws Exception { @@ -299,39 +225,15 @@ private void runProguard(Path config) throws Exception { Files.delete(this.proguardOut); } - // Make paths relative to work directory; fixes spaces in path to config, @"" doesn't work Path workingDirectory = getTemporaryFile(""); - Path proguardJar = workingDirectory.relativize(getTemporaryFile(PROGUARD_JAR)); - config = workingDirectory.relativize(config); - - // Honestly, if you still have spaces in your path at this point, you're SOL. - Process p = new ProcessBuilder("java", "-jar", proguardJar.toString(), "@" + config.toString()) - .directory(workingDirectory.toFile()) // Set the working directory to the temporary folder] - .start(); + getProject().javaexec(spec -> { + spec.workingDir(workingDirectory.toFile()); + spec.args("@" + workingDirectory.relativize(config)); + spec.classpath(getTemporaryFile(String.format(PROGUARD_JAR, proguardVersion))); - // We can't do output inherit process I/O with gradle for some reason and have it work, so we have to do this - this.printOutputLog(p.getInputStream(), System.out); - this.printOutputLog(p.getErrorStream(), System.err); - - // Halt the current thread until the process is complete, if the exit code isn't 0, throw an exception - int exitCode = p.waitFor(); - if (exitCode != 0) { - Thread.sleep(1000); - throw new IllegalStateException("Proguard exited with code " + exitCode); - } + spec.executable(getJavaLauncherForProguard().getExecutablePath().getAsFile()); + }).assertNormalExitValue().rethrowFailure(); } - private void printOutputLog(InputStream stream, PrintStream outerr) { - new Thread(() -> { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { - String line; - while ((line = reader.readLine()) != null) { - outerr.println(line); - } - } catch (Exception e) { - e.printStackTrace(); - } - }).start(); - } } diff --git a/fabric/build.gradle b/fabric/build.gradle index e0d5d8442..caeb009aa 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -78,8 +78,7 @@ components.java { } task proguard(type: ProguardTask) { - url 'https://github.com/Guardsquare/proguard/releases/download/v7.2.1/proguard-7.2.1.zip' - extract 'proguard-7.2.1/lib/proguard.jar' + proguardVersion "7.2.1" compType "fabric" } diff --git a/forge/build.gradle b/forge/build.gradle index 334bd968c..b2c81567e 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -99,8 +99,7 @@ components.java { } task proguard(type: ProguardTask) { - url 'https://github.com/Guardsquare/proguard/releases/download/v7.2.1/proguard-7.2.1.zip' - extract 'proguard-7.2.1/lib/proguard.jar' + proguardVersion "7.2.1" compType "forge" } diff --git a/gradle.properties b/gradle.properties index d3db032c2..77516438f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,8 +4,12 @@ mod_version=1.9.3 maven_group=baritone archives_base_name=baritone +java_version=17 + minecraft_version=1.19.4 + forge_version=45.0.43 + fabric_version=0.14.11 nether_pathfinder_version=1.4.1 \ No newline at end of file diff --git a/tweaker/build.gradle b/tweaker/build.gradle index 724c50d29..d6662a5de 100644 --- a/tweaker/build.gradle +++ b/tweaker/build.gradle @@ -95,8 +95,7 @@ jar { } task proguard(type: ProguardTask) { - url 'https://github.com/Guardsquare/proguard/releases/download/v7.2.1/proguard-7.2.1.zip' - extract 'proguard-7.2.1/lib/proguard.jar' + proguardVersion "7.2.1" } task createDist(type: CreateDistTask, dependsOn: proguard)