如何自动创建批处理/shell 脚本来运行 Java 控制台应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5261316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:18:01  来源:igfitidea点击:

How to automatically create batch / shell scripts to run a Java console application?

javaantmavenbatch-filesh

提问by Eran Medan

I have a Java command-line application, and would like to create an Ant* build script that will create all the required batch/shell scripts to run the application successfully including all the classpath variables. I need it to do the following:

我有一个 Java 命令行应用程序,想创建一个 Ant* 构建脚本,该脚本将创建所有必需的批处理/shell 脚本以成功运行应用程序,包括所有类路径变量。我需要它执行以下操作:

  1. Create a shell script file for Linux/Unix and a batch file for Windows/DOS
  2. Add all classpath dependencies (from Maven or simply use the build path in Eclipse)
  3. Add any necessary boilerplate sh/bat code to run (ENV variables, JAVA_HOME, etc.)
  1. 为 Linux/Unix 创建一个 shell 脚本文件,为 Windows/DOS 创建一个批处理文件
  2. 添加所有类路径依赖项(来自 Maven 或简单地使用 Eclipse 中的构建路径)
  3. 添加任何必要的样板 sh/bat 代码以运行(ENV 变量、JAVA_HOME 等)

I found only a partial answer here.

我在这里只找到了部分答案。

But I haven't found anything that does this basic and trivial task that every build involves.

但是我还没有找到任何可以完成每个构建都涉及的基本而微不足道的任务的东西。

Disclaimer- the original question was Ant/Maven, but I would prefer to see if it can be done in Ant.

免责声明- 最初的问题是 Ant/Maven,但我更愿意看看它是否可以在 Ant 中完成。

采纳答案by khmarbaise

In Maven the best solution for this is the maven-appassembler-pluginwhich handles the creation of a shell script / batch file. In combination with maven-assembly you can create a tar.gz or zip archive which contains everything which is needed.

在 Maven 中,最好的解决方案是maven-appassembler-plugin,它处理 shell 脚本/批处理文件的创建。结合 maven-assembly,您可以创建一个 tar.gz 或 zip 存档,其中包含所需的一切。

回答by Sean Patrick Floyd

Maven knows the dependency:build-classpathgoal, which does most of the dirty work:

Maven 知道dependency:build-classpath目标,它完成了大部分肮脏的工作:

mvn dependency:build-classpath -DoutputFile=cp.txt

You can use this generated file in a shell script to create the java classpath (I know, it ain't much, but it'll get you started).

你可以在 shell 脚本中使用这个生成的文件来创建 java 类路径(我知道,它并不多,但它会让你开始)。

Or you can use the exec-maven-pluginto launch a main class from the current maven context. Something like this will do:

或者你可以使用exec-maven-plugin从当前的 maven 上下文启动一个主类。像这样的事情会做:

mvn compile org.codehaus.mojo:exec-maven-plugin:1.2:java \
    -Dexec.mainClass=com.yourcompany.YourClass

回答by Andy Thomas

One approach is to use a binary executable builder for Java applications. Some of these can be launched from ant with provided ant tasks.

一种方法是为 Java 应用程序使用二进制可执行文件构建器。其中一些可以通过提供的 ant 任务从 ant 启动。

For example, exe4jcan create executables for command-line applications, and supports Windows, Linux and Mac. The executable wraps the Java code, its classpath, and JVM search path. A custom ant task "com.exe4j.Exe4JTask" supports generation of the executable from an exe4J configuration -- which can be created with a friendly wizard.

例如,exe4j可以为命令行应用程序创建可执行文件,并支持 Windows、Linux 和 Mac。可执行文件包装了 Java 代码、它的类路径和 JVM 搜索路径。自定义 ant 任务“com.exe4j.Exe4JTask”支持从 exe4J 配置生成可执行文件——可以使用友好的向导创建。

回答by bhdrkn

Apache commons Launchercreates startup scripts for you. Both Windows and Linux script will be generated. Also it can be integrated with Ant.

Apache commons Launcher为您创建启动脚本。将生成 Windows 和 Linux 脚本。它也可以与 Ant 集成。

I hope it will help others.

我希望它能帮助别人。