Class DependencyLoader
DependencyLoader class provides functionality to dynamically download and load external
JAR dependencies at runtime for Bukkit/Spigot/Paper plugins.
It supports downloading files from remote repositories (such as Maven or a custom repository) and loading them into the
plugin’s classpath by using low-level reflection (via Unsafe and Reflector) to modify the internal
structures of the class loader.
There are two main modes for storing the downloaded libraries:
- Complex structure: Uses a Maven-like directory layout (e.g.,
/libraries/com/google/code/gson/gson/2.8.9/gson-2.8.9.jar). - Flat structure: Places the JAR files directly inside the libraries folder (e.g.,
/libraries/gson-2.8.9.jar).
complexStructure flag.
Preconfigured instances:
BUKKIT_LOADERuses Bukkit's world container with a subfolder named "libraries" and locks its structure setting.- Custom loaders can be created via
fromFolder(File, String)orfromFolder(File).
Example usage:
// In your plugin's onLoad or onEnable method:
DependencyLoader loader = DependencyLoader.BUKKIT_LOADER;
loader.load("com.example", "my-library", "${version}", DependencyLoader.MAVEN_REPO_URLS[0], false);
// Alternatively, load dependencies from a YAML configuration file:
loader.loadFromYAML(new File(getDataFolder(), "dependencies.yml"));
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DependencyLoaderA preconfiguredDependencyLoaderthat uses Bukkit's world container and a subfolder named "libraries".static final String[]An array of default Maven repository URLs. -
Method Summary
Modifier and TypeMethodDescriptionstatic DependencyLoaderfromFolder(File librariesFolder) Creates a newDependencyLoaderinstance for the specified folder without a subfolder.static DependencyLoaderfromFolder(File librariesFolder, String folderName) Creates a newDependencyLoaderinstance for the specified folder and subfolder.final booleanOverloaded variant ofthe main load methodthat does not replace existing files and uses the default repository URL.final booleanOverloaded variant ofthe main load methodthat uses the default repository URL.final booleanOverloaded variant ofthe main load methodthat does not replace existing files and uses the default repository URL.final booleanDownloads and loads a dependency JAR file into the plugin's runtime classpath.booleanloadFromConfiguration(org.bukkit.configuration.file.FileConfiguration c) Loads dependencies from a givenFileConfiguration.booleanloadFromFile(File file) Loads dependencies specified in a YAML file.booleanloadFromYAML(me.croabeast.file.YAMLFile file) Loads dependencies specified in aYAMLFileinstance.
-
Field Details
-
BUKKIT_LOADER
A preconfiguredDependencyLoaderthat uses Bukkit's world container and a subfolder named "libraries".Note: The folder structure for this loader is fixed; its
complexStructureflag cannot be changed. -
MAVEN_REPO_URLS
An array of default Maven repository URLs.
-
-
Method Details
-
load
public final boolean load(String group, String artifact, String version, String repoUrl, boolean replace) Downloads and loads a dependency JAR file into the plugin's runtime classpath.The method constructs the dependency path and file name from the group, artifact, and version. It downloads the JAR if it does not exist or if
replaceistrue, and then loads it dynamically via reflection.- Parameters:
group- the dependency group ID.artifact- the dependency artifact ID.version- the dependency version (this can be parameterized via release tags).repoUrl- the base URL of the repository to download the dependency from; ifnull, the first URL fromMAVEN_REPO_URLSis used.replace- iftrue, the dependency will be re-downloaded even if it exists.- Returns:
trueif the dependency was successfully loaded;falseotherwise.
-
load
Overloaded variant ofthe main load methodthat uses the default repository URL.- Parameters:
group- the dependency group ID.artifact- the dependency artifact ID.version- the dependency version.- Returns:
trueif the dependency was successfully loaded;falseotherwise.
-
load
Overloaded variant ofthe main load methodthat does not replace existing files and uses the default repository URL.- Parameters:
group- the dependency group ID.artifact- the dependency artifact ID.version- the dependency version.- Returns:
trueif the dependency was successfully loaded;falseotherwise.
-
load
Overloaded variant ofthe main load methodthat does not replace existing files and uses the default repository URL.- Parameters:
group- the dependency group ID.artifact- the dependency artifact ID.version- the dependency version.- Returns:
trueif the dependency was successfully loaded;falseotherwise.
-
loadFromConfiguration
public boolean loadFromConfiguration(org.bukkit.configuration.file.FileConfiguration c) Loads dependencies from a givenFileConfiguration.The configuration should contain a list of dependency maps under the key
dependencies.- Parameters:
c- theFileConfigurationcontaining dependency definitions.- Returns:
trueif at least one dependency was loaded successfully;falseotherwise.
-
loadFromFile
Loads dependencies specified in a YAML file.The YAML file must have a
.ymlextension and contain dependency definitions.- Parameters:
file- the YAML file containing dependency definitions.- Returns:
trueif dependencies were loaded successfully;falseotherwise.
-
loadFromYAML
public boolean loadFromYAML(me.croabeast.file.YAMLFile file) Loads dependencies specified in aYAMLFileinstance.- Parameters:
file- aYAMLFilecontaining dependency definitions.- Returns:
trueif dependencies were loaded successfully;falseotherwise.
-
fromFolder
Creates a newDependencyLoaderinstance for the specified folder and subfolder.- Parameters:
librariesFolder- the base folder containing dependency libraries.folderName- the subfolder name to use (may benull).- Returns:
- a new
DependencyLoaderinstance.
-
fromFolder
Creates a newDependencyLoaderinstance for the specified folder without a subfolder.- Parameters:
librariesFolder- the base folder containing dependency libraries.- Returns:
- a new
DependencyLoaderinstance.
-