TarUpload.groovy 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import org.gradle.api.DefaultTask;
  2. import org.gradle.api.tasks.*;
  3. import org.gradle.api.tasks.bundling.Tar;
  4. import org.gradle.api.tasks.bundling.Compression;
  5. /**
  6. * Extends the Tar task, uploading the created archive to a remote directory, unpacking and deleting it.
  7. * Requires Ant ssh (jsch) support.
  8. */
  9. class TarUpload extends Tar {
  10. @Input
  11. String remoteDir
  12. @Input
  13. Login login
  14. TarUpload() {
  15. compression = Compression.BZIP2
  16. }
  17. @TaskAction
  18. void copy() {
  19. super.copy();
  20. upload();
  21. }
  22. def upload() {
  23. String username = login.username
  24. String password = login.password
  25. String host = login.host
  26. project.ant {
  27. scp(file: archivePath, todir: "$username@$host:$remoteDir", password: password)
  28. sshexec(host: host, username: username, password: password, command: "cd $remoteDir && tar -xjf $archiveName")
  29. sshexec(host: host, username: username, password: password, command: "rm $remoteDir/$archiveName")
  30. }
  31. }
  32. void setLogin(Login login) {
  33. dependsOn(login)
  34. this.login = login
  35. }
  36. }
  37. /**
  38. * Stores login information for a remote host.
  39. */
  40. class Login extends DefaultTask {
  41. @Input
  42. String host
  43. String username
  44. String password
  45. @TaskAction
  46. login() {
  47. project.ant {
  48. input("Please enter the ssh username for host '$host'", addproperty: "user.$host")
  49. input("Please enter the ssh password '$host'", addproperty: "pass.$host")
  50. }
  51. username = ant.properties["user.$host"]
  52. password = ant.properties["pass.$host"]
  53. }
  54. }