Sunday, June 29, 2014

Apache Ant vs Gradle : The Copy Task

In Apache Ant, if you wanted to make a copy of a file you would do the following:
<copy file = "oldAnt.txt" tofile = "newAnt.txt"/>
Here's how you would do the same thing in Gradle:
copy {
   from "oldGradle.txt"
   into "."

   rename "oldGradle.txt", "newGradle.txt"
}
Alternatively, you could also use the following Groovy one-liner:
new File("newGroovy.txt").text = new File("oldGroovy.txt").text
And just for completeness, here's the Java 7 version:
import java.nio.file.Files

Files.copy(
   file("oldJava.txt").toPath(),
   file("newJava.txt").toPath()
)

In Apache Ant, if you wanted to make a copy of a directory you would do the following:
<copy todir="./newDir">
   <fileset dir = "./oldDir" />
</copy>
Here's how you would do the same thing in Gradle:
copy {
   from "./oldDir"
   into "./newDir"
}

Suppose you only wanted to copy the “.java” files in a directory.

Here's how you would do that in Apache Ant:
<copy todir="./newDir">
   <fileset dir = "./oldDir" includes = "**/*.java" />
</copy>
Here's how you would do the same thing in Gradle:
copy {
   from "./oldDir"
   into "./newDir"

   include "**/*.java"
}
Note: For whatever reason if a subfolder of the source directory is named src, Apache Ant will ignore it.  Gradle, on the other hand, makes no such distinction.

Suppose you wanted to copy all the “.java” files except the ones in a specific directory.

Here's how you would do that in Apache Ant:
<copy todir="./newDir">
   <fileset dir = "./oldDir" includes = "**/*.java" excludes = "**/skip/" />
</copy>
Here's how you would do the same thing in Gradle:
copy {
   from "./oldDir"
   into "./newDir"

   include "**/*.java"
   exclude "**/skip/"
}

References

No comments:

Post a Comment