An open API service indexing awesome lists of open source software.

https://github.com/saicone/ezlib

Runtime library/dependency loader & relocator for Java in a single class
https://github.com/saicone/ezlib

dependency-manager java java16 java17 java21 java23 java8

Last synced: 1 day ago
JSON representation

Runtime library/dependency loader & relocator for Java in a single class

Awesome Lists containing this project

README

          

Ezlib

The easy way to load dependencies at runtime.




















Ezlib provides an easy methods to load all needed dependencies at runtime into class loaders.

```java
// 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();
```

## Dependency

How to implement ezlib in your project.

This library contains the following artifacts:

* `ezlib` - The main project, supports:
1. Load dependencies from repositories.
2. Dependency relocation.
3. Dependency file mapper, for example, apply transformations to source code before appending it to classpath.
* `loader` - An extensive library loader (that also use `ezlib` module), supports:
1. Transitive dependencies.
2. Snapshot dependencies.
3. Optional dependencies.
4. Dependency scope filter.
5. Testing dependency classes before loading.
6. Dependency version provided by path, for example, use `@latest` to get the latest version from `maven-metadata.xml`.
7. Dependency loading from configuration file (JSON and YAML are supported by default).
8. Dependency loading from class constant declaration.
9. Conditional functions to filter dependencies based on runtime variables, for example, use `java.version >= 17` to only load on Java +17.
* `annotations` - An annotation processor to declare dependencies and generate resources for `loader` module.
* `internal` - The internal code that actually load the classes on runtime, not made for users.

Project build examples:

build.gradle

```groovy
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

```kotlin
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

```xml


Jitpack
https://jitpack.io

com.saicone.ezlib
ezlib
1.3.3
compile

com.saicone.ezlib
loader
1.3.3
compile

com.saicone.ezlib
annotations
1.3.3
provided

org.apache.maven.plugins
maven-shade-plugin
3.6.1



com.saicone.ezlib:ezlib
com.saicone.ezlib:loader




com.saicone.ezlib
${project.groupId}.ezlib





package

shade


```

## Features

### Easy dependency builder
Ezlib allow you to append dependencies into parent class loader and specify repository before load method.
```java
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();
```

### Package relocation
Ezlib uses [jar-relocator](https://github.com/lucko/jar-relocator), so you can load dependencies with package relocation.

Here an example with Redis library and all the needed dependencies.
```java
Map 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();
```

### Dependency loader
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:
```java
// 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.
```java
public class Main {
public static void main(String[] args) {
new EzlibLoader().load();
}
}
```