1. 程式人生 > 實用技巧 >使用ClassGraph 讀取webjars 資原始檔的內容

使用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/