leiningen clojure web项目使用本地jar包


leiningen clojure web项目使用本地jar简易方式

简书地址

luminusweb模板创建的clojure web服务端项目如何使用本地的jar。

我的方案

将jar放在项目里的某个目录下,推荐resources下,然后在project.clj里加入引用即可。

:resource-paths ["resources/jars/sheet-clone-0.1.jar"]

使用的时候像跟调用其他第三方的java sdk一样使用

(:import (com.alibaba.excel EasyExcel #_ExcelWriter)
           [com.hczt.sheetclone.utils CloneExcelByPoi]
           [org.apache.poi.xssf.usermodel XSSFWorkbook])

其他方案:

参考Paul大神的文章using-local-jars-with-leiningen
贴一下主要内容。
Create a directory in the project.
mkdir maven_repository.
Add local jars to this repository.
For example, this command adds the jaad-0.8.3.jar file to the maven repository.

mvn install:install-file -Dfile=jaad-0.8.3.jar -DartifactId=jaad -Dversion=0.8.3 -DgroupId=jaad -Dpackaging=jar -DlocalRepositoryPath=maven_repository

Add the following to project.clj

:repositories {"local" ~(str (.toURI (java.io.File. "maven_repository")))}

Now a regular lein deps should work

lein deps
Downloading: jaad/jaad/0.8.3/jaad-0.8.3.pom from local
Transferring 0K from local
[WARNING] *** CHECKSUM FAILED - Error retrieving checksum file for jaad/jaad/0.8.3/jaad-0.8.3.pom - IGNORING

The warning can be ignored, since the jar will be checked into the project and not downloaded from the internet.


2021-04-20 实践补充

事实上,推荐的链接里面讨论区也有对达成uberjar就不起作用的讨论。
今天对接江苏银行的银联支付,对方提供了一个jar包。我们本地调好以后在达成jar启动时,报错了

    at clojure.core$load$fn__6839.invoke(core.clj:6126)
    at clojure.core$load.invokeStatic(core.clj:6125)
    at clojure.core$load.doInvoke(core.clj:6109)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.lang.Var.invoke(Var.java:384)
    at clojure.lang.Util.loadWithClass(Util.java:251)
    at custombackend.core.(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sssoft.jspaypos.JSPayPos
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at clojure.lang.RT.classForName(RT.java:2211)
    at clojure.lang.RT.classForNameNonLoading(RT.java:2224)
    at custombackend.modules.jspaypos.jspaypos_service$loading__6721__auto____58888.invoke(jspaypos_service.clj:1)
    at custombackend.modules.jspaypos.jspaypos_service__init.load(Unknown Source)
    at custombackend.modules.jspaypos.jspaypos_service__init.(Unknown Source)
    ... 126 more

JSPayPos 就是我们在clojure引入的一个class.

(:import (com.sssoft.jspaypos JSPayPos)
           (jspaypos JSPayPosUtil)
           [java.io FileInputStream File])

解决方案

把第三放jar包安装到本地maven仓库,然后在project.clj里当做线上repo的包正常引入,打包也会没有问题的。

  • 安装jar包
    ```shell

    marvin @ 192 in ~/git/redcreation/customplatform/custombackend on git:caisheng x [22:42:21]

    $ mvn install:install-file -DgeneratePom=true -DcreateChecksum=true \

-Dpackaging=jar -Dfile=resources/jars/JSPaypos-1.0.5.jar -DgroupId=com.sssoft.JSPaypos
-DartifactId=JSPaypos -Dversion=1.0.5
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: ‘properties’ (position: START_TAG seen …\n\n … @184:17) @ /Users/marvin/.m2/settings.xml, line 184, column 17
[WARNING]
[INFO] Scanning for projects…
[INFO]
[INFO] ——————< org.apache.maven:standalone-pom >——————-
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ——————————–[ pom ]———————————
[INFO]
[INFO] — maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom —
[INFO] Installing /Users/marvin/git/redcreation/customplatform/custombackend/resources/jars/JSPaypos-1.0.5.jar to /Users/marvin/.m2/repository/com/sssoft/JSPaypos/JSPaypos/1.0.5/JSPaypos-1.0.5.jar
[INFO] Installing /var/folders/5q/9lnj73114r31ncggww5cpqr80000gn/T/mvninstall3708658756963899382.pom to /Users/marvin/.m2/repository/com/sssoft/JSPaypos/JSPaypos/1.0.5/JSPaypos-1.0.5.pom
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 0.317 s
[INFO] Finished at: 2021-04-20T22:43:36+08:00
[INFO] ————————————————————————

* 在依赖中引入
```clojure
[com.sssoft.JSPaypos/JSPaypos "1.0.5"]
  • 安装依赖
    # marvin @ 192 in ~/git/redcreation/customplatform/custombackend on git:caisheng x [22:43:44]
    $ lein deps
    WARNING: You have $CLASSPATH set, probably by accident.
    It is strongly recommended to unset this before proceeding.
    
  • 打包
    lein with-profile test uberjar
    
  • 启动
    java -jar target/uberjar/custombackend.jar
    
    没有报错。

在jenkins等环境发布时,也可以这么做。


评论
  目录