使用groovy 下载maven依赖包

主要集成了groovy的grape

maven 依赖

注意groovy的grape需要依赖apache ivy

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>mylocal-test</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>3.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ivy</groupId>
            <artifactId>ivy</artifactId>
            <version>2.5.0</version>
        </dependency>
    </dependencies>
 
</project>

参考代码

private static void demoapp() {
        System.setProperty("grape.root", ".");
        Grape.setEnableAutoDownload(true);
        Grape.setEnableAutoDownload(true);
        Map<String, Object> param = new HashMap<>();
        GroovyClassLoader classLoader = new GroovyClassLoader();
        param.put("classLoader", classLoader); // 注意需要指定classloader
        param.put("group", "com.fasterxml.jackson.core");
        param.put("module", "jackson-databind"); // module 就是maven 的artifactId
        param.put("version", "2.12.5");
        // no need to download transitive dependencies as we only need to check the component itself
        param.put("validate", false);
        param.put("transitive", true);
        Grape.grab(param);
        Arrays.stream(Grape.listDependencies(classLoader)).forEach(new Consumer<Map>() {
            @Override
            public void accept(Map map) {
                System.out.println(map.toString());
            }
        });
}

效果

 

 

参考资料

http://docs.groovy-lang.org/latest/html/documentation/grape.html

posted on 2021-09-21 13:42  荣锋亮  阅读(1323)  评论(0编辑  收藏  举报

导航