mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-10-22 12:20:26 +02:00
Fix java 8 support for dependency loader
This commit is contained in:
parent
82c7c49e41
commit
b610b98547
@ -248,7 +248,7 @@
|
||||
<dependency>
|
||||
<groupId>com.github.GeorgeV220</groupId>
|
||||
<artifactId>LibraryLoader</artifactId>
|
||||
<version>v1.6.1</version>
|
||||
<version>v1.7.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -101,16 +101,20 @@ public abstract class SongodaPlugin extends JavaPlugin {
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;apache;commons", "commons-lang3", "3.12.0"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "net;kyori", "adventure-platform-bukkit", "4.1.1"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "net;kyori", "adventure-api", "4.11.0"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;zaxxer", "HikariCP", "4.0.3"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;reactivestreams", "reactive-streams", "1.0.2", false));
|
||||
//dependencies.add(new Dependency("https://repo1.maven.org/maven2", "io;r2dbc", "r2dbc-spi", "1.0.0.RELEASE", false));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;jooq", "jooq", "3.14.16")); //3.19.1
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;mariadb;jdbc", "mariadb-java-client", "3.2.0"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;h2database", "h2", "1.4.200",
|
||||
new Relocation("org;h2", "com;craftaro;third_party;org;h2")) //Custom relocation if the package names not match with the groupdId
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;slf4j", "slf4j-api", "2.0.11"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;zaxxer", "HikariCP", "4.0.3", true,
|
||||
new Relocation("org.slf4j", "com.craftaro.third_party.org.slf4j"))
|
||||
);
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;github;cryptomorin", "XSeries", "9.8.0",
|
||||
new Relocation("com;cryptomorin;xseries", "com;craftaro;third_party;com;cryptomorin;xseries")) //Custom relocation if the package names not match with the groupdId
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;reactivestreams", "reactive-streams", "1.0.2", true));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;jooq", "jooq", "3.14.16", true,
|
||||
new Relocation("org;reactivestreams", "com.craftaro.third_party.org.reactivestreams")) //Relocate reactive-streams to avoid conflicts
|
||||
);
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "org;mariadb;jdbc", "mariadb-java-client", "3.2.0"));
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;h2database", "h2", "1.4.200", false,
|
||||
new Relocation("org;h2", "com;craftaro;third_party;org;h2")) //Custom relocation if the package names not match with the groupId
|
||||
);
|
||||
dependencies.add(new Dependency("https://repo1.maven.org/maven2", "com;github;cryptomorin", "XSeries", "9.8.0", false,
|
||||
new Relocation("com;cryptomorin;xseries", "com;craftaro;third_party;com;cryptomorin;xseries")) //Custom relocation if the package names not match with the groupId
|
||||
);
|
||||
|
||||
//Load plugin dependencies
|
||||
|
@ -2,30 +2,66 @@ package com.craftaro.core.dependency;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Dependency {
|
||||
private final String repositoryUrl;
|
||||
private final String groupId;
|
||||
private final String artifactId;
|
||||
private final String version;
|
||||
private boolean relocate;
|
||||
private final Relocation relocation;
|
||||
private final List<Relocation> relocations;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
* @param version The version of the dependency.
|
||||
*/
|
||||
public Dependency(String repositoryUrl, String groupId, String artifactId, String version) {
|
||||
this(repositoryUrl, groupId, artifactId, version, true);
|
||||
}
|
||||
|
||||
public Dependency(String repositoryUrl, String groupId, String artifactId, String version, boolean relocate) {
|
||||
this(repositoryUrl, groupId, artifactId, version, null);
|
||||
this.relocate = relocate;
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
* @param version The version of the dependency.
|
||||
* @param baseRelocate If the dependency should be relocated to com.craftaro.third_party.
|
||||
*/
|
||||
public Dependency(String repositoryUrl, String groupId, String artifactId, String version, boolean baseRelocate) {
|
||||
this(repositoryUrl, groupId, artifactId, version, baseRelocate, new Relocation[0]);
|
||||
}
|
||||
|
||||
public Dependency(String repositoryUrl, String groupId, String artifactId, String version, @Nullable Relocation relocation) {
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
* @param version The version of the dependency.
|
||||
* @param baseRelocate If the dependency should be relocated to com.craftaro.third_party.
|
||||
* @param extraRelocations Extra relocations to apply to the dependency.
|
||||
*/
|
||||
public Dependency(String repositoryUrl, String groupId, String artifactId, String version, boolean baseRelocate, Relocation... extraRelocations) {
|
||||
this.relocations = new ArrayList<>();
|
||||
this.repositoryUrl = repositoryUrl;
|
||||
this.groupId = groupId.replaceAll(";", ".");
|
||||
this.artifactId = artifactId;
|
||||
this.version = version;
|
||||
this.relocation = relocation;
|
||||
this.relocate = true;
|
||||
if (baseRelocate) {
|
||||
//Add base relocate
|
||||
this.relocations.add(new Relocation(groupId, "com.craftaro.third_party." + groupId));
|
||||
}
|
||||
if (extraRelocations.length > 0) {
|
||||
this.relocations.addAll(Arrays.asList(extraRelocations));
|
||||
}
|
||||
}
|
||||
|
||||
public String getRepositoryUrl() {
|
||||
@ -44,11 +80,11 @@ public class Dependency {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public Relocation getRelocation() {
|
||||
return this.relocation;
|
||||
public List<Relocation> getRelocations() {
|
||||
return this.relocations;
|
||||
}
|
||||
|
||||
public boolean relocate() {
|
||||
return this.relocate;
|
||||
public boolean shouldRelocate() {
|
||||
return this.relocate || !relocations.isEmpty();
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,12 @@ import com.craftaro.core.SongodaPlugin;
|
||||
import com.georgev22.api.libraryloader.LibraryLoader;
|
||||
import com.georgev22.api.libraryloader.exceptions.InvalidDependencyException;
|
||||
import com.georgev22.api.libraryloader.exceptions.UnknownDependencyException;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import me.lucko.jarrelocator.JarRelocator;
|
||||
import me.lucko.jarrelocator.Relocation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
@ -29,6 +31,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipInputStream;
|
||||
@ -36,7 +39,7 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class DependencyLoader {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DependencyLoader.class);
|
||||
private static final Logger logger = Logger.getLogger("CraftaroCore");
|
||||
private static final LibraryLoader libraryLoader = new LibraryLoader(new File("craftaro"));
|
||||
|
||||
public static LibraryLoader getLibraryLoader() {
|
||||
@ -101,7 +104,7 @@ public class DependencyLoader {
|
||||
out.close();
|
||||
|
||||
//Load dependency into the classpath
|
||||
logger.info("[CraftaroCore] Loading dependency " + groupId + ":" + artifactId + ":" + version);
|
||||
logger.info("[CraftaroCore] Downloaded dependency " + groupId + ":" + artifactId + ":" + version);
|
||||
loadJarIntoClasspath(outputFile, dependency);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -109,51 +112,40 @@ public class DependencyLoader {
|
||||
}
|
||||
|
||||
public static void loadJarIntoClasspath(File file, Dependency dependency) {
|
||||
try {
|
||||
if (!isRelocated(file) && dependency.relocate()) {
|
||||
if (!isRelocated(file) && dependency.shouldRelocate()) {
|
||||
logger.info("[CraftaroCore] Loading dependency for relocation " + file);
|
||||
//relocate package to com.craftaro.core.third_party to avoid conflicts
|
||||
List<Relocation> relocations = new ArrayList<>();
|
||||
|
||||
for (com.craftaro.core.dependency.Relocation r : dependency.getRelocations()) {
|
||||
relocations.add(new Relocation(r.getFrom(), r.getTo()));
|
||||
}
|
||||
|
||||
//Relocate the classes
|
||||
File finalJar = new File(file.getParentFile(), file.getName().replace("raw-", ""));
|
||||
JarRelocator relocator = new JarRelocator(file, finalJar, relocations);
|
||||
try {
|
||||
List<Relocation> relocations = new ArrayList<>();
|
||||
//Create a new zip file with the relocated classes
|
||||
ZipEntry entry;
|
||||
String com = "com.";
|
||||
String craftaro = "craftaro.";
|
||||
String third_party = "third_party.";
|
||||
String relocation = com + craftaro + third_party;
|
||||
|
||||
if (dependency.getRelocation() != null) {
|
||||
relocations.add(new Relocation(dependency.getRelocation().getFrom(), dependency.getRelocation().getTo()));
|
||||
} else {
|
||||
//Use artifactId as the relocation
|
||||
relocations.add(new Relocation(dependency.getGroupId(), relocation + dependency.getGroupId()));
|
||||
}
|
||||
|
||||
//Relocate the classes
|
||||
File finalJar = new File(file.getParentFile(), file.getName().replace("raw-", ""));
|
||||
JarRelocator relocator = new JarRelocator(file, finalJar, relocations);
|
||||
try {
|
||||
relocator.run();
|
||||
} catch (Exception e) {
|
||||
logger.error("[CraftaroCore] Failed to relocate dependency " + file, e);
|
||||
}
|
||||
relocator.run();
|
||||
logger.info("[CraftaroCore] Relocated dependency " + file);
|
||||
//Delete the old jar
|
||||
file.delete();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("[CraftaroCore] Failed to relocate dependency " + file, e);
|
||||
logger.severe("[CraftaroCore] Failed to relocate dependency1 " + file);
|
||||
if (e.getMessage().contains("zip file is empty")) {
|
||||
logger.severe("Try deleting the 'server root/craftaro' folder and restarting the server");
|
||||
}
|
||||
e.printStackTrace();
|
||||
//Delete the new jar cuz it's probably corrupted
|
||||
finalJar.delete();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
try {
|
||||
libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
|
||||
} catch (InvalidDependencyException e) {
|
||||
} catch (Exception ignored) {
|
||||
//already loaded
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("[CraftaroCore] Failed to load dependency " + file, e);
|
||||
}
|
||||
logger.info("[CraftaroCore] ----------------------------");
|
||||
}
|
||||
|
||||
private static boolean isRelocated(File file) {
|
||||
|
@ -6,6 +6,12 @@ public class Relocation {
|
||||
private final String to;
|
||||
|
||||
public Relocation(String from, String to) {
|
||||
if (from == null) {
|
||||
throw new IllegalArgumentException("from cannot be null");
|
||||
}
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException("to cannot be null");
|
||||
}
|
||||
this.from = from.replaceAll(";", ".");
|
||||
this.to = to.replaceAll(";", ".");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user