使用ClassGraph 读取webjars 资源文件的内容
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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/
以上是关于使用ClassGraph 读取webjars 资源文件的内容的主要内容,如果未能解决你的问题,请参考以下文章