Set dynamic classpath for Application plugin


(Klaus Christiansen) #1

I have an application that extracts from data from a database.
It is always the same columns in the same tables. But some time database is DB2 other times it is Oracle.
This means that the database drivers may differ from customer to customer.

I use the Application plugin to make a distribution of my application.

The plan is that the customer get my application and copies there driver jars into the lib folder. (or some other folder)

I can’t figure out how to make a wildcard in the classpath in the execution scripts.

This is the classpath part of the .bat file created.
set CLASSPATH=%APP_HOME%\lib\EncyclopediaDumper.jar;%APP_HOME%\lib\db2jcc4.jar;%APP_HOME%\lib\db2jcc_license_cu.jar

I would like something like:
set CLASSPATH=%APP_HOME%\lib\EncyclopediaDumper.jar;%APP_HOME%\db-lib\*

All help is appreciated.


(Benjamin Muschko) #2

You should be able to modify the classpath of the task type CreateStartScripts via the property classpath. Keep in mind that Java does not support a wildcard-based classpath definition as indicated by db-lib*. You will need to determine the concrete paths dynamically in your code.


(Klaus Christiansen) #3

db-lib* was suppose to be db-lib\*

startScripts {
    classpath += file("db-lib/*")
}

results in:

A problem occurred evaluating root project 'EncyclopediaDumper'.
Could not normalize path for file 'C:\Users\kec\workspace_neon\XcodeCon3\EncyclopediaDumper\db-lib\*'.

(Benjamin Muschko) #4

The syntax you use doesn’t work. You’ll have to explicitly list the files you want to add. I believe the following should work:

startScripts.classpath += files { file("db-lib").listFiles() }


(Klaus Christiansen) #5

I’m not sure that I’m explaining myself well enough.

The folder “db-lib” in my application is empty and shall be empty.
Customer receives my application.zip unzip it and copy his/hers driver jars into the folder “db-lib”.

So I need the classpath to be something like: “CLASSPATH=%APP_HOME%\lib\EncyclopediaDumper.jar;%APP_HOME%\db-lib\*”

I need this: “db-lib\*” because the drivers can be named anything.


(Benjamin Muschko) #6

The property classpath only accepts file paths that can be normalized. So a wildcard definition won’t work. I suggest that you provide your own script template that already adds it to the CLASSPATH environment variable.

startScripts {
    unixStartScriptGenerator.template = resources.text.fromFile('customUnixStartScript.txt')
    windowsStartScriptGenerator.template = resources.text.fromFile('customWindowsStartScript.txt')
}

You can simply use the Gradle script templates as starting point.

Alternatively, I’d probably just create different distributions of your application that already contains the correct database driver.


(Klaus Christiansen) #7

Thanks. The solution with custom starter scripts seams good.