lapots
lapots

Reputation: 13415

gradle task which works with files

I am trying to write task that works with files, based on gradle-watch-plugin. (Though it does not now. It supposed to run when I start threadWatch task I presume)

However I don't get how to pass and get passed filenames inside task.

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'groovy'
apply plugin: 'com.bluepapa32.watch'

watch {
    rules {
        files fileTree(dir: '/src/main/resources', include: '*.xls')
        tasks 'validateRules'
    }
}

....

dependencies {
    compile "org.codehaus.groovy:groovy-all:2.0.5"

    compile ("org.drools:drools-core:${droolsVersion}")
    compile ("org.drools:drools-compiler:${droolsVersion}")
    compile ("org.drools:drools-decisiontables:${droolsVersion}")
}

import org.drools.builder.*
import org.drools.io.ResourceFactory

task validateRules(type: DefaultTask) {

    ext.ruleValidator = { 
        xls_file_name ->
            try {
                def tConfig = KnowledgeBuilderFactory.newDecisionTableConfiguration().with {
                    tConfig.inputType = DecisionTableInputType.XLS
                    def builder = KnowledgeBuilderFactory.newKnowledgeBuilder().with {
                        builder.add(
                            ResourceFactory.newInputStreamResource(new FileInputStream(xls_file_name)),
                            ResourceType.DTABLE,
                            tConfig
                        )
                        if (builder.hasErrors()) {
                            println 'Invalid ${xls_file_name} rule table!'
                        }
                    }
                    table.add rFactory
                }
            } catch (exc) {
                println exc.getMessage()
            }
    }

    doLast {
        ruleValidator()
    }
}

task watchThread() << {
    Thread.start {
        project.tasks.watch.execute()
    }
}

According to Gradle Watch Plugin I need to define watch configuration with needed task and file list.

However it did not described how task should look like and how pass parameters there -> So I created task, based on DefaultTask , which needs to acquire file list.

However I don't know to get it there because I don't understand how watch plugin is passing there. At all.

Upvotes: 0

Views: 343

Answers (1)

ysb33r
ysb33r

Reputation: 21

Firstly, since Gradle 2.5 there is a continuous build mode (-t on command-line), which means you can forego the use of the watch plugin in most cases. You should consider upgrading your build to at least 2.5 if it is possible.

Now as to the code itself, there is a couple of things you can do better.

[1] You seem to have mixed task configuration with task action.

task ValidateRules << {
    // Given that you are assuming a single XLS file you can do
    // (It will throw exception if there is more than one)
    def xls_file_name = inputs.files.singleFile

    // ... rest of your code inside the try block can follow here
    // (I would not catch the exception, but rather let Gradle handle it)
}

[2] Now configure your custom task to act upon a change in inputs

ValidateRules {
  inputs.files fileTree(dir: '/src/main/resources', include: '*.xls')
}

Upvotes: 1

Related Questions