我正在使用Gradle
application plugin作为我的Java应用程序.
启动脚本Gradle生成的工作正常,但我更喜欢在用户启动应用程序时不会弹出控制台.
有没有办法做到这一点?
最佳答案 通过修改启动脚本,我得到了我想要的东西(目前仅适用于Windows).
的build.gradle:
apply from: "IO.gradle"
// Modify the Windows start script so that no console is shown when the user starts the app.
// This also creates a copy of the original start script in case we want to use the console for debugging
startScripts << {
def startScriptDir = outputDir.getAbsolutePath()
def winStartScript = startScriptDir + "/" + applicationName + ".bat"
def winStartScriptCopy = startScriptDir + "/" + applicationName + "WithConsole.bat"
def overwriteExistingFile = true
copyFile(winStartScript, winStartScriptCopy, overwriteExistingFile)
modifyFile(winStartScript) {
// javaw.exe doesn't have a console
if(it.contains("java.exe")){
return it.replace("java.exe", "javaw.exe")
}
// Command that launches the app
else if(it.startsWith("\"%JAVA_EXE%\" %DEFAULT_JVM_OPTS%")){
return "start \"\" /b " + it
}
// Leave the line unchanged
else{
return it
}
}
}
installApp {
// Include the additional start script
into("bin/"){
from(startScripts.outputDir)
}
}
IO.gradle:
import java.nio.*
import java.nio.file.*
/**
* This will completely re-write a file, be careful.
*
* Simple Usage:
*
* modifyFile("C:\whatever\whatever.txt") {
* if(it.contains("soil"))
* return null // remove dirty word
* else
* return it
* }
*
* The closure must return the line passed in to keep it in the file or alter it, any alteration
* will be written in its place.
*
* To delete an entire line instead of changing it, return null
* To add more lines after a given line return: it + "\n" + moreLines
*
* Notice that you add "\n" before your additional lines and not after the last
* one because this method will normally add one for you.
*/
def modifyFile(srcFile, Closure c) {
modifyFile(srcFile, srcFile, c)
}
def modifyFile(srcFile, destFile, Closure c={println it;return it}) {
StringBuffer ret = new StringBuffer();
File src = new File(srcFile)
File dest = new File(destFile)
src.withReader{reader->
reader.eachLine{
def line=c(it)
if(line != null) {
ret.append(line)
ret.append("\n")
}
}
}
dest.delete()
dest.write(ret.toString())
}
/**
* Copies a file specified at 'origin' to 'destination'.
* If 'overwrite' is set to true any existing file at 'destination' is overwritten (defaults to false).
*/
def copyFile(String origin, String destination, boolean overwrite=false){
Path origPath = Paths.get(origin)
Path destPath = Paths.get(destination)
def fileAtDestination = destPath.toFile()
if(fileAtDestination.exists()){
if(overwrite) {
fileAtDestination.delete()
Files.copy(origPath, destPath)
}
else{
println("Won't overwrite existing file $fileAtDestination")
println("Call 'copyFile(orig, dest, true)' to delete the existing file first")
}
}
else {
// There's no file at the destination yet
Files.copy(origPath, destPath)
}
}
// Define methods visible to other Gradle scripts
ext{
modifyFile = this.&modifyFile
copyFile = this.©File
}
modifyFile由Bill K创作.