SpringBoot 使用WebJars统一管理静态资源

SpringBoot 使用WebJars统一管理静态资源

经验文章nimo972025-03-12 14:21:036A+A-

原文地址:https://dwz.cn/uIYZCLgW

作者:yizhiwazi

推荐使用Webjars的三大理由:

  • 将静态资源版本化,更利于升级和维护。
  • 剥离静态资源,提高编译速度和打包效率。
  • 实现资源共享,有利于统一前端开发。

学习目标

简单两步!快速学会使用WebJars统一管理前端依赖。

使用教程

一、引入相关依赖

首先在 WebJars官网 找到项目所需的依赖,例如在pom.xml引入 jQuery、BootStrap前端组件等。例如:

  • 版本定位工具:webjars-locator-core 用于省略版本号访问静态资源
  • 前端组件:jquery 、bootstrap
 
 org.webjars
 webjars-locator-core
 
 
 org.webjars
 jquery
 3.3.1
 

二、访问静态资源

好了,就是这么简单,启动系统直接访问静态资源:

快速访问:
http://localhost:8080/webjars/jquery/jquery.js (推荐)

快速访问:
http://localhost:8080/webjars/jquery/3.3.1/jquery.js

三、发布静态资源(私服)

通常很多时候 WebJars官网 并不一定都有我们项目所需的依赖,此时咱们可以根据实际需求将静态资源打包并发布至公司的Maven私服仓库,然后在项目引用即可。

例如将Bootstrap的Metronic静态资源 发布至远程仓库,步骤如下:

1、新建SpringBoot工程 然后在src\main\resources\ 新建目录 META-INF\resources\webjars\metronic 重点来了 这里4.1.9 必须跟POM文件的4.1.9保持一致。

2、修改POM文件 填写项目信息和公司私服地址。



 4.0.0
 
 org.webjars
 metronic
 4.1.9
 jar
 metronic
 metronic
 
 
 
 socks
 https://github.com/yizhiwazi
 
 
 
 
 
 xx-repo
 
http://127.0.0.1:8088/nexus/content/repositories/thirdparty/
 
 
 xx-plugin-repo
 
http://127.0.0.1:8088/nexus/content/repositories/thirdparty/
 
 

3、在本地MAVEN的配置文件指定公司私服的账号密码。



 D:\dev\mvnrepository
 
 
 
 aliyun
 central
 aliyun-all
 http://maven.aliyun.com/nexus/content/groups/public/
 
 
 
 
 repo1
 central
 Human Readable Name for this Mirror.
 http://repo1.maven.org/maven2/
 
 
 
 
 repo2
 central
 Human Readable Name for this Mirror.
 http://repo2.maven.org/maven2/
 
  
 
 
 
 
 
 xx-repo
 admin
 123456
 
 
 
 xx-plugin-repo
 admin
 123456
 
 

4、打开IDEA->Maven->Deploy 将项目到公司私服,大功告成。

点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7