问题描述
我在Eclipse中有一个Java项目。它工作正常,我可以使用java -jar sample-app.jar
I have a Java project in Eclipse. It works fine and I can run it from cmd using java -jar sample-app.jar
我现在必须使用Gradle生成可执行文件 sh
文件在Linux中使用。
我已经找到了一些有关创建启动脚本的资源,但是我设法做的最多的就是使用标准的'startScripts'任务来创建sample.app.bat文件。
I now have to use Gradle to produce an executable sh
file for use in Linux.I've found some resources on creating start scripts but the most I've managed to do is use the standard 'startScripts' task to create a sample.app.bat file.
我在Google上逐个问题地搜索问题,试图找到答案,但我运气不佳。我还多次阅读了Gradle Application插件文档,但仍然很困惑。
I've scoured question after question and page after page on google to try and find an answer and I'm not having much luck. I've also read through the Gradle Application plugin documentation a number of times and still quite confused.
如何编写Gradle任务以生成 sh
文件执行我的sample-app.jar文件?
How do I write a Gradle task to generate an sh
file to execute my sample-app.jar file?
注意: sample-app.bat文件不执行。它只会产生一个错误:找不到或加载主类com.sample.MainClass
Note: The sample-app.bat file does no execute. It only produces an "Error: Cannot find or load main class com.sample.MainClass
推荐答案
似乎提到了插件是您要寻找的。,您可以找到一个小样。现在要运行主类,您需要运行 gradle run
。要准备 sh 和 bat 脚本,请运行gradle gradle startScripts
。 (并且易于复制)版本的应用程序并启动脚本,运行 gradle distZip
或 gradle distTar
。这两个任务都将准备并存档。可以提取到给定目录并使用准备好的脚本运行。最后一个任务 gradle installDist
将在给定目录中准备脚本和可执行jar,但不打包它们。
It seems that mentioned application plugin is what you're looking for. Here you can find a little demo. Now to run main class you need to run gradle run
. To prepare sh and bat scripts run gradle gradle startScripts
. To prepare a runnable (and easily copyable) version of application and starts scripts run gradle distZip
or gradle distTar
. Both tasks will prepare and archive that can be extracted to given directory and run with prepared script. The last task gradle installDist
will prepare both script and executable jar in a given directory but without packing them.
使用 dist *
, installDist
和 startScripts
可以在 build
目录下找到。尝试运行:
All artifacts created with dist*
, installDist
and startScripts
can be found under build
directory. Try running:
gradle clean distZip distTar installDist
然后是 tree -a build
以查看所有准备好的归档文件。
and then tree -a build
to view all prepared filed.
要从准备好的jar中运行主类,请记住在正确的目录中执行shell脚本,例如: build / install / lol / bin
。
To run main class from the prepared jar remember to execute shell scripts in the correct directory, e.g.: build/install/lol/bin
.
这篇关于如何使用Gradle生成可执行的.sh文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!