[Application Plugin] How to configure variables in generated scripts?


#1

I am trying to use the Application Plugin for a small application.

However too much modification is needed of the generated start scripts as it stands now. I want to be able to build the distribution and give it directly to the users to run. I was thinking that the generated scripts should check for the presence of another script, e.g. setenv.sh, and execute it if it is there. This is based on what Tomcat does.

The purpose of setenv.sh would be to set things like JAVA_OPTS and JAVA_HOME.

This way I could create a setenv.sh file for a particular environment, do the build, and then give the distribution to the user. Without a setenv.sh I would have to do the build, unzip it, modify the script, rezip it!

Unless I am missing something very obvious…please let me know I’m pretty new to Gradle. I suspected at first that there was something I could set within my build.gradle file in order to customize the script, but I don’t see anything or any examples of that.

Thanks for any help.


(Andrew Oberstar) #2

I haven’t found I nice way myself, but something like this met my needs at the time:

startScripts << {
 def lines = windowsScript.readLines().collect {
  if (it ==~ /^set DEFAULT_JVM_OPTS=/) {
   return 'set DEFAULT_JVM_OPTS=-Dapp.home="%~dp0.." -Dlogback.configurationFile=%~dp0..\config\logback.groovy'
  } else {
   return it
  }
 }
 windowsScript.withPrintWriter { writer -> lines.each { writer.println it } }
}

(Luke Daley) #3

Andrew is right, there is no direct way to do this right now.

I tend to use:

startScripts {
   jvmOpts = "-Xmx1024m"
   inputs.property("jvmOpts", { jvmOpts }) // for incremental build to work properly
    doLast {
     def optsEnvVar = "DEFAULT_JVM_OPTS"
     unixScript.text = unixScript.text.replace("$optsEnvVar=${'""'}", "$optsEnvVar=${'"'}$jvmOpts${'"'}")
     windowsScript.text = windowsScript.text.replace("set $optsEnvVar=", "set $optsEnvVar=$jvmOpts")
   }
}

#4

Thanks for ideas Andrew and Luke! How would you guys feel about doing something like I mentioned similar to Tomcat?

For example in ‘unixStartScript.txt’ around line 68, include this:

CLASSPATH=$classpath
  if [ -r "setenv.sh" ]; then
  . "setenv.sh"
fi

(Luke Daley) #5

I like the idea personally.


#6

Luke,

I created a pull request to update the templates:

https://github.com/gradle/gradle/pull/77

Please take a look when you have a chance.

Thanks.


(Luke Daley) #7

Hi Dlandis,

I’ve created GRADLE-2207 to track this.