-
-
Save jlmelville/2bfe9277e9e2c0ff79b6 to your computer and use it in GitHub Desktop.
tasks.withType(CreateStartScripts).each { task -> | |
task.doLast { | |
String text = task.windowsScript.text | |
text = text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" }) | |
task.windowsScript.write text | |
} | |
} |
Thanks. This helped a lot. I was not having a problem with a classpath that was too long, but I did want everything that was in the /lib directory included on may classpath. Here is what I found worked for both linux and windows.
tasks.withType(CreateStartScripts).each { task ->
task.doLast {
task.windowsScript.write task.windowsScript.text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" })
task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "CLASSPATH=\$(echo \$APP_HOME/lib/*.jar | tr ' ' ':')" })
}
}
Thanks! @barrybecker4: Unfortunately, the line
task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "CLASSPATH=\$(echo \$APP_HOME/lib/*.jar | tr ' ' ':')" })
does not work properly when the folder names contain a space character in Unix. Instead, the same idea as for Windows may be used:
task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "${it[1]}*" })
Helpful workaround. A slightly cleaner approach in the same vein is to override the classpath property in the startScripts, which is the pre-configured task of type CreateStartScripts added by the "application" plugin.
startScripts {
classpath = files( '$APP_HOME/lib/*' )
}
The gradle application task handily helps in creating a CLI program from a gradle project. However, in the Windows batch file, the classpath is set by manually specifying every jar on the classpath, e.g.:
and then using that with the
-classpath
switch of your java executable. If you have a large classpath (and let's face it, it's Java, so that's probable), you are in imminent danger of overflowing CMD's puny line length restrictions, resulting in athe input line is too long
error.If you insert the snippet above in your
build.gradle
file, it will use:which works with Java 6 and above.