- Notifications
You must be signed in to change notification settings - Fork1
Runtime library/dependency loader & relocator for Java in a single class
License
saicone/ezlib
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Ezlib provides an easy methods to load all needed dependencies at runtime into class loaders.
// Create ezlib with default "libs" folderEzlibezlib =newEzlib();// Or specify a folder with new Ezlib(folder);// Initialize ezlibezlib.init();// Load from maven repository into child class loaderezlib.dependency("commons-io:commons-io:2.11.0").load();// Load from maven repository into parent class loaderezlib.dependency("commons-io:commons-io:2.11.0").parent(true).load();// Load from specified repositoryezlib.dependency("com.saicone.rtag:rtag:1.3.0").repository("https://jitpack.io/").load();
- Minimum Java 8
Take in count ezlib is made to be inside your project, so you must configure it as shaded dependency.
For Gradle Groovy project (build.gradle)
plugins { id'com.gradleup.shadow' version'8.3.5'}repositories { maven { url'https://jitpack.io' }}// Use only ezlibdependencies { implementation'com.saicone.ezlib:ezlib:VERSION'}// Use ezlib loader insteaddependencies { implementation'com.saicone.ezlib:loader:VERSION'// Use annotations compileOnly'com.saicone.ezlib:annotations:VERSION' annotationProcessor'com.saicone.ezlib:annotations:VERSION'}jar.dependsOn (shadowJar)shadowJar { relocate'com.saicone.ezlib', project.group+'.ezlib'}
For Gradle Kotlin project (build.gradle.kts)
plugins { id("com.gradleup.shadow") version"8.3.5"}repositories { maven("https://jitpack.io")}// Use only ezlibdependencies { implementation("com.saicone.ezlib:ezlib:VERSION")}// Use ezlib loader insteaddependencies { implementation("com.saicone.ezlib:loader:VERSION")// Use annotations compileOnly("com.saicone.ezlib:annotations:VERSION") annotationProcessor("com.saicone.ezlib:annotations:VERSION")}tasks { jar { dependsOn(tasks.shadowJar) } shadowJar { relocate("com.saicone.ezlib","${project.group}.ezlib") }}
For Maven project (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>VERSION</version> <scope>compile</scope> </dependency><!-- Use ezlib loader--> <dependency> <groupId>com.saicone.ezlib</groupId> <artifactId>loader</artifactId> <version>VERSION</version> <scope>compile</scope> </dependency><!-- Use annotations--> <dependency> <groupId>com.saicone.ezlib</groupId> <artifactId>annotations</artifactId> <version>VERSION</version> <scope>provided</scope> </dependency></dependencies><build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.3.0</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.
Ezlibezlib =newEzlib();ezlib.init();// Load from maven repositoryezlib.dependency("commons-io:commons-io:2.11.0").parent(true).load();// Load from specified repositoryezlib.dependency("com.saicone.rtag:rtag:1.1.0") .repository("https://jitpack.io/") .parent(false) .load();
Ezlib usesjar-relocator, so you can load dependencies with package relocation.
Here an example with Redis library and all the needed dependencies.
Map<String,String>map =newHashMap();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");Ezlibezlib =newEzlib();ezlib.init();// Load all the needed dependencies firstezlib.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 dependencyezlib.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")publicclassMyObject {}// 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" })publicclassMyObject {}
Then execute ezlib loader on project initialization, all the needed dependencies will be loaded by default.
publicclassMain {publicstaticvoidmain(String[]args) {newEzlibLoader().load(); }}
About
Runtime library/dependency loader & relocator for Java in a single class