michaelcarrano
michaelcarrano

Reputation: 1316

Issue with Gradle task to write file

I am developing an Android application where I have a directory of JSON files and I want to create a gradle task that will combine all these files into a single JSON file.

This is the gradle task I have so far but does not create the file:

// Task that combines all JSON files in ../libraries into src/main/res/raw/libraries.json
task combineJSonFiles {
    String content = ""
    FileTree tree = fileTree(dir: '../libraries', include: '**/*.json')
    tree.each {File file ->
        content += file.getText()
    }

    println "[" + content.substring(0, content.length()-1) + "]" // prints out the correct contents
    File libraries = file("../app/src/main/res/raw/libraries.json")
    println libraries.getProperties()
}

I print out the properties and I am not sure why these are the property values:

{directory=false, canonicalFile=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw/libraries.json, file=false, freeSpace=0, canonicalPath=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw/libraries.json, usableSpace=0, hidden=false, totalSpace=0, path=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw/libraries.json, name=libraries.json, prefixLength=1, absolute=true, class=class java.io.File, parentFile=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw, absolutePath=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw/libraries.json, absoluteFile=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw/libraries.json, parent=/Users/michaelcarrano/AndroidStudioProjects/detective_droid/app/src/main/res/raw}

Any help is appreciated as I have not seemed to figure this out even after reading the documentation. http://www.gradle.org/docs/current/userguide/working_with_files.html

Upvotes: 3

Views: 6819

Answers (2)

michaelcarrano
michaelcarrano

Reputation: 1316

I am just posting the code for the task that now works:

task combineJSonFiles {
    String content = ""
    FileTree tree = fileTree(dir: '../libraries', include: '**/*.json')
    tree.each {File file ->
        content += file.getText()
    }
    def libraries = new File("app/src/main/res/raw/libraries.json")
    libraries.text = "[" + content.substring(0, content.length()-1) + "]"
}

My issue was trying to use Java.io.File and having the wrong directory path set for my file.

Upvotes: 2

Perryn Fowler
Perryn Fowler

Reputation: 2232

Creating an instance of java.io.File in Groovy/Java does not create the file on disk. You will need to write something to it. Check out this tutorial for working with files in Groovy.

Also you have put your task implementation in a task configuration block, rather than a task action. This means your code will not be running when you are expecting - it will run every time you run gradle rather than when you run this task. You need to put your code in a doLast block

Upvotes: 1

Related Questions