如何从clojure代码构建可执行的jar?与java的一个主要功能在一个程序 [英] how to build executable jar from clojure code? with a main function of java in one program
问题描述
如何从clojure代码构建可执行jar?
How can I build an executable jar from clojure code?
我想让它有一个main函数,以便运行程序。
I want it to have a main function, so that java -jar jarfile.jar runs the program.
推荐答案
正如Erhan Bagdemir回答,你应该使用Leiningen。以下是我在Windows上的操作方式:
As Erhan Bagdemir answered, you should use Leiningen. Here's how I do it on Windows:
您将要下载Leiningen脚本,并把它放在你的 PATH
。如果您的路径上有一个已用于可执行脚本的文件夹,请执行以下操作:
You're going to download the Leiningen script and put it somewhere on your PATH
. If you have a folder already on your path that you use for executable scripts, if not, do the following:
- 下载来自Github的Leiningen软件包。
- 解压缩您的主文件夹(例如
/ Users / vikbehal
) - 将解压缩创建的文件夹添加到
PATH
。为此,打开控制面板,然后转到系统区域,您可以在其中编辑环境变量。通过添加/ Users / vikbehal / lein
(或任何文件夹名称)来编辑系统或用户的PATH
)到PATH
,确保通过分号(;
)将该条目与其他条目分隔开。 - 按
Ok
可保存更改。
- Download the Leiningen package from Github.
- Unzip it in your home folder (e.g.
/Users/vikbehal
) - Add the folder that was created by unzipping to your
PATH
. To do this, open the Control Panel, and go to the System area where you can edit your Environment Variables. Edit either the system or your user'sPATH
value by adding/Users/vikbehal/lein
(or whatever the folder is called) to yourPATH
, making sure to keep that entry separated from others by a semicolon (;
). - Press
Ok
to save your changes.
您可以通过打开命令提示符并运行 lein help
来验证您是否已成功安装Leiningen。你应该看到Leiningen安装一些初始文件。如果您遇到错误,请确保您已正确添加Leiningen所在的文件夹到 PATH
。
You can verify that you've successfully installed Leiningen by opening up a command prompt and running lein help
. You should see Leiningen installing some initial files. If you get an error, make sure you've properly added the folder where Leiningen lives to your PATH
.
Leiningen为您提供了一个良好的默认目录结构,处理依赖关系管理,并确保在运行REPL时正确设置Java的类路径,或者在这种情况下,
Leiningen gives you a good default directory structure, handles dependency management, and ensures that Java's classpath is set properly when running a REPL or, in this case, jarring your application.
以下是Leiningen标准命令的失败:
Here's a run-down of Leiningen standard commands:
# Start a blank project
lein new my-project
# Pull down dependencies defined in your project.clj file
lein deps
# Create a "library" jar of your project
lein jar
# Create an executable jar of your project
lein uberjar
看起来最后一个命令是你想要的,对吗?首先,您需要配置一些东西,以便Leiningen可以正确地构建可执行文件。
It looks like that last command is what you want, right? First, you need to configure a few things, so that Leiningen can build your executable jar properly.
您需要做三件事,使 lein uberjar
正常工作:
You need to do three things to make lein uberjar
work properly:
- 编写一个
-main
函数,作为执行jar文件的入口点。 (注意-
,表示此函数更像是一个Java方法,而不是常规的Clojure函数。) - 请确保在
ns
命名空间声明中添加(:gen-class:main true)
声明 - 添加
:main my.main.namespace
project.clj
- Write a
-main
function that will act as the "entry point" to the execution of your jar file. (Note the-
, it denotes that this function is more like a "Java method" than a regular Clojure function.) - Make sure you add a
(:gen-class :main true)
declaration in thens
namespace declaration at the top of the file that contains your-main
function. - Add a
:main my.main.namespace
entry in yourproject.clj
现在运行 lein uberjar
。在开发时,你可以使用 lein run
,如果你想测试你的应用程序如何作为一个可执行的jar,但不想显式重新jar每次你进行更改。
Now run lein uberjar
. While developing, you can alternatively use lein run
if you want to test how your application would behave as an executable jar, but don't want to explicitly re-jar every time you make a change.
这篇关于如何从clojure代码构建可执行的jar?与java的一个主要功能在一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!