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

How do I change unixStartScriptGenerator.template in the createStartScripts task so that distTar uses my custom template file in build.gradle?

我需要修改 gradle 为 distTar task. I seem to be able to set unixStartScriptGenerator.template 生成的 start 脚本,如下所示,但是当我从 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'
}

这并不重要,但我的模板 unixStartScriptTemplate.txt 与 the original unixStartScript.txt 基本相同,但有一些额外的注释和一行更改 JAVACMD。

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

通过这种方式您可以添加一个新任务,而您应该连接到现有任务,尝试:

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