Amen
Amen

Reputation: 80

Gradle does not execute task

So, this is my gradle task. I want to copy some sql-Files from src/main/sql to build/sql while filtering some data based on .properties Files.

import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
    def db_names = ['oracle', 'postgresql', 'h2']
    db_names.each{db -> 
        copy {
            from('src/main/sql') 
            include '*sql'
            def properties = new Properties()
            file('src/main/sql/' + db + '_token.properties').withInputStream{
                properties.load(it);   
            }
            filter(ReplaceTokens, tokens: properties)
            rename 'vorlage', db
            into 'build/sql'
        }
    }
}

Basically this task works fine when I run gradle buildSQL. When I run gradle clean buildSQL the task will not be executed.

What do I have to do so that the task "buildSQL" is running?

Upvotes: 1

Views: 982

Answers (2)

Chriki
Chriki

Reputation: 16338

The task is probably never executed as it doesn’t have any inputs. The only thing that runs is the copy method at build configuration time.

  • When you only run the buildSQL task, then the copy method runs in the configuration phase and the build is done afterwards as there are no tasks with inputs.
  • When you run clean buildSQL, then the copy method runs in the configuration phase and the build executes the clean task in the execution phase – which deletes the copies that were just made.

I’d suggest to read up on Gradle’s build phases if my explanation doesn’t make sense immediately. Maybe running gradle --console=verbose clean buildSQL will shed some more light on what’s going on, too.

Theory aside, here’s how you could fix your configuration:

import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
    def db_names = ['oracle', 'postgresql', 'h2']
    db_names.each { db ->
        def properties = new Properties()
        file("src/main/sql/${db}_token.properties").withInputStream {
            properties.load(it)
        }
        from('src/main/sql') {
            include '*sql'
            filter(ReplaceTokens, tokens: properties)
            rename 'vorlage', db
        }
    }
    into 'build/sql'
}

In other words, just remove the copy method but keep its CopySpec for the Copy task.

Upvotes: 2

Lukas Körfer
Lukas Körfer

Reputation: 14493

@Chriki already explained the (major) problem, so I'll just add a possible solution:

import org.apache.tools.ant.filters.ReplaceTokens

task buildSQL(type: Copy) {
    def db_names = ['oracle', 'postgresql', 'h2']
    db_names.each { db ->
        from('src/main/sql') {
            include '*sql'
            def properties = new Properties()
            file('src/main/sql/' + db + '_token.properties').withInputStream {
                properties.load(it);   
            }
            filter(ReplaceTokens, tokens: properties)
            rename 'vorlage', db
        }
    }
    into 'build/sql'
}

Upvotes: 2

Related Questions