How to copy files in Groovy

后端 未结 8 1676
余生分开走
余生分开走 2020-12-13 17:37

I need to copy a file in Groovy and saw some ways to achieve it on the web:

1

new AntBuilder().copy( file:\"$sourceFile.canonicalPath\", 
                  


        
相关标签:
8条回答
  • 2020-12-13 17:46

    If it is a text file, I would go with:

      def src = new File('src.txt')
      def dst = new File('dst.txt')
      dst << src.text
    
    0 讨论(0)
  • 2020-12-13 17:57

    For copying files in Jenkins Groovy

    For Linux:

    try {
    	echo 'Copying the files to the required location'
    	sh '''cd /install/opt/
    	cp /install/opt/ssl.ks /var/local/system/'''					
    	echo 'File is copied successfully'
    }
    catch(Exception e) {
    	error 'Copying file was unsuccessful'
    }
    
    
    
    **For Windows:**
    try {
    	echo 'Copying the files to the required location'
    	bat '''@echo off
    	copy C:\\Program Files\\install\\opt\\ssl.ks C:\\ProgramData\\install\\opt'''				
    	echo 'File is copied successfully'
    }
    catch(Exception e) {
    	error 'Copying file was unsuccessful'
    }

    0 讨论(0)
  • 2020-12-13 17:58

    If you are doing this in code, just use something like:

    new File('copy.bin').bytes = new File('orig.bin').bytes
    

    If this is for build-related code, this would also work, or use the Ant builder.

    Note, if you are sure the files are textual you can use .text rather than .bytes.

    0 讨论(0)
  • 2020-12-13 17:59

    I'm using AntBuilder for such tasks. It's simple, consistent, 'battle-proven' and fun.

    2nd approach is too OS-specific (Linux-only in your case)

    3rd it too low-level and it eats up more resources. It's useful if you need to transform the file on the way: change encoding for example

    4th looks overcomplicated to me... NIO package is relatively new in JDK.

    In the end of the day, I'd go for 1st option. There you can switch from copy to scp task, without re-developing the script almost from scratch

    0 讨论(0)
  • 2020-12-13 18:03

    This is the way using platform independent groovy script. If anyone has questions please ask in the comments.

    def file = new File("java/jcifs-1.3.18.jar")
    this.class.classLoader.rootLoader.addURL(file.toURI().toURL())
    
    def auth_server = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance("domain", "username", "password")
    def auth_local = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance(null, "local_username", "local_password")
    
    def source_url = args[0]
    def dest_url = args[1]
    def auth = auth_server
    
    //prepare source file
    if(!source_url.startsWith("\\\\"))
    {
      source_url = "\\\\localhost\\"+ source_url.substring(0, 1) + "\$" + source_url.substring(1, source_url.length());
      auth = auth_local  
    }
    source_url = "smb:"+source_url.replace("\\","/");
    println("Copying from Source -> " + source_url);
    println("Connecting to Source..");
    
    def source = Class.forName("jcifs.smb.SmbFile").newInstance(source_url,auth)
    println(source.canRead());
    
    
    // Reset the authentication to default
    auth = auth_server
    
    //prepare destination file
    if(!dest_url.startsWith("\\\\"))
    {
      dest_url = "\\\\localhost\\"+ dest_url.substring(0, 1) + "\$" +dest_url.substring(2, dest_url.length());
      auth = auth_local  
    }
    
    def dest = null
    dest_url = "smb:"+dest_url.replace("\\","/");
    println("Copying To Destination-> " + dest_url);
    println("Connecting to Destination..");
    
    dest = Class.forName("jcifs.smb.SmbFile").newInstance(dest_url,auth)
    println(dest.canWrite());
    
    if (dest.exists()){
      println("Destination folder already exists");
    }
    source.copyTo(dest);
    
    0 讨论(0)
  • 2020-12-13 18:06

    If you have Java 7, I would definitely go with

    Path source = ...
    Path target = ...
    Files.copy(source, target)
    

    With the java.nio.file.Path class, it can work with symbolic and hard links. From java.nio.file.Files:

    This class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

    Just as references:

    Copy files from one folder to another with Groovy

    http://groovyconsole.appspot.com/view.groovy?id=8001

    My second option would be the ant task with AntBuilder.

    0 讨论(0)
提交回复
热议问题