0
votes

I am integrating worklight 6.1 official deployment ant task into gradle build script. We will utilize below script to run flexible continuous integration process.

def antTaskLibClasspath = "ant-task-lib/worklight-ant-deployer.jar"

def verifyDatabaseArguments(String database){
    switch(project.ext[database + 'Type']){
        case 'derby':
            def dbNameProp = database + 'Name'
            def dbDirPathProp = database + 'Dir'
            def dbProperties = ['databaseName':project.ext[dbNameProp], 'databaseDir':project.ext[dbDirPathProp] ]
                .each { key, value ->
                    if(!value?.trim()){
                        def errMsg = "Missing mandatory parameter : ${key}"
                        println errMsg
                        throw new StopExecutionException(errMsg)
                    }
                }
            project.ext[dbDirPathProp] = Eval.me(""" "${project.ext[dbDirPathProp]}" """).replace("\\", "/")
            def dbFile = file(project.ext[dbDirPathProp] + "/${project.ext[dbNameProp]}")
            if(!dbFile.exists()){
                if(dbFile.mkdirs()){
                    println "Create folders ${project.ext[dbDirPathProp]} for ${database} derby database"
                }else {
                    def errMsg = "Can not create folders for ${database} derby database"
                    println errMsg
                    throw new StopExecutionException(errMsg)
                }
            }
            break
        case 'oracle':
            break
        default:
            errMsg = "Unsupported database type"
            println errMsg
            throw new StopExecutionException(errMsg)
            break
        }
    }

    verifyDatabaseArguments("worklightDatabase")
    verifyDatabaseArguments("worklightReportsDatabase") 

    task configure {
    description "Configure database for worklight server"
    doLast {

        ant.typedef(name:'configureDatabase',
            classname:'com.ibm.worklight.config.ant.database.ConfigureDatabaseTask',
            classpath:antTaskLibClasspath
        )

        ant.configureDatabase(kind: "Worklight"){
            switch(worklightDatabaseType){
                case 'derby':
                    derby(database:worklightDatabaseName, datadir:worklightDatabaseDir)
                    break
            }
        }
        ant.configureDatabase(kind: "WorklightReports"){
            switch(worklightReportsDatabaseType){
                case 'derby':
                    derby(database:worklightReportsDatabaseName, datadir:worklightReportsDatabaseDir)
                    break
            }
        }
    }
}

And these are properties which were referenced in the script overhead. I've set them in gradle.properties where the build.gradle is located.

worklightDatabaseType=derby

# derby database name
worklightDatabaseName=WRKLGHT
# derby database file directory
worklightDatabaseDir=${System.properties['user.home']}/.derby/ibm

worklightReportsDatabaseType=derby

# derby database name
worklightReportsDatabaseName=WLREPORT
# derby database file directory
worklightReportsDatabaseDir=${System.properties['user.home']}/.derby/ibm

The command line shows ant error message which I can't figure out what's going wrong after I try to configure database with command $ > gradle configure.

$ > gradle install

Line 75 is the place where I start the first invocation of ant.configureDatabase( ...

Did I miss any mandatory configuration which may not documented in the worklight 6.1?

My jdk version is jdk7_60, gradle version 2.4 without wrapper. Any suggestion/comment is appreciated, thank you~

By the way, if any consultant from IBM mobilefirst team is watching,
please consider to ask your product team to rename ant task which has dash symbol in it's name.
It seems that Groovy AntBuilder can not load ant element name includes dash symbol,
so we have to unzip worklight ant task jar archives and search for the task class we need in defaults.properties,
then redefine ant task using class name manually in gradle script. That's not convenient.
The build progress of worklight project is complex. We don't like to use ant to setup build automation.


Appended after Peter post his suggestion:

Thank you, Peter. I have step over the ant library issue after adopting your way. But it stucks again, and I couldn't interpret the error message.

At first I assume this issue is because worklight 6.1 ant deploy task may not be compatible with gradle,
so I wrote ant build script below to test if my assumption is correct,
which implements the same build logic and variable as previous gradle script does.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="install">
    <loadproperties srcfile="build.properties" />
    <taskdef resource="com/worklight/ant/defaults.properties">
        <classpath>
            <pathelement location="worklight-ant-task-libs/worklight-ant-deployer.jar"/>
        </classpath>
    </taskdef>
    <target name="configure">
        <configuredatabase kind="Worklight">
            <derby database="${worklightDatabaseName}" datadir="${worklightDatabaseDir}"/>
        </configuredatabase>
        <configuredatabase kind="WorklightReports">
            <derby database="${worklightReportsDatabaseName}" datadir="${worklightReportsDatabaseDir}"/>
        </configuredatabase>
    </target>
</project>

build.properties

# derby database name
worklightDatabaseName=WRKLGHT
# derby database file directory
worklightDatabaseDir=${user.home}/.derby/ibm
# derby database name
worklightReportsDatabaseName=WLREPORT
# derby database file directory
worklightReportsDatabaseDir=${user.home}/.derby/ibm

And my build progress fails again.
I have upload the ant error message log file to google drive.
Can anyone give me some suggestion to help me figure it out? Thank you~

1
You haven't shown the error you're getting. I also don't see a reference to the ant property in your example script. BTW, you can escape the ant task name with quotes, e.g. ant.'my-task'(args).Peter Ledbrook
Sorry , I just removed the error message accidentally. Here it comes.youjenlee
If the answer fixed the specific problem you were having, you should accept it. Any subsequent issues should be raised as separate questions. Anyway, the log file states "Caused by: ERROR XBM0J: Directory C:\Users\martinli\.derby\ibm\WRKLGHT already exists". Why not try deleting that directory? Or change the worklightDatabaseDir and worklightReportsDatabaseDir properties.Peter Ledbrook
Thank you Peter, I will raise up another discussion thread next time if I have a problem which includes several issues. Inspired by your answer, I just figure it out what brings out the second issue of worklight ant task. I shouldn't create the folder for derby database file manually. The database configuration task will fail if it detects a folder with the same name as I specified in the element attribute. So... the only thing I have to do is to make sure that parent folders of the database folder is already exist before I start the configuration task.youjenlee

1 Answers

1
votes

I'm going to use a lot of guesswork here, but according to this section of the Ant Manual, ant.library.dir is only set by Ant's Launcher class, which Gradle's Ant integration probably doesn't use.

I suspect that the Worklight Ant task you're using assumes that property is set and throws an exception. You can check this by running

 gradle --stacktrace configure

assuming that configure is the task you want to run. This will show you where the exception is being thrown from (and any causes too).

You might be best off adding an unpacked Ant distribution to your source tree (or perhaps just its lib directory) and adding the following entry to a gradle.properties file in the root of your project:

systemProp.ant.library.dir=<path to Ant>/lib

Of course you should replace <path to Ant> with the actual path to an Ant installation.