Groovy Script for Runing Processes & Commands
Groovy is bringing power and flexibility of Java to its script environment, and making development simpler. Starting a command and runing a new process in groovy is much more pragmatic than java. String class in groovy has execute method to run content of string as in command line. For example:
I implemented a groovy script to run 7-Zip to compress eclipse workspaces into a backup file at specified output path. Path list of workspaces is kept in a *.properties file to easily gather them with using java.util.ResourceBundle; then use output file path and workspace path list as parameter to 7z program. In order to use ResourceBundle without problem, *.properties file must be located in the same directory with groovy script.
"mspaint".execute()
I implemented a groovy script to run 7-Zip to compress eclipse workspaces into a backup file at specified output path. Path list of workspaces is kept in a *.properties file to easily gather them with using java.util.ResourceBundle; then use output file path and workspace path list as parameter to 7z program. In order to use ResourceBundle without problem, *.properties file must be located in the same directory with groovy script.
// TIMESTAMP OF SCRIPT START def timestamp = System.currentTimeMillis() def outputDir = "D:\\Projects\\Backups\\" if(this.args.size() > 0) { //////////////////////////////////////// println "\nOUTPUT PATH: " + args[0] + "\n" //////////////////////////////////////// outputDir = args[0] // CREATING BACKUP REPOSITORY DIRECTORY; IF NOT EXISTS dir = new File(outputDir) if(!dir.exists()){ //////////////////////////////////////// println "CREATING PATH ${outputDir}\n" //////////////////////////////////////// dir.mkdirs() } } // FORMATTING DATE-TIME def now_ = new java.util.Date() def formatter_ = new java.text.SimpleDateFormat("yyyyMMdd_HHmm") def now = formatter_.format(now_) //////////////////////////////////////// println "BACKUP STARTED AT " + now + "\n" //////////////////////////////////////// // RETRIEVING WORKSPACE DIRECTORIES FROM backup.properties def buffer = new java.lang.StringBuffer() java.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("backup") def keys = rb.getKeys() keys.each { key -> //println rb.getString(key) buffer.append(" " + rb.getString(key)) } // PREPARING 7Z COMMAND TO COMPRESS WORKSPACES def dQ = "\"" def _7z_command = dQ + "C:\\Program Files\\7-Zip\\7z.exe" + dQ + " a " def inputs = buffer.toString() def output = outputDir + "\\mywsbackups_" + now + ".7z" def command = _7z_command + dQ + output + dQ + " " + inputs //////////////////////////////////////// println "RUNNING: " + command + "\n" //////////////////////////////////////// // EXECUTING 7Z COMMAND proc = command.execute() // PRINT OUTPUT OF 7Z COMMAND println proc.text //////////////////////////////////////// println "END OF BACKUP - TOOK:" + (System.currentTimeMillis() - timestamp) + " ms\n" ////////////////////////////////////////
Comments
Post a Comment
Thx for reading! Comments are appreciated...