# 动态依赖

一般在开发Bukkit插件时,开发者想要引入第三方库基本需要将库打包在插件本体中
这样会使插件显得非常臃肿,所以一种十分“优雅”的加载方式就诞生了

该功能在后续添加的时候会因为cache导致无法运行,需要删除重新加载才能运行

# 单个依赖

@RuntimeDependency(value = "!com.google.code.gson:gson:2.10.1", relocate = ["!com.google.gson","!com.example.library.gson"])
object Example : Plugin()

这样在插件启动的时候就会载入Google的GSON到服务器中

# 方法详解

让我们来看看这个方法有什么参数


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(RuntimeDependencies.class)
public @interface RuntimeDependency {

    /**
     * 依赖地址,格式为:
     * <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
     */
    String value();

    /**
     * 测试类
     * <p>
     * <code>
     * test = "!org.bukkit.Bukkit" // 前面带个感叹号避免在编译时重定向
     * </code>
     */
    String test() default "";

    /**
     * 仓库地址,留空默认使用 <a href="https://maven.aliyun.com/repository/central">阿里云中央仓库</a>
     */
    String repository() default "";

    /**
     * 是否进行依赖传递
     */
    boolean transitive() default true;

    /**
     * 忽略可选依赖
     */
    boolean ignoreOptional() default true;

    /**
     * 忽略加载异常
     */
    boolean ignoreException() default false;

    /**
     * 依赖范围
     */
    DependencyScope[] scopes() default {DependencyScope.RUNTIME, DependencyScope.COMPILE};

    /**
     * 依赖重定向
     * <p>
     * <code>
     * relocate = ["!taboolib.", "!taboolib610."] // 同 test 参数
     * </code>
     */
    String[] relocate() default {};

    /**
     * 是否外部库(不会被扫到)
     */
    boolean external() default true;
}

# 变体 - 多个依赖

@RuntimeDependencies(
    RuntimeDependency(value = "!com.google.code.gson:gson:2.10.1", relocate = ["!com.google.gson","!com.example.library.gson"]),
    RuntimeDependency(value = "!com.github.ben-manes.caffeine:caffeine:2.9.3", relocate = ["!com.github.benmanes.caffeine","!com.example.library.caffeine"])
)
object Example : Plugin()

入门

10分钟

单个依赖

方法详解

变体 - 多个依赖