Ezlib provides an easy methods to load all needed dependencies at runtime into class loaders.
// Create ezlib with default "libs" folder
Ezlib ezlib = new Ezlib(); // Or specify a folder with new Ezlib(folder);
// Initialize ezlib
ezlib.init();
// Load from maven repository into child class loader
ezlib.dependency("commons-io:commons-io:2.11.0").load();
// Load from maven repository into parent class loader
ezlib.dependency("commons-io:commons-io:2.11.0").parent(true).load();
// Load from specified repository
ezlib.dependency("com.saicone.rtag:rtag:1.3.0").repository("https://jitpack.io/").load();How to implement ezlib in your project.
This library contains the following artifacts:
ezlib- The main project, supports:- Load dependencies from repositories.
- Dependency relocation.
- Dependency file mapper, for example, apply transformations to source code before appending it to classpath.
loader- An extensive library loader (that also useezlibmodule), supports:- Transitive dependencies.
- Snapshot dependencies.
- Optional dependencies.
- Dependency scope filter.
- Testing dependency classes before loading.
- Dependency version provided by path, for example, use
@latestto get the latest version frommaven-metadata.xml. - Dependency loading from configuration file (JSON and YAML are supported by default).
- Dependency loading from class constant declaration.
- Conditional functions to filter dependencies based on runtime variables, for example, use
java.version >= 17to only load on Java +17.
annotations- An annotation processor to declare dependencies and generate resources forloadermodule.internal- The internal code that actually load the classes on runtime, not made for users.
Project build examples:
build.gradle
plugins {
id 'com.gradleup.shadow' version '9.3.1'
}
repositories {
maven { url = uri('https://jitpack.io') }
}
dependencies {
implementation 'com.saicone.ezlib:ezlib:1.3.3'
implementation 'com.saicone.ezlib:loader:1.3.3'
// The annotation module should be implemented as annotation processor
compileOnly 'com.saicone.ezlib:annotations:1.3.3'
annotationProcessor 'com.saicone.ezlib:annotations:1.3.3'
}
jar.dependsOn (shadowJar)
shadowJar {
relocate 'com.saicone.ezlib', project.group + '.ezlib'
}build.gradle.kts
plugins {
id("com.gradleup.shadow") version "9.3.1"
}
repositories {
maven("https://jitpack.io")
}
dependencies {
implementation("com.saicone.ezlib:ezlib:1.3.3")
implementation("com.saicone.ezlib:loader:1.3.3")
// The annotation module should be implemented as annotation processor
compileOnly("com.saicone.ezlib:annotations:1.3.3")
annotationProcessor("com.saicone.ezlib:annotations:1.3.3")
}
tasks {
jar {
dependsOn(tasks.shadowJar)
}
shadowJar {
relocate("com.saicone.ezlib", "${project.group}.ezlib")
}
}pom.xml
<repositories>
<repository>
<id>Jitpack</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!-- Use ezlib -->
<dependency>
<groupId>com.saicone.ezlib</groupId>
<artifactId>ezlib</artifactId>
<version>1.3.3</version>
<scope>compile</scope>
</dependency>
<!-- Use ezlib loader -->
<dependency>
<groupId>com.saicone.ezlib</groupId>
<artifactId>loader</artifactId>
<version>1.3.3</version>
<scope>compile</scope>
</dependency>
<!-- Use annotations -->
<dependency>
<groupId>com.saicone.ezlib</groupId>
<artifactId>annotations</artifactId>
<version>1.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<artifactSet>
<includes>
<include>com.saicone.ezlib:ezlib</include>
<include>com.saicone.ezlib:loader</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.saicone.ezlib</pattern>
<shadedPattern>${project.groupId}.ezlib</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</build>Ezlib allow you to append dependencies into parent class loader and specify repository before load method.
Ezlib ezlib = new Ezlib();
ezlib.init();
// Load from maven repository
ezlib.dependency("commons-io:commons-io:2.11.0").parent(true).load();
// Load from specified repository
ezlib.dependency("com.saicone.rtag:rtag:1.1.0")
.repository("https://jitpack.io/")
.parent(false)
.load();Ezlib uses jar-relocator, so you can load dependencies with package relocation.
Here an example with Redis library and all the needed dependencies.
Map<String, String> map = new HashMap<>();
map.put("com.google.gson", "myproject.path.libs.gson");
map.put("org.apache.commons.pool2", "myproject.path.libs.pool2");
map.put("org.json", "myproject.path.libs.json");
map.put("org.slf4j", "myproject.path.libs.slf4j");
map.put("redis.clients.jedis", "myproject.path.libs.jedis");
Ezlib ezlib = new Ezlib();
ezlib.init();
// Load all the needed dependencies first
ezlib.dependency("com.google.gson:gson:2.8.9").relocations(map).parent(true).load();
ezlib.dependency("org.apache.commons:commons-pool2:2.11.1").relocations(map).parent(true).load();
ezlib.dependency("org.json:json:20211205").relocations(map).parent(true).load();
ezlib.dependency("org.slf4j:slf4j-api:1.7.32").relocations(map).parent(true).load();
// Then load redis dependency
ezlib.dependency("redis.clients:jedis:4.2.2").relocations(map).parent(true).load();Ezlib loader is the easier way to load dependencies and all the needed sub dependencies, so you can use it with annotations.
For example, if MyObject need the redis library:
// Use plain annotation
@Dependency("redis.clients:jedis:4.2.2")
public class MyObject {
}
// Use with relocations
@Dependency(value = "redis.clients:jedis:4.2.2",
relocations = {
"com.google.gson", "myproject.path.libs.gson",
"org.apache.commons.pool2", "myproject.path.libs.pool2",
"org.json", "myproject.path.libs.json",
"org.slf4j", "myproject.path.libs.slf4j",
"redis.clients.jedis", "myproject.path.libs.jedis"
}
)
public class MyObject {
}Then execute ezlib loader on project initialization, all the needed dependencies will be loaded by default.
public class Main {
public static void main(String[] args) {
new EzlibLoader().load();
}
}