如何在 createStartScripts 任务中更改 unixStartScriptGenerator.template 以便 distTar 使用我在 build.gradle 中的自定义模板文件?

Tra*_*ell 5 java software-distribution gradle build.gradle gradle-plugin

我需要修改 gradle 为distTar任务生成的启动脚本。我似乎能够设置 unixStartScriptGenerator.template如下所示,但是当我从 tar 解压缩脚本时,我的更改不存在。

这是我的完整内容build.gradle

apply plugin: 'java'
apply plugin: 'war'

// to use the main method in Main, which uses Jetty
apply plugin: 'application'
mainClassName = 'com.example.Main'
project.version = '2017.0.0.0'

repositories {
  mavenLocal()
  mavenCentral()
}

task createStartScripts(type: CreateStartScripts) {
  // based on https://github.com/gradle/gradle/tree/v3.5.0/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins
  def tplName = 'unixStartScriptTemplate.txt'
  // this succeeds. I tried negating it to ensure that it runs, and it failed, so it's running.
  assert project.file(tplName).exists();
  unixStartScriptGenerator.template = project.file(tplName).newReader() as TextResource
//  windowsStartScriptGenerator.template = resources.text.fromFile('customWindowsStartScript.txt')
}


dependencies {
 // mostly elided
  compile 'org.apache.logging.log4j:log4j-api:2.8.+'
  compile 'org.apache.logging.log4j:log4j-core:2.8.+'
  compile 'de.bwaldvogel:log4j-systemd-journal-appender:2.2.2'

  testCompile "junit:junit:4.12"
}

task wrapper(type: Wrapper) {
  gradleVersion = '3.5'
}
Run Code Online (Sandbox Code Playgroud)

这并不重要,但我的模板 unixStartScriptTemplate.txt与原始 unixStartScript.txt大致相同,但有一些额外的注释和一行更改 JAVACMD。

如果有更好的方法来设置此模板,请告诉我。

Opa*_*pal 3

通过这种方式,您可以添加一项新任务,而您应该挂钩现有任务,请尝试:

tasks.withType(CreateStartScripts) {
  def tplName = 'unixStartScriptTemplate.txt'
  assert project.file(tplName).exists()
  unixStartScriptGenerator.template = resources.text.fromFile(tplName)
}
Run Code Online (Sandbox Code Playgroud)