So I have a bunch of archives in a directory that look like this:
test_61995.zip
test_61234.zip
test_61233.zip
I want to copy only the latest file from here using Gradle. Is is possilbe to sort the files and date and time and copy usng gradle?
Sure, you can do that. Here is an example
Kotlin DSL:
tasks {
val cp by creating(Copy::class.java) {
from(File("/home/madhead/Downloads/").listFiles().sortedBy { it.lastModified() }.last())
into(File("/home/madhead/Downloads/so53777253/"))
}
}
Groovy DSL:
task cp(type: Copy) {
from(new File("/home/madhead/Downloads/").listFiles().sort{ it.lastModified() }[0])
into(new File("/home/madhead/Downloads/so53777253/"))
}
This will copy the latest modified file from /home/madhead/Downloads/ to /home/madhead/Downloads/so53777253/.
Related
I want to untar a file that is in .tar.xz format. Gradle's tarTree() does not support this format, so I need to unzip the .xz to a .tar, then I can make use of it.
According to the docs, i should be able to do something like this:
ant.untar(src: myTarFile, compression: "xz", dest: extractDir)
However, I get an error:
Caused by: : xz is not a legal value for this attribute
at org.apache.tools.ant.types.EnumeratedAttribute.setValue(EnumeratedAttribute.java:94)
This SO answer talks about using the Apache Ant Compress antlib within Maven. How can I achieve a similar result using Gradle?
Converting the Maven SO answer in your link would be something like:
configurations {
antCompress
}
dependencies {
antCompress 'org.apache.ant:ant-compress:1.4'
}
task untar {
ext {
xzFile = file('path/to/file.xz')
outDir = "$buildDir/untar"
}
inputs.file xzFile
outputs.dir outDir
doLast {
ant.taskdef(
resource:"org/apache/ant/compress/antlib.xml"
classpath: configurations.antCompress.asPath
)
ant.unxz(src:xzFile.absolutePath, dest:"$buildDir/unxz.tar" )
copy {
from tarTree("$buildDir/unxz.tar")
into outDir
}
}
}
See https://docs.gradle.org/current/userguide/ant.html
Here's my solution that involves command line utilities.
task untar() {
inputs.property('archiveFile', 'path/to/file.xz')
inputs.property('dest', 'path/to/file.xz')
outputs.dir("${buildDir}/destination")
doLast {
mkdir("${buildDir}/destination")
exec {
commandLine('tar', 'xJf', inputs.properties.archiveFile, '-C', "${buildDir}/destination")
}
}
}
We currently have a Gradle (v4.10.3) build script that compresses every static resource during build time. Below is a snippet of the code that we have:
tasks.register("gzipJsFiles") {
doLast {
fileTree(dir: "${buildDir}/classes/main/static/js", include: "**/*.min.js", exclude: "*.gz").eachWithIndex { file, index ->
def dynamicTask = "gzipJs-$file.name"
task "${dynamicTask}" (type: GzipJsTask) {
source = file
dest = Paths.get(file.absolutePath + ".gz").toFile()
}
tasks."$dynamicTask".execute()
}
}}
Now, with the latest versions of Gradle, the Task.execute() is being deprecated.
Is there a way to achieve the GZip task, to zip every file in file tree, individually with the newer versions of Gradle (5.x or higher)?
I don't know where the GzipJsTask comes from, but if it is the one from gradle-js-plugin, you can see from the source code that it is simply a wrapper around some Ant commands. So instead of creating Gradle tasks dynamically at execution time, which is no longer possible, just run the commands directly:
doLast {
fileTree(dir: "${buildDir}/classes/main/static/js", include: "**/*.min.js", exclude: "*.gz").each { file ->
ant.gzip(src: file.absolutePath, destfile: file.absolutePath + ".gz")
}
}
Is there any way to configure distribution file name when creating archive using distribution plugin in gradle?
Here is my build.gradle (I'm using gradle 2.10) file:
apply plugin: 'distribution'
version '1.2'
distributions {
custom {
baseName = 'someName'
contents {...}
}
}
And calling the task customDistZip creates file: $buildDir/distributions/someName-1.2.zip I would like to have a file name configurd to: someName.zip (without version number).
My workaround for this is to create new task to rename the zip file after creation and use gradle finalizedBy feature:
task renameDist {
doLast {
new File("$buildDir/distributions/someName-${project.version}.zip")
.renameTo("$buildDir/distributions/someName.zip")
}
}
customDistZip.finalizedBy renameDist
But this don't look like elegant and nice solution.
You can either do:
customDistZip.setVersion('')
to set the version string used with baseName to empty or
customDistZip.setArchiveName('someName.zip')
to set the full archive filename, which skips basename alltogether.
Kotlin/Groovy:
distributions {
->distribution<- {
distributionBaseName.set(->name<-)
}
}
I'm currently building a zip in gradle from file referenced in a configuration using the following code:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
exclude { it.file in configurations.common.files }
Additionally I want to add an image file (logo.png), located in the same directory of the build.gradle .
How can I achieve this?
You can simply add it with one more from method call like:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
from ('logo.png')
exclude { it.file in configurations.common.files }
}
I have a gradle project which requires some data files available somewhere on the internet using http. The goal is that this immutable remote file is pulled once upon first build. Subsequent build should not download again.
How can I instruct gradle to fetch the given file to a local directory?
I've tried
task fetch(type:Copy) {
from 'http://<myurl>'
into 'data'
}
but it seems that copy task type cannot deal with http.
Bonus question: is there a way to resume a previously aborted/interrupted download just like wget -c does?
How about just:
def f = new File('the file path')
if (!f.exists()) {
new URL('the url').withInputStream{ i -> f.withOutputStream{ it << i }}
}
You could probably use the Ant task Get for this. I believe this Ant task does not support resuming a download.
In order to do so, you can create a custom task with name MyDownload. That can be any class name basically. This custom task defines inputs and outputs that determine whether the task need to be executed. For example if the file was already downloaded to the specified directory then the task is marked UP-TO-DATE. Internally, this custom task uses the Ant task Get via the built-in AntBuilder.
With this custom task in place, you can create a new enhanced task of type MyDownload (your custom task class). This task set the input and output properties. If you want this task to be executed, hook it up to the task you usually run via task dependencies (dependsOn method). The following code snippet should give you the idea:
task downloadSomething(type: MyDownload) {
sourceUrl = 'http://www.someurl.com/my.zip'
target = new File('data')
}
someOtherTask.dependsOn downloadSomething
class MyDownload extends DefaultTask {
#Input
String sourceUrl
#OutputFile
File target
#TaskAction
void download() {
ant.get(src: sourceUrl, dest: target)
}
}
Try like that:
plugins {
id "de.undercouch.download" version "1.2"
}
apply plugin: 'java'
apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download
task downloadFile(type: Download) {
src 'http://localhost:8081/example/test-jar-test_1.jar'
dest 'localDir'
}
You can check more here: https://github.com/michel-kraemer/gradle-download-task
For me works fine..
The suggestion in Ben Manes's comment has the advantage that it can take advantage of maven coordinates and maven dependency resolution. For example, for downloading a Derby jar:
Define a new configuration:
configurations {
derby
}
In the dependencies section, add a line for the custom configuration
dependencies {
derby "org.apache.derby:derby:10.12.1.1"
}
Then you can add a task which will pull down the right files when needed (while taking advantage of the maven cache):
task deployDependencies() << {
String derbyDir = "${some.dir}/derby"
new File(derbyDir).mkdirs();
configurations.derby.resolve().each { file ->
//Copy the file to the desired location
copy {
from file
into derbyDir
// Strip off version numbers
rename '(.+)-[\\.0-9]+\\.(.+)', '$1.$2'
}
}
}
(I learned this from https://jiraaya.wordpress.com/2014/06/05/download-non-jar-dependency-in-gradle/).
Using following plugin:
plugins {
id "de.undercouch.download" version "3.4.3"
}
For a task which has the purpose of only downloading
task downloadFile(type: Download) {
src DownloadURL
dest destDir
}
For including download option into your task:
download {
src DownloadURL
dest destDir
}
For including download option with multiple downloads into your task:
task downloadFromURLs(){
download {
src ([
DownloadURL1,
DownloadURL2,
DownloadURL3
])
dest destDir
}
}
Hope it helped :)
just now ran into post on upcoming download task on gradle forum.
Looks like the perfect solution to me.. Not (yet) available in an official gradle release though
Kotlin Version of #Benjamin's Answer
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.1")
}
}
tasks.register("downloadPdf"){
val path = "myfile.pdf"
val sourceUrl = "https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf"
download(sourceUrl,path)
}
fun download(url : String, path : String){
val destFile = File(path)
ant.invokeMethod("get", mapOf("src" to url, "dest" to destFile))
}