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

2024-01-07

我需要修改 gradle 生成的启动脚本distTar https://docs.gradle.org/current/userguide/distribution_plugin.html任务。我似乎可以设置unixStartScriptGenerator.template https://github.com/gradle/gradle/blob/v3.5.0/subprojects/plugins/src/main/java/org/gradle/api/internal/plugins/DefaultTemplateBasedStartScriptGenerator.java#L58如下所示,但是当我从 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'
}

这并不重要,但我的模板 unixStartScript Template.text 与原始 unixStartScript.txt https://github.com/gradle/gradle/tree/v3.5.0/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins,但有一些额外的注释和一行更改 JAVACMD。

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


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

tasks.withType(CreateStartScripts) {
  def tplName = 'unixStartScriptTemplate.txt'
  assert project.file(tplName).exists()
  unixStartScriptGenerator.template = resources.text.fromFile(tplName)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐