Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gradle 渐变开始脚本:隐藏控制台_Gradle - Fatal编程技术网

Gradle 渐变开始脚本:隐藏控制台

Gradle 渐变开始脚本:隐藏控制台,gradle,Gradle,我正在为我的Java应用程序使用Gradle 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 al

我正在为我的Java应用程序使用Gradle

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)
  }
}

modifyFile
是作者。

这是一篇旧文章,但我无意中发现了同样的问题

Matthias Braun在回答中对开始脚本的修改是好的,但是我认为通过以下方式进行修改更为简洁:

  • 使用指出的修改修改windows的默认模板(使用
    javaw.exe
    并修改启动命令使控制台静音)

  • 然后修改startScript模板,而不是就地修改生成的脚本:这可以按如下所示完成:

    星条旗{ def tplName='windowsStartScriptWithoutConsoleTemplate.txt' assert project.file(tplName).exists() unixStartScriptGenerator.template=resources.text.fromFile(tplName) }

显然,这并没有在控制台存在的情况下添加第二个startScript,但对我来说这不是必需的

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
}