A quick hack I put together for a JPA project in a spring container uses a seperate build-ddl.gradle file (see below for the contents). Since I don't have a hibernate.cfg.xml in my project, but schema-export-task needs one, I generate a temporary hibernate-temp.cfg.xml.

One thing you probably want to change in the script is the line:
def hibprops = "src/hibernate.production.properties"

and the outputfilename in the hbm2ddl call:
hbm2ddl(drop:"true", create:"true", export:"false", format:"true", delimiter:";", outputfilename:"db/schema.production.ddl")





Here the build-ddl.gradle script:
=================================


apply from: 'build.gradle'

defaultTasks 'ddl'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath  group:'org.slf4j',           name:'slf4j-api',        
version:'1.6.1'

        //----------------------
        // any extra dependencies for the buildscript itself....
        //----------------------
    }
}

configurations {
    schemaExportAntTask
}

dependencies {
    schemaExportAntTask  group:'org.slf4j',     name:'slf4j-api',        
version:'1.6.1'
    schemaExportAntTask  group:'org.slf4j',     name:'jcl-over-slf4j',   
version:'1.6.1'
    schemaExportAntTask  group:'org.slf4j',     name:'log4j-over-slf4j', 
version:'1.6.1'
    schemaExportAntTask  group:'org.hibernate', name:'hibernate-core',   
version:'3.6.4.Final'
    schemaExportAntTask  group:'org.hibernate', name:'hibernate-tools',  
version:'3.2.4.GA'
}

task ddl(dependsOn:compileJava) << {
    def tempHibCfgXmlFilename = "build/ddltemp/hibernate-temp.cfg.xml"
    def allClasses = []
    def allClassLoaderUrls = [ sourceSets.main.classesDir.toURI().toURL() ]
    sourceSets.main.classesDir.eachFileRecurse {
        if (it.name ==~ /.*\.class/) {
            def classRelPath = it.absolutePath - 
sourceSets.main.classesDir.absolutePath
            allClasses << 
classRelPath[1..-7].replace(System.properties['file.separator'], '.')
        }
    }
    configurations.runtime.collect { allClassLoaderUrls << it.toURI().toURL() }

    def urlCL = new URLClassLoader(allClassLoaderUrls as URL[], 
getClass().classLoader)
    def hibCfgXml = new HibernateCfgXml()
    allClasses.each {
        try {
if (Class.forName(it, true, urlCL).annotations*.annotationType().collect{it.name}.contains('javax.persistence.Entity')) {
                hibCfgXml.addEntityClass(it)
            }
        } catch (Throwable t) {
            println "Error loading class $it, skipping that one..."
        }
    }
    hibCfgXml.write(tempHibCfgXmlFilename)

def htcp = configurations.schemaExportAntTask + files(sourceSets.main.java.srcDirs) + sourceSets.main.runtimeClasspath

    // assure db directory is present:
    File dbDir = new File('db')
    if (!dbDir.exists()) {
        dbDir.mkdirs()
    }

    ant {
taskdef(name: 'hibernatetool', classname: 'org.hibernate.tool.ant.HibernateToolTask', classpath: htcp.asPath)
        def hibprops = "src/hibernate.production.properties"
        hibernatetool(destdir: '.') {
            classpath (path:sourceSets.main.classesDir.path)
            configuration (configurationfile:tempHibCfgXmlFilename, 
propertyfile:hibprops)
hbm2ddl(drop:"true", create:"true", export:"false", format:"true", delimiter:";", outputfilename:"db/schema.production.ddl")
        }
    }
}


class HibernateCfgXml {
    Set classNames = [] as Set

    void addEntityClass(String fqcn) {
        classNames << fqcn
    }

    void write(String filename) {
        def hibCfgXmlFile = new File(filename)
        if (hibCfgXmlFile.exists()) {
            hibCfgXmlFile.delete()
        }
        if (!hibCfgXmlFile.parentFile.exists()) {
            hibCfgXmlFile.parentFile.mkdirs()
        }
        List lines = []
        classNames.each {
            lines << "        <mapping class=\"$it\"/>"
        }
        hibCfgXmlFile.write('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd";>
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.format_sql">true</property>
''' + lines.join("\n") + '''
    </session-factory>
</hibernate-configuration>''')
    }
}


=================================


Cheers,
RĂ¼diger.


Am 03.08.2011 06:31, schrieb Magnus Rundberget:
Luke,

I've done hibernate/jpa projects with gradle in various flavours. Not that 
difficult.
Your tone/approach to this forum has a slight streak of negativity btw.

Anything in particular you need help with, feel free to ask.

Cheers
Magnus

Date: Tue, 2 Aug 2011 06:57:32 -0700
From: [email protected]
To: [email protected]
Subject: [gradle-user] Re: Hibernate Pojo project


Luke Stephens wrote:

I have a Hibernate Pojo project and I am trying to convert it to gradle
(mainly because I want some further flexibility). I am unsure after
looking at everything I can find, and trying a few things out, if gradle
is ready to handle developing Pojo, JPA based, hibernate projects??

I have seen some things based on Maven integrations, but we avoid Maven as
much as possible.

Any Ideas, or tips on how to implement this? I want a gradle script (or
script elements) that:

- do normal clean/compile/build.
- can generate ddl schema files from Pojo annotations
- can set up a test run that includes setting up a derby database.


Your questions are a bit vague, but...

- do normal clean/compile/build.

apply plugin: "java"

- can generate ddl schema files from Pojo annotations

If there is an Ant task for this, then use it. Otherwise, put the required
Jar on the build script class path (as I showed before) and use an Exec
task.

- can set up a test run that includes setting up a derby database.

Shouldn't be too difficult, but you'd have to be more specific.

--
Peter Niederwieser
Principal Engineer, Gradleware
http://gradleware.com
Creator, Spock Framework
http://spockframework.org
Twitter: @pniederw

--
View this message in context: 
http://gradle.1045684.n5.nabble.com/Hibernate-Pojo-project-tp4658742p4658818.html
Sent from the gradle-user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

     http://xircles.codehaus.org/manage_email


                                        



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to