使用ClassGraph 读取webjars 资源文件的内容

webjars 是很方便,方式很多时候我们也需要读取内容,ClassGraph 是一个高效的classpath 以及模块扫描器
如果查看了webjars 提供的webjars-locator 内部实现也是基于此工具的,但是weebjars 默认提供的功能缺少
内容读取的能力,我们可以基于ClassGraph提供读取文件内容的能力

参考webjars 项目

webjars实际上就是利用了web 容器对于META-INF/resources下资源文件的自动加载处理,制作一个webjars 就是打包
资源到这个路径下

  • 项目结构

 

 

 

  • pom.xml
 
<?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>com.dalong-web</groupId>
    <artifactId>webjars</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
        <package-version>1.0</package-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <destinationDir>${project.build.outputDirectory}/META-INF/resources/webjars/dalongdemo/${package-version}</destinationDir>
    </properties>
    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <targetPath>${destinationDir}</targetPath>
            </resource>
        </resources>
    </build>
</project>
  • 构建
mvn clean package install

webjars 引用以及读取内容

  • 项目结构

    就是基于spring start 的web 项目,很简单

  • pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalong</groupId>
    <artifactId>wbjarsapps</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wbjarsapps</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.dalong-web</groupId>
            <artifactId>webjars</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
            <version>4.8.87</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 入口
package com.dalong.wbjarsapps;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.Resource;
import io.github.classgraph.ScanResult;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
@RestController
public class WbjarsappsApplication {
    public static void main(String[] args) {
        SpringApplication.run(WbjarsappsApplication.class, args);
    }
    @RequestMapping(value = {"/r"})
    public  Object listResources() {
         List<String> resultContents = new ArrayList<>();
         ClassGraph classGraph =new ClassGraph();
         try (ScanResult scanResult = classGraph.acceptPaths("META-INF/resources/webjars").scan()) {
            scanResult.getResourcesWithExtension(".js").forEachByteArrayIgnoringIOException((Resource res, byte[] content) -> {
                resultContents.add(new String(content, StandardCharsets.UTF_8));
            });
         }
         MultiValueMap<String,String> headers =new HttpHeaders();
         headers.add("content-type","text/js");
         return new ResponseEntity(resultContents.get(0),headers,HttpStatus.OK);
    }
}
 

说明

以上是一个简单的内容读取,实际上我们基于webjars 的多版本能力可以灵活的用来进行配置管理(比如配置系统,我们可以灵活的管理)
同时结合ClassGraph强大高效的classpath 扫描能力,我们可以增强系统的扩展能力

参考资料

https://github.com/classgraph/classgraph
https://www.webjars.org/

posted on   荣锋亮  阅读(1238)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-07-23 cube.js 最近版本的一些更新
2019-07-23 zeebe 0.20.0 集群部署试用
2019-07-23 redash oracle 数据源docker 镜像
2018-07-23 linuxkit 基本试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示