习惯Clojure从运行jar到外部复制资源 [英] Idiomatic Clojure to copy resources from running jar to outside

查看:166
本文介绍了习惯Clojure从运行jar到外部复制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个古典问题,但我找不到任何关于它clojure方式。

It seems like a classical problem but I can't find anything about it "the clojure way".

所以,我有一个foo /目录里面的资源/ (leiningen项目)。当jar'd / uberjar'd时,这个foo /目录被放在jar的根目录下。由于jar中的文件在运行时可能不是物理一致的,因此您不能使用基本复制功能将目录递归地复制到外部世界。

So, I have a foo/ directory inside resources/ (leiningen project). When jar'd/uberjar'd, this foo/ directory is put at the root of the jar. As files inside jar may not be physically consistent at runtime, you can't use basic copy function to recursively copy the directory to the outside world.

Java的几种解决方案世界存在(如何编写一个Java程序,它可以提取一个JAR文件并将其数据存储在指定的目录(位置)?如何从jar资源中提取目录(和子目录)?例如)没有找到任何现有的解决方案Clojure。
作为一个初学者(Clojure和Java),我不知道如何将上面的解决方案翻译成Clojure。逐字逐句翻译从Java到Clojurish Java Interop似乎不对。
有一个官方,clojure惯用的方法来做到这一点吗?

Several solutions for the Java world exist (How to write a Java program which can extract a JAR file and store its data in specified directory (location)? and How to extract directory (and sub directories) from jar resource? for example), but I didn't find any existing solution for Clojure. As a beginner (both Clojure and Java), I'm not sure how to translate above solutions to Clojure. Translating literally line by line from Java to Clojurish Java Interop doesn't seem right. Is there an "official", clojure-idiomatic way to do this ?

注意,我使用Raynes的fs实用程序库。似乎没有一个函数来做这个直接,但也许有一些元素,我可以用来简化的过程? (除了明显的基本糖)

Note that I'm using Raynes' fs utilities library. There doesn't seem to be a function to do this directly, but maybe there's some elements I can use to simplify the process ? (besides the obvious basic io sugar)

推荐答案

我写了 cpath-clj 几个月前,将列出的类路径上的资源通过URI。然后您可以尝试以下操作:

I've written cpath-clj a few months back that will list resources on the classpath by URI. You could then try the following:

(require '[cpath-clj.core :as cp]
         '[clojure.java.io :as io])

(doseq [[path uris] (cp/resources (io/resource "foo"))
        :let [uri (first uris)
              relative-path (subs path 1)
              output-file (io/file output-directory relative-path)]]
  (with-open [in (io/input-stream uri)]
    (io/copy in output-file)))

因为库不是用这个用例写的:

There is some juggling going on since the library was not written with this use case in mind:


  • (cp / resources(io / resource foo))将给你 foo 目录的内容 - 如果你只使用(cp / resourcesfoo)在类路径上找到所有这样的目录,

  • 理论上,可以有多个文件 path ,这就是为什么函数返回多个 uris ;在我们的例子中,只有第一个是感兴趣的。

  • path 始终以斜杠开头, ,我们必须将其移除。

  • (cp/resources (io/resource "foo")) will give you the contents of your foo directory - if you had only used (cp/resources "foo") all such directories on the classpath would have been found,
  • theoretically, there can be multiple files with the same path on the classpath, that's why the function returns multiple uris; in our case, only the first one is of interest.
  • path is always starting with a slash, so to get the relative one, we have to remove it.

也许对您有帮助。

这篇关于习惯Clojure从运行jar到外部复制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆