Search code examples
classloaderdropwizard

External jars with Dropwizard


I am trying to write a Dropwizard application and its doc tells me that I need to ship everything as an uber jar.

However, in my application I need to support multiple databases and this requires multiple database JDBC driver jars in my classpath, all of which are not expected to be shipped together with my application. Users are expected to place the corresponding JDBC jar like mysql-connector-java-5.1.39.jar in a particular folder by their own.

After reading Dropwizard's documentation I am not sure if this kind of usage is supported. Does anyone have experience making it to work this way?


Solution

  • Since java 6, you can wildcard classpaths.

    Using the application plugin, the generated bin folder will have a start script that contains the classpath. What we want to do, is to instead of listing every possible jar in the bin folder, we simply include all of them.

    Note: You can also do the same thing with different folders if you want the classpath in a different location.

    This can be achieved (in a workaround manner since there are problems with this plugin in my version) in the easiest way as follows. In build.gradle you do:

    startScripts {
      doLast {
        def windowsScriptFile = file getWindowsScript()
        def unixScriptFile    = file getUnixScript()
        windowsScriptFile.text = windowsScriptFile.text.replaceAll('CLASSPATH=.*', 'CLASSPATH=\\$APP_HOME/lib/*')
        unixScriptFile.text  = unixScriptFile.text.replaceAll('CLASSPATH=.*', 'CLASSPATH=\\$APP_HOME/lib/*')
      }
    }
    

    This will wildcard your lib folder in the start scripts. When starting up, your classpath will simply be

    lib/*
    

    When you drop jars into that folder, they will automatically be picked up (on startup, not on runtime).

    I hope this helps,

    Artur