Gradle

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
To raise new issues or bugs against Gradle, please use forums.gradle.org.
  • Gradle
  • GRADLE-1354

Groovy's Sql doesn't see a database driver class specified in "buildscripts"

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Open Open
  • Resolution: Unresolved
  • Affects Version/s: 0.9.1
  • Fix Version/s: None

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.

Activity

  • All
  • Comments
  • History
  • Activity
  • TeamCity
  • Commits
  • Source
  • Reviews
Hide
Permalink
Steinar Haugen added a comment - 11/Aug/11 5:40 AM

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.

Show
Steinar Haugen added a comment - 11/Aug/11 5:40 AM 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.

People

  • Assignee:
    Unassigned
    Reporter:
    Thor Kummer
Vote (2)
Watch (1)

Dates

  • Created:
    26/Jan/11 10:45 PM
    Updated:
    11/Aug/11 5:40 AM
  • Atlassian JIRA (v5.0.3#729-sha1:bf569e4)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Gradle. Try JIRA - bug tracking software for your team.