|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +title: Usage |
| 4 | +description: How to use Ezlib |
| 5 | +--- |
| 6 | + |
| 7 | +:::info |
| 8 | + |
| 9 | +Ezlib uses a gradle-like dependency format to load them. |
| 10 | + |
| 11 | +::: |
| 12 | + |
| 13 | +## Basic |
| 14 | + |
| 15 | +Ezlib provides an easy method to load all needed dependencies at runtime into class loaders. |
| 16 | + |
| 17 | +Here is an example to load dependencies into a child class loader. |
| 18 | + |
| 19 | +```java |
| 20 | +// Create ezlib with default "libs" folder |
| 21 | +Ezlib ezlib = new Ezlib(); |
| 22 | +// Or specify a folder |
| 23 | +Ezlib ezlib = new Ezlib(new File("folder/path")); |
| 24 | + |
| 25 | +// Load from maven repository (default) |
| 26 | +ezlib.load("commons-io:commons-io:2.11.0"); |
| 27 | + |
| 28 | +// Load from specified repository |
| 29 | +ezlib.load("com.saicone.rtag:rtag:1.1.0", "https://jitpack.io/"); |
| 30 | + |
| 31 | +// You can change default repository |
| 32 | +ezlib.setDefaultRepository("repo URL"); |
| 33 | +``` |
| 34 | + |
| 35 | +## Parent ClassLoader |
| 36 | + |
| 37 | +Ezlib allows you to append dependencies into the parent class loader by just setting "true" at the end of the load method. |
| 38 | + |
| 39 | +```java |
| 40 | +Ezlib ezlib = new Ezlib(); |
| 41 | + |
| 42 | +// Load from maven repository (default) |
| 43 | +ezlib.load("commons-io:commons-io:2.11.0", true); |
| 44 | + |
| 45 | +// Load from specified repository |
| 46 | +ezlib.load("com.saicone.rtag:rtag:1.1.0", "https://jitpack.io/", true); |
| 47 | +``` |
| 48 | + |
| 49 | +## Relocation |
| 50 | + |
| 51 | +Ezlib uses [jar-relocator](https://github.com/lucko/jar-relocator), so you can load dependencies with package relocation. |
| 52 | + |
| 53 | +Here an example with Redis library and all the needed dependencies. |
| 54 | + |
| 55 | +```java |
| 56 | +Map<String, String> relocations = new HashMap(); |
| 57 | +relocations.put("com.google.gson", "myproject.path.libs.gson"); |
| 58 | +relocations.put("org.apache.commons.pool2", "myproject.path.libs.pool2"); |
| 59 | +relocations.put("org.json", "myproject.path.libs.json"); |
| 60 | +relocations.put("org.slf4j", "myproject.path.libs.slf4j"); |
| 61 | +relocations.put("redis.clients.jedis", "myproject.path.libs.jedis"); |
| 62 | + |
| 63 | +Ezlib ezlib = new Ezlib(); |
| 64 | + |
| 65 | +// Load all the needed dependencies first |
| 66 | +ezlib.load("com.google.gson:gson:2.8.9", relocations, true); |
| 67 | +ezlib.load("org.apache.commons:commons-pool2:2.11.1", relocations, true); |
| 68 | +ezlib.load("org.json:json:20211205", relocations, true); |
| 69 | +ezlib.load("org.slf4j:slf4j-api:1.7.32", relocations, true); |
| 70 | + |
| 71 | +// Then load redis dependency |
| 72 | +ezlib.load("redis.clients:jedis:4.2.2", relocations, true); |
| 73 | +``` |
| 74 | + |
| 75 | +:::warning |
| 76 | + |
| 77 | +Make sure to relocate the imports during compile time, while excluding the class that you use to load the dependencies because the strings will be relocated too. |
| 78 | + |
| 79 | +::: |
0 commit comments