如何在另一个文件中使用我项目中的一个文件? [英] How can I use one file from my project in another file?
问题描述
简单的问题.我是 Clojure 的新手.
如何在另一个文件中使用我项目中的一个文件?基本上我如何包含、导入或需要另一个文件?不是来自图书馆,而是来自我自己的代码.
谢谢,亚历克斯
通常你会希望使用与库代码相同的方法,即use
/require
您的命名空间(通过文件顶部的 ns
表单,有时通过 REPL 中的 use
/require
函数).为此,您必须确保它们在类路径上.一个简短的指南:
遵循通常的 Clojure 项目结构:包含所有源文件的
src/
目录,其中文件src/foo/bar/baz.clj
定义了一个名为foo.bar.baz
的命名空间.注意你必须维护目录结构/命名空间名称结构的对应关系;否则事情就行不通了.另请注意,不得在名称空间名称中使用_
字符或在文件名中使用-
字符(连字符),并且无论何时使用_
在文件名中,您必须在命名空间名称中使用-
(反之亦然.) 最后,Maven 项目的目录层次结构会稍微复杂一些,但不要担心暂时就这个问题(除非你已经是 Maven 的熟练用户,在这种情况下这对你来说不是问题).另见这个我的回答早先关于使用 Clojure 处理 Java 类路径的 SO 问题,以更详细地逐步说明文件系统层次结构/类路径层次结构对应关系.
如果来自
foo.bar
命名空间的代码需要使用来自foo.quux.baz
命名空间的代码,请执行类似(nsfoo.bar (:require [foo.quux.baz :as baz]))
在foo/bar.clj
中并从baz
调用函数为baz/某些功能
.或者你可以将(:use foo.quux.baz)
放在ns
形式中,而不是直接调用它们(没有命名空间限定符,例如some-function代码>).这与您对库代码所做的完全相同.
使用 REPL 中的项目代码时,请确保在类路径中包含 src/
目录(目录本身,而不是其中的任何文件). 您可能应该考虑使用一些工具为您自动化 REPL 设置(包括类路径管理);Leiningen 非常受 Clojurians 的欢迎,并且有一些插件可以将 Maven 与 Clojure 结合使用.
警告:您的 JVM 启动命令可能(事实上,可能会)识别一个名为 $CLASSPATH
的环境变量.至于它和你的Clojure项目的关系,嗯,基本上应该没有.更有可能的是,您的项目每个都需要不同的类路径,有些可能使用与其他人所需的不兼容的某些 jar 版本(特别是如果您使用 Clojure 1.1——最新的稳定版本——对于某些项目,同时进行试验与其他人的 1.2 快照).因此,管理类路径的正确方法是为每个项目准备一个最小版本并将其传递给 JVM 启动命令.如前所述,您应该花一些时间学习使用一个好的工具(如上面提到的 Leiningen)尽快为您设置类路径,这样您就不需要自己关心了.
(作为旁注,在某些情况下,您可能需要添加的不仅仅是 src/
目录和 jar
到类路径,例如,如果您计划在调用 compile
以生成 .class
文件时,您还必须将目标目录也放在类路径上.不过,这超出了本问题的范围.)>
顺便说一句,我已经用正常"这个词开始了这个答案,因为你也可以使用诸如 load
& 之类的东西.in-ns
将单个命名空间拆分为多个文件.不过,在大多数情况下,这并不是您真正想要做的;只需使用经过深思熟虑的命名空间布局即可.
Simple question. I'm new to Clojure.
How can I use one file from my project in another file? Basically how can I include, import, or require another file? Not from libraries but fro my own code.
Thanks, Alex
Normally you'll want to use the same method that you use with library code, which is to use
/ require
your namespaces (through an ns
form at the top of the file and sometimes the use
/ require
functions at the REPL). For this to work, you have to make sure they are on the classpath. A short guide to that:
Follow the usual Clojure project structure: a
src/
directory containing all your source files, where filesrc/foo/bar/baz.clj
defines a namespace calledfoo.bar.baz
. Note that you must maintain the directory structure / namespace name structure correspondence; things won't work otherwise. Also note that you must not use the_
character in namespace names or the-
character (the hyphen) in filenames and whenever you use_
in filenames you must use a-
in namespace names (and the other way around.) Finally, the directory hierarchy will be slightly more complicated with Maven projects, but don't worry about this for now (unless you're already a proficient user of Maven, in which case this won't be a problem for you).Also see this answer of mine to an earlier SO question about Java classpath handling with Clojure for a more detailed step-by-step explanation of the filesystem hierarchy / classpath hierarchy correspondence.
If your code from the
foo.bar
namespace needs to use code from thefoo.quux.baz
namespace, do something like(ns foo.bar (:require [foo.quux.baz :as baz]))
infoo/bar.clj
and call functions frombaz
asbaz/some-function
. Or you can put(:use foo.quux.baz)
in thens
form instead to call them directly (without the namespace qualifier, e.g.some-function
). That's exactly the same thing as what you'd do for library code.
When working with your project's code from the REPL, make sure you include the src/
directory (the directory itself, not any of the files therein) on the classpath. You should probably consider using some tool to automate the REPL setup (including classpath management) for you; Leiningen is very popular with Clojurians and there are plugins for using Maven with Clojure too.
Warning: Your JVM-launching command might (in fact, probably will) recognise an environment variable called $CLASSPATH
. As for its relationship to your Clojure projects, well, basically there should be none. More likely than not, your projects will require a different classpath each, with some possibly using versions of certain jars incompatible with those required by others (notably if you're using Clojure 1.1 -- latest stable release -- for some projects, while experimenting with 1.2 snapshots with others). Thus the correct way of managing the classpath is to prepare a minimal version for each project and pass that to the JVM launching command. As mentioned previously, you should invest some time in learning to use a good tool (like the above mentioned Leiningen) to set up the classpath for you as soon as possible so you don't need to care about this yourself.
(As a side note, you might have to add more than just the src/
directory and your jar
s to the classpath in some scenarios, e.g. if you plan on calling compile
to produce .class
files, you'll have to put the target directory on the classpath too. That's beyond the scope of this question, though.)
BTW, I've started this answer with the word "normally", because you could also use things like load
& in-ns
to split a single namespace into multiple files. Most of the time this won't be what you really want to do, though; just use a well thought out namespace layout instead.
这篇关于如何在另一个文件中使用我项目中的一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!