使用ClassGraph 读取webjars 资源文件的内容
2021-04-10 08:29
标签:git des pac asc location mapping java prope reac webjars 是很方便,方式很多时候我们也需要读取内容,ClassGraph 是一个高效的classpath 以及模块扫描器 webjars实际上就是利用了web 容器对于META-INF/resources下资源文件的自动加载处理,制作一个webjars 就是打包 就是基于spring start 的web 项目,很简单 以上是一个简单的内容读取,实际上我们基于webjars 的多版本能力可以灵活的用来进行配置管理(比如配置系统,我们可以灵活的管理) https://github.com/classgraph/classgraph 使用ClassGraph 读取webjars 资源文件的内容 标签:git des pac asc location mapping java prope reac 原文地址:https://www.cnblogs.com/rongfengliang/p/13368657.html
如果查看了webjars 提供的webjars-locator 内部实现也是基于此工具的,但是weebjars 默认提供的功能缺少
内容读取的能力,我们可以基于ClassGraph提供读取文件内容的能力参考webjars 项目
资源到这个路径下
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 引用以及读取内容
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/>
/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() {
ListString> 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));
});
}
MultiValueMapString,String> headers =new HttpHeaders();
headers.add("content-type","text/js");
return new ResponseEntity(resultContents.get(0),headers,HttpStatus.OK);
}
}
说明
同时结合ClassGraph强大高效的classpath 扫描能力,我们可以增强系统的扩展能力参考资料
https://www.webjars.org/
上一篇:Java面向对象
下一篇:Python基础初识
文章标题:使用ClassGraph 读取webjars 资源文件的内容
文章链接:http://soscw.com/index.php/essay/73718.html