[GRADLE-1354] Groovy's Sql doesn't see a database driver class specified in "buildscripts" Created: 26/Jan/11  Updated: 10/Feb/17  Resolved: 10/Feb/17

Status: Resolved
Project: Gradle
Affects Version/s: 0.9.1
Fix Version/s: None

Type: Bug
Reporter: Thor Kummer Assignee: Unassigned
Resolution: Won't Fix Votes: 2


 Description   

I specified the PostgresQL driver in the "buildsscripts" closure, as in Example 39.4. "Declaring external dependencies for the build script"

buildscript {
repositories

{ mavenCentral() }
dependencies { classpath group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4' }
}

Gradle -s prints

> 15:29:59-tk@tk:~/workspace/b-one-crm> gradle -s -b build.gradle.groovy deployDb
> :deployDb
>
> FAILURE: Build failed with an exception.
>
> * Where:
> Build file '/home/tk/workspace/b-one-crm/build.gradle.groovy' line: 100
>
> * What went wrong:
> Execution failed for task ':deployDb'.
> Cause: java.lang.ClassNotFoundException: org.postgresql.Driver
>
> * Try:
> Run with -S option to get the full (very verbose) stacktrace.
>
> * Exception is:
> org.gradle.api.tasks.LocationAwareTaskExecutionException: Build file '/home/tk/workspace/b-one-crm/build.gradle.groovy' line: 100
> Execution failed for task ':deployDb'.
> at org.gradle.api.internal.tasks.DefaultTaskExecuter.executeActions(DefaultTaskExecuter.java:63)
> at org.gradle.api.internal.tasks.DefaultTaskExecuter.execute(DefaultTaskExecuter.java:41)
> at org.gradle.api.internal.project.taskfactory.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:32)
> at org.gradle.api.internal.project.taskfactory.ExecutionShortCircuitTaskExecuter.execute(ExecutionShortCircuitTaskExecuter.java:50)
> at org.gradle.api.internal.tasks.SkipTaskExecuter.doExecute(SkipTaskExecuter.java:57)
> at org.gradle.api.internal.tasks.SkipTaskExecuter.execute(SkipTaskExecuter.java:35)
> at org.gradle.api.internal.tasks.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:32)
> at org.gradle.api.internal.AbstractTask.execute(AbstractTask.java:231)
> at org.gradle.execution.DefaultTaskGraphExecuter.executeTask(DefaultTaskGraphExecuter.java:167)
> at org.gradle.execution.DefaultTaskGraphExecuter.doExecute(DefaultTaskGraphExecuter.java:160)
> at org.gradle.execution.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:78)
> at org.gradle.execution.TaskNameResolvingBuildExecuter.execute(TaskNameResolvingBuildExecuter.java:161)
> at org.gradle.execution.DelegatingBuildExecuter.execute(DelegatingBuildExecuter.java:54)
> at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:153)
> at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:107)
> at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:75)
> at org.gradle.launcher.Main.execute(Main.java:93)
> at org.gradle.launcher.Main.main(Main.java:42)
> at org.gradle.launcher.GradleMain.main(GradleMain.java:49)
> Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.ClassNotFoundException: org.postgresql.Driver
> at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:445)
> at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:432)
> at org.gradle.api.internal.tasks.DefaultTaskExecuter.executeActions(DefaultTaskExecuter.java:55)
> ... 18 common frames omitted
> Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
> at Db.lookupVersion(/home/tk/workspace/b-one-crm/build.gradle.groovy:100)
> at Db$lookupVersion.callCurrent(Unknown Source)
> at Db.<init>(/home/tk/workspace/b-one-crm/build.gradle.groovy:87)
> at build_gradle_groovy_efff0db1333cb234a677f1a67e718f98$_run_closure1_closure7.doCall(/home/tk/workspace/b-one-crm/build.gradle.groovy:25)
> at build_gradle_groovy_efff0db1333cb234a677f1a67e718f98$_run_closure1.doCall(/home/tk/workspace/b-one-crm/build.gradle.groovy:23)
> ... 21 common frames omitted
>
> BUILD FAILED
>
> Total time: 2.024 secs

The buildscript is pasted below

import groovy.sql.Sql
import java.text.*

mixin()

def dbHost = 'db.example.com'
def application = 'crm'

buildscript {
repositories { mavenCentral() }

dependencies

{ classpath group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4' }

}

task deployDb << {

def databaseVersions = new XmlSlurper().parse('database-versions.xml')
def dbProjectEl = databaseVersions.project.find

{ it.@name as String == application }

dbProjectEl.database.each { database ->

Db db = new Db(database, dbHost)

println "database-scripts/$db.name, db.appliedScript: $db.appliedScript, db.versionScript: $db.versionScript"

("database-scripts/$db.name" as File).listFiles().sort

{ it.name }

.each { script ->

if(script.name.endsWith('.sql') && db.appliedScript < script.name && script.name <= db.versionScript) {

println "Updating script $script"

ant.exec(executable: './x', failonerror: 'true')

{ arg(line: "$db.host $db.user $db.name $script") }

println "successfully deploy database script $script"
}
}
}
}

def mixin() {

Number.metaClass.format <<

{ fmt -> new DecimalFormat(fmt).format(delegate) }

Date.metaClass.format <<

{ fmt -> new SimpleDateFormat(fmt).format(delegate) }

File.metaClass.recurse <<

{ Closure include, Closure action -> delegate.recurse(include, include, action) }

File.metaClass.recurse << { Closure includeDirs, Closure includeFiles, Closure action ->
if(delegate.directory && delegate.name != '.svn' && includeDirs(delegate)) {
delegate.eachFile

{ it.recurse(includeDirs, includeFiles, action) }

} else if(includeFiles(delegate))

{ action(delegate) }

}
}

class Db {
String driver
String host
String port
String name
String user
String password
String url
String versionScript
String appliedScript

Db(db, host) {
this.driver = 'org.postgresql.Driver'
this.host = host
this.port = '5432'
this.password = 'redacted'
this.name = db.@name as String
this.user = extractUser(db)
this.url = "jdbc:postgresql://$host:$port/$name"
this.versionScript = "${new Integer(db.@version as String).format("0000")}.sql"
this.appliedScript = "${lookupVersion().format("0000")}.sql"
}

def extractUser(db) {
def result = db.@user as String
if(! result)

{ def split = (db.@name as String).split(/_/) result = split.length > 1 ? split[1] : split[0] }

return result
}

int lookupVersion() {
def Sql sql = Sql.newInstance(url, user, password, driver)
try

{ return sql.firstRow("SELECT version FROM version").version as int }

catch(org.postgresql.util.PSQLException e) {
if(e.message == 'ERROR: relation "version" does not exist')

{ return 9999 }

throw e
}
}

}

See mailing list thread [gradle-user] Question about the buildscripts own dependencies - "buildscript" for more details.



 Comments   
Comment by Steinar Haugen [ 11/Aug/11 ]

A work-around that works for us is

// configure additional 3rd party libraries that we need to add to the gradle runtime classpath
repositories {
    mavenCentral()
}
configurations {
    gradleRuntime {transitive = false}
}
dependencies {
    gradleRuntime JCONN, DBUNIT
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.gradleRuntime.each { File file ->
    loader.addURL(file.toURL())
    logger.info "Added jar to gradle runtime: $file"
}

What's new in milestone 4 for us (compared with milestone 1, which we recently upgraded from) is that we didn't need the buildscript dependencies when doing this in ms1, but had to add normal buildscript dependencies in addition to this work-around hack in ms4.

Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by Benjamin Muschko [ 10/Feb/17 ]

Thanks again for reporting this issue. We haven't heard back from you after our inquiry from November 15th. We are closing this issue now. Please create an issue on GitHub if you still feel passionate about getting it resolved.

Generated at Wed Jun 30 11:53:26 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.