Gradle start脚本:隐藏控制台

时间:2014-12-14 18:59:07

标签: gradle

我正在使用Gradle application plugin作为我的Java应用程序。

启动脚本Gradle生成的工作正常但我更喜欢在用户启动应用程序时不会弹出控制台。

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:2)

通过修改启动脚本,我得到了我想要的东西(目前仅适用于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.&copyFile
}

modifyFileBill K撰写。

答案 1 :(得分:0)

这是一篇旧文章,但是我偶然遇到了同样的问题。

Matthias Braun对答案中的开始脚本所做的修改是好的,但是我认为以下列方式这样做更干净:

很明显,这也不会在存在控制台的情况下添加第二个startScript,但是对我而言这不是必需的。