Hi community

I am trying to write a Gradle task that does the following thing.

   - Exporting JPA annotated Hibernate POJOs to a file

I have found several links to that issue (e.g.
http://www.beeworks.be/maven-to-gradle-part-2/) but now of them helped me to
solve the problem. My situation is as follows.

   - I am using Spring 3.x and JPA annotations and therefore there is
   neither a persistance.xml nor *.hbm.xml because Spring does the
   configuration at runtime

Since I am new to Gradle I tried to do it within a Groovy script as a
'proof-of-concept' (see listing below). Some parts of the script I would
like to place into the Gradle task but I have some question left.

   - Is there an official task / plugin which solves my problem?
   - How can a Gradle task using the declared dependencies of the module?
   - Can a Gradle task specific additionals dependencies? In this case I
   would add the scannotation jar into the classpath while the task is
   executing.

Any hints are welcome.

Best regards,
Silvio

<code>
// use Groovy 1.7.4 or greater
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.scannotation.*

@GrabResolver(name = 'jboss', root = '
https://repository.jboss.org/nexus/content/groups/public/')
@GrabResolver(name = 'spring', root = '
http://maven.springframework.org/release/')

@Grapes([
@Grab(group = 'org.hibernate', module = 'hibernate-core', version =
'3.5.6-FINAL'),
@Grab(group = 'org.hibernate', module = 'hibernate-annotations', version =
'3.5.6-FINAL'),
@Grab(group = 'javax.persistence', module = 'persistence-api', version =
'1.0'),
@Grab(group = 'org.slf4j', module = 'slf4j-simple', version = '1.4.2'),
@Grab(group = 'net.sf.scannotation', module = 'scannotation', version =
'1.0.2')
])

def logger = LoggerFactory.getLogger(getClass());

def outputFile = new File('./hockey-game-mysql.sql')
def jarFile = new File('./core/build/libs/core-0.1-SNAPSHOT.jar')

assert jarFile.exists(): "File does not exist ${jarFile.absolutePath}"

if (outputFile.exists()) {
  logger.info("Detected previously generated SQL file. About to delete
${outputFile.name}")
  outputFile.delete()
}

this.getClass().classLoader.rootLoader.addURL(jarFile.toURL())

URL[] urls = [jarFile.toURL()] as URL[]
AnnotationDB db = new AnnotationDB()
db.scanArchives(urls)

if (db.annotationIndex[javax.persistence.Entity.class.name] ||
db.annotationIndex[javax.persistence.MappedSuperclass.class.name]) {

  def cfg = new org.hibernate.cfg.AnnotationConfiguration()

  def properties = new Properties()
  properties['hibernate.format_sql'] = true
  properties['hibernate.use_sql_comments'] = true
  properties['hibernate.dialect'] =
'org.hibernate.dialect.MySQL5InnoDBDialect'

  cfg.properties = properties
  addClasses(cfg, db.annotationIndex[
javax.persistence.MappedSuperclass.class.name], logger)
  addClasses(cfg, db.annotationIndex[javax.persistence.Entity.class.name],
logger)

  cfg.buildSessionFactory()

  def exporter = new org.hibernate.tool.hbm2ddl.SchemaExport(cfg)
  exporter.delimiter = ';'
  exporter.format = Boolean.TRUE
  exporter.haltOnError = Boolean.TRUE

  exporter.outputFile = outputFile.absolutePath

  exporter.execute(false, false, false, true);

  assert outputFile.text.length() > 0 : 'File is empty'
}
else {
  logger.warn('No annotated classes have been found')
}

private def addClasses(org.hibernate.cfg.AnnotationConfiguration cfg,
Set<String> classNames, logger) {
  for (String className: classNames) {

    logger.info "About to add annotated class '$className' to Hibernate
configuration"

    cfg.addAnnotatedClass(getClass().getClassLoader().loadClass(className))
  }
}
<code>

Reply via email to