Note that there are some explanatory texts on larger screens.

plurals
  1. POInheritance Tree for Task Types in Gradle
    primarykey
    data
    text
    <p>How do I cleanly separate tasks that might require two different configuration tasks in Gradle? I'm attempting to separate the actual tasks that I want to execute in a buildSrc/dbhelpertasks.gradle file from the parent build.gradle file. build.gradle will contain partially configured tasks that are used in dbhelpertasks.gradle.</p> <p>I have a number of different databases that I want to connect and execute SQL on so I created a SQLServerTask that takes a database name and URL. </p> <p>Custom SQLServerTask.groovy</p> <pre> import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction import groovy.sql.Sql class SQLServerTask extends DefaultTask { def hostname def database def port = 1433 def username def password def sql def sqlServerDriver = 'net.sourceforge.jtds.jdbc.Driver' @TaskAction def executeSql() { url = hostname + ":" + port + "/" + database databaseConnProps = [user: username, password: password] def sqlInstance = Sql.newInstance(url, databaseConnProps, sqlServerDriver) println "I would execute " + sql } } </pre> <p>I then created a task in my build.gradle that would configure the SQLServerTask with some of the properties. </p> <p>build.gradle</p> <pre> import groovy.sql.Sql apply from: 'buildSrc/dbhelpertasks.gradle' repositories { mavenCentral() } configurations { driver } dependencies { driver group: 'net.sourceforge.jtds', name: "jtds", version: "1.2.4" } //Load up all the drivers for use in this project URLClassLoader loader = GroovyObject.class.classLoader configurations.driver.each {File file -> loader.addURL(file.toURL()) } task contentDbTask(type: SQLServerTask) { println "Configuring content db task" hostname = contentDbUrl database = contentDbName username = contentDbUserName password = contentDbPassword } </pre> <p>The dbhelpertasks.gradle file contains the final configuration (SQL Statement) of the actual tasks that I want to execute.</p> <p>buildSrc/dbhelpertasks.gradle</p> <pre> task getSiteParams(type: contentDbTask) { println "Configuring Site Params Task" sql = "SELECT * FROM CMS_SITE_PARAM" } </pre> <p>When I execute <code>gradle getSiteParams</code> it ends up failing </p> <pre> * What went wrong: A problem occurred evaluating script. > Could not find property 'contentDbTask' on root project 'shipyard'. </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload