Quantcast
Channel: Gradle Forums - Latest posts
Viewing all articles
Browse latest Browse all 19850

Lazy evaluation of properties

$
0
0

Using exec in your own task action should work as well. However, I was specifically mentioning that you can continue to use an Exec task, but defer adding the final argument until execution with a task that does nothing other than read the properties and add the arg to the createDatabase task. Something like this would work:

task createClient {
    doLast {
        def customer = project.property('client')
        mkdir "$customer/conf"
        file("$customer/conf/config.properties").write("database=foo")
    }
}

task createDatabase(type: Exec) {
    commandLine = ['CMD', '/C', 'scripts\\db\\CreateNewDB.cmd']
}

task configureCreateDatabase {
    createDatabase.dependsOn it
    dependsOn createClient
    doLast {
        File configFile = file("$rootDir/$client/conf/config.properties")
        Properties props = new Properties()
        if (configFile.exists()) {
            configFile.withInputStream { props.load(it) }
            createDatabase.args props.getProperty('database')
        }
    }
}

You also have the CommandLineArgumentProvider option, which will work as long as you don’t eagerly access the commandLine property:

class DatabaseArgumentProvider implements CommandLineArgumentProvider {
    Iterable<String> asArguments() {
        File configFile = file("$rootDir/$client/conf/config.properties")
        Properties props = new Properties()
        configFile.withInputStream { props.load(it) }
        return Arrays.asList(props.getProperty('database'))
    }
}

task createDatabase(type: Exec) {
    commandLine = ['CMD', '/C', 'scripts\\db\\CreateNewDB.cmd']
    argumentProviders.add(new DatabaseArgumentProvider())   
}

Viewing all articles
Browse latest Browse all 19850

Trending Articles