Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to modify the start script that gradle generates for the distTar task. I seem to be able to set unixStartScriptGenerator.template as shown below, but when I unpack the script from the tar my changes are not there.

This is my full 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'
}

Not that it should matter, but my template, unixStartScriptTemplate.txt, is mostly the same as the original unixStartScript.txt, but with some extra comments and a line changing JAVACMD.

If there is a better way to set this template, please let me know.

like image 663
Travis Well Avatar asked Oct 18 '22 12:10

Travis Well


1 Answers

This way you add a new task whereas you should rather hook into existing one, try:

tasks.withType(CreateStartScripts) {
  def tplName = 'unixStartScriptTemplate.txt'
  assert project.file(tplName).exists()
  unixStartScriptGenerator.template = resources.text.fromFile(tplName)
}
like image 56
Opal Avatar answered Oct 21 '22 05:10

Opal