Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Gradle Dependency Management

You're reading from   Gradle Dependency Management

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781784392789
Length 188 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Hubert Klein Ikkink Hubert Klein Ikkink
Author Profile Icon Hubert Klein Ikkink
Hubert Klein Ikkink
Arrow right icon
View More author details
Toc

Optional Ant task dependencies

We can reuse the existing Ant tasks in Gradle. The default tasks from Ant can be invoked from within our build scripts. However, if we want to use an optional Ant task, we must define a dependency for the classes needed by the optional Ant task. We create a new dependency configuration, and then we add a dependency to this new configuration. We can reference this configuration when setting the classpath for the optional Ant task.

Let's add the optional Ant task SCP for the secure copying of files to/from a remote server. We create the sshAntTask configuration to add dependencies for the optional Ant task. We can choose any name for the configuration. To define the optional task, we use the taskdef method from the internal ant object. The method takes a classpath attribute, which must be the actual path of all files of the sshAntTask dependencies. The Configuration class provides the asPath property to return the path to the files in a platform-specific way. So, if we use this on a Windows computer, the file path separator is a ; and for other platforms it is a :. The following example build file contains all the code to define and uses the SCP Ant task:

configurations {
  // We define a new dependency configuration.
  // This configuration is used to assign
  // dependencies to, that are needed by the
  // optional Ant task scp.
  sshAntTask
}

repositories {
  // Repository definition to download dependencies.
  jcenter()
}

dependencies {
  // Define external module dependencies
  // for the scp Ant task.
  sshAntTask(group: 'org.apache.ant', 
        name: 'ant-jsch', 
        version: '1.9.4')
}

// New task that used Ant scp task.
task copyRemote(
  description: 'Secure copy files to remote server') << {

  // Define optional Ant task scp.
  ant.taskdef(
    name: 'scp',
    classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',

    // Set classpath based on dependencies assigned
    // to sshAntTask configuration. The asPath property
    // returns a platform-specific string value
    // with the dependency JAR files.
    classpath: configurations.sshAntTask.asPath)

  // Invoke scp task we just defined.
  ant.scp(
    todir: 'user@server:/home/user/upload',
    keyFile: '${user.home}/.ssh/id_rsa',
    passphrase: '***',
    verbose: true) {
    fileset(dir: 'html/files') {
      include name: '**/**'
    }
  }
}
You have been reading a chapter from
Gradle Dependency Management
Published in: Jun 2015
Publisher:
ISBN-13: 9781784392789
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime