Skip to content

Using PCT with Groovy

Gilles QUERRET edited this page Sep 2, 2017 · 10 revisions

Pointy bracket hell? ant-contrib hell?

Despite being primarily a build tool, Apache Ant is a very practical tool for manipulating files including zip files, copy, resource processing, …​ But if ever you’ve been working with a build.xml file and found yourself a little restricted by all those pointy brackets, or found it a bit weird using XML as a scripting language and wanted something a little cleaner and more straight forward, then maybe Ant scripting with Groovy might be what you’re after.

Hmm, Groovy... But what is Groovy?

Wikipedia explains it:

Apache Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform, is dynamically compiled to Java virtual machine (JVM) bytecode, and interoperates with other Java code and libraries. Groovy uses a Java-like curly-bracket syntax. Most Java code is also syntactically valid Groovy, although semantics may be different.

How easy is it?

This page is a very good introduction to Ant scripting in Groovy.

A short example on how to execute an OpenEdge procedure in Groovy:

def ant = new AntBuilder()          
ant.taskdef (resource: 'PCT.properties')

ant.PCTRun(procedure: 'updateCustomers.p', dlcHome: '/usr/dlc') {
  DBConnection(dbName: 'sports2000', dbDir: '/db/prod')
}

Another example on how to execute the same procedure on each OpenEdge database in a directory:

new File('databases').traverse(type: FileType.FILES, nameFilter: ~/[a-z]+\.db/) { db ->
  ant.PCTRun(procedure: 'updateCustomers.p', dlcHome: '/usr/dlc') {
    DBConnection(dbName: db.name, dbDir: db.parent)
  }
}

I don't want new dependencies!

If you're working with PCT, it's because your product is written in OpenEdge. OpenEdge includes a Java Virtual Machine since ages. Groovy is pure Java. PCT is pure Java. Both are just a JAR files. This means that if you want to use Groovy / PCT to deploy on customer site, you can rely on the JVM bundled with OpenEdge, which is available at $DLC/jre (or %DLC%\jre). So once the DLC variable is correctly set, you can execute your update script:

$DLC/jre/bin/java -jar groovy-pct.jar update-script.groovy

What can I use it for?

  • Database update mechanism
  • Rolling out new release

Examples

Use command-line parameters

def cli = new groovy.util.CliBuilder(usage: 'java -jar groovy-pct.jar script.groovy [options]')
cli.dlc(args: 1, 'OpenEdge installation directory')
cli.host(args: 1,optionalArg: true, 'Hostname')
cli.port(args: 1,optionalArg: true, 'Port')
cli.user(args: 1,optionalArg: true, 'User name')
cli.password(args: 1,optionalArg: true, 'Password')

def options = cli.parse(args)

Read info from database

propath = [ 'dir1', 'dir2', 'dir3']
ant.PCTRun (procedure: 'readinfo.p', dlcHome: options.dlc) {
  DBConnection (dbname: 'dbname', dbdir: options.dbDir)
  Propath {
    propath.each { pathelement (location: it) }
  }
  OutputParameter (name: 'xxx')
  OutputParameter (name: 'yyy')
}
def xxx = ant.project.properties.xxx.toInteger();
def yyy = ant.project.properties.yyy.toInteger();
println "-> Output parameters : ${xxx} and ${yyy}"

Clone this wiki locally