Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Copy files and expand only some of them and/or ignore dollar signs in others

Tags:

gradle

I have a tree of files that I'd like to copy with Gradle, and for some of the files (e.g. ending in .txt), I'd like to do some property substitions. For example, I have:

task "copyAndroidAssets$flavor" (type: Copy, 
   dependsOn: ["cleanAndroidAssets", "copyAndroidRes$flavor"] ) {
   from "build/assets/${flavorLc}/release/"
   into '../android/assets'
   expand ( versionName: myVersionName, versionCode: myVersionCode )
}

The problem is that some of the files in the tree being copied have dollar signs ($) in them that have nothing to do with property expansion, and this creates the error SimpleTemplateScript6.groovy: 1: illegal string body character after dollar sign;.

In my specific scenario, all the files except one can simply be copied as is. Only a single file, about.txt, needs to have values substituted. Any suggestions on a simple way to do this?

like image 860
mm2001 Avatar asked Aug 06 '14 04:08

mm2001


People also ask

How do I create a tar file in Gradle?

You can run gradle distZip to package the main distribution as a ZIP, or gradle distTar to create a TAR file. To build both types of archives just run gradle assembleDist . The files will be created at $buildDir/distributions/${project.name}-${project. version}.

What is Gradle user home?

The Gradle user home directory ( $USER_HOME/.gradle by default) is used to store global configuration properties and initialization scripts as well as caches and log files.

What is setting Gradle?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.

What is the build Gradle file?

“build. gradle” are scripts where one can automate the tasks. For example, the simple task to copy some files from one directory to another can be performed by the Gradle build script before the actual build process happens.


2 Answers

Now (Gradle 2.3.1) there is a better solution: https://issues.gradle.org/browse/GRADLE-1566

processResources {
    inputs.property('version', version)
    filesMatching("**/version.properties") {
        expand version: version
    } }

In my case I want project properties expansion only in yaml files:

processResources {
    filesMatching("**/*.yaml") {
        expand project.properties
    }
}
like image 134
Boris Lopez Avatar answered Nov 14 '22 00:11

Boris Lopez


Thanks to the link from Opal in the comments, I found a solution. The link shows that it is possible to have multiple from sources and each of these can have a separate expand treatment. Thus, in my case, I only wanted to expand .txt files, so I split the from into two parts using include & exclude as follows:

task "copyAndroidAssets$flavor" (type: Copy, 
    dependsOn: ["cleanAndroidAssets", "copyAndroidRes$flavor"] ) {
    from ("build/assets/${flavorLc}/release/") {
        include '**/*.txt'
        expand ( versionName: versionName, versionCode: versionCode )
    }
    from ("build/assets/${flavorLc}/release/") {
        exclude '**/*.txt'
    }
    into '../android/assets'
    expand ( versionName: myVersionName, versionCode: myVersionCode )
}
like image 37
mm2001 Avatar answered Nov 14 '22 01:11

mm2001