API 文档 | CreateStartScripts |
---|
创建用于启动 JVM 应用程序的启动脚本。
示例
task createStartScripts(type: CreateStartScripts) { outputDir = file('build/sample') mainClass = 'org.gradle.test.Main' applicationName = 'myApp' classpath = files('path/to/some.jar') }
注意:Gradle 的 "application"
插件会添加一个预配置的此类型任务,名为 "startScripts"
。
该任务会生成分别面向 Microsoft Windows 环境和类 UNIX 环境(例如 Linux、macOS)的脚本。实际生成由 CreateStartScripts.getWindowsStartScriptGenerator()
和 CreateStartScripts.getUnixStartScriptGenerator()
属性实现,这些属性的类型为 ScriptGenerator
。
示例
task createStartScripts(type: CreateStartScripts) { unixStartScriptGenerator = new CustomUnixStartScriptGenerator() windowsStartScriptGenerator = new CustomWindowsStartScriptGenerator() } class CustomUnixStartScriptGenerator implements ScriptGenerator { void generateScript(JavaAppStartScriptGenerationDetails details, Writer destination) { // implementation } } class CustomWindowsStartScriptGenerator implements ScriptGenerator { void generateScript(JavaAppStartScriptGenerationDetails details, Writer destination) { // implementation } }
默认生成器类型为 TemplateBasedScriptGenerator
,并使用默认模板。可以通过 TemplateBasedScriptGenerator.setTemplate(org.gradle.api.resources.TextResource)
方法更改模板。
此任务使用的默认实现使用 Groovy 的 SimpleTemplateEngine 解析模板,并提供以下变量:
applicationName
- 参见JavaAppStartScriptGenerationDetails.getApplicationName()
。optsEnvironmentVar
- 参见JavaAppStartScriptGenerationDetails.getOptsEnvironmentVar()
。exitEnvironmentVar
- 参见JavaAppStartScriptGenerationDetails.getExitEnvironmentVar()
。moduleEntryPoint
- 模块入口点,如果不存在则为null
。如果存在主类名,也会包含在内,格式为[moduleName]/[className]
。mainClassName
- 主类名,如果不存在通常为""
。出于历史原因,在使用主模块时,这可能被设置为--module [moduleEntryPoint]
。不应依赖此行为,并且可能在未来的版本中移除。entryPointArgs
- 用作命令行参数以进入应用程序的参数,以连接字符串形式表示。它应该插入到程序参数之前。defaultJvmOpts
- 参见JavaAppStartScriptGenerationDetails.getDefaultJvmOpts()
。appNameSystemProperty
- 参见JavaAppStartScriptGenerationDetails.getAppNameSystemProperty()
。appHomeRelativePath
- 相对于脚本自身路径的应用程序主目录路径。classpath
- 参见JavaAppStartScriptGenerationDetails.getClasspath()
。它已编码为连接字符串。modulePath
(不同大小写) - 参见JavaAppStartScriptGenerationDetails.getModulePath()
。它已编码为连接字符串。
编码后的路径预期脚本中存在一个名为 APP_HOME
的变量,该变量设置为应用程序主目录,可以使用 appHomeRelativePath
解析。
示例
task createStartScripts(type: CreateStartScripts) { unixStartScriptGenerator.template = resources.text.fromFile('customUnixStartScript.txt') windowsStartScriptGenerator.template = resources.text.fromFile('customWindowsStartScript.txt') }
属性 | 描述 |
applicationName | 应用程序的名称。 |
classpath | 应用程序的类路径。 |
defaultJvmOpts | 应用程序的默认 JVM 选项。默认为空列表。 |
executableDir | 在分发包中写入脚本的目录。 |
mainClass | 用于启动 Java 应用程序的主类名。 |
mainModule | 用于启动模块化 Java 应用程序的主模块名。 |
optsEnvironmentVar | 用于为 JVM 提供附加选项的环境变量。 |
outputDir | 写入脚本的目录。 |
unixStartScriptGenerator | 类 UNIX 启动脚本生成器。 |
windowsStartScriptGenerator | Windows 启动脚本生成器。 |
String
applicationName
应用程序的名称。
FileCollection
classpath
应用程序的类路径。
String
executableDir
在分发包中写入脚本的目录。
String
optsEnvironmentVar
用于为 JVM 提供附加选项的环境变量。
File
outputDir
写入脚本的目录。