Search code examples
javagradlederby

How to include Apache Derby as a Gradle dependency using downloaded binaries


I know Java pretty well and am an experienced C/Python programmer, but I may have some fundamental misconceptions when it comes to Gradle.

I'm developing in the terminal and vim, it's just who I am. I have Apache Derby set up on my system: downloaded, environment variables set, etc. I wish to use this in my Java project which I'm building with Gradle, but I don't know how to include it in the dependencies other than from the Maven repository.

If I do:

testCompile group: 'org.apache.derby', name: 'derby', version: '10.5.3.0' 

My understanding is that this downloads it from the Maven repo again. Am I going about things the wrong way by wanting to use my system Derby, or is there a way to point Gradle to it? If this question is riddled with misconceptions I would appreciate them being put straight.

Thanks.


Solution

  • +1 for using vim/terminal, that is cool! 😎

    Gradle is extremely flexible and will do whatever you ask - although there are best practices, see below. You can have a libs/ folder with all your libraries in it, and point Gradle to it:

    dependencies {
        implementation fileTree('libs') { include '*.jar' }
    }
    

    This will add all JARs in the libs/ folder to your classpath. Gradle calls this a file dependency. The libs folder does not have to be in your project directory, so yes, you can point to your system-wide libs, too.

    However.

    Gradle has a very advanced caching mechanism, which will minimize network usage and even balance this with local storage requirements. It will also reliably share downloads between projects. You want to use this.
    Also, many Java libs have dependencies of their own ("transitive dependencies"), often quite a lot of them, resulting in whole trees of dependencies. Since the transitive closure of all of them will end up on your classpath, there can be conflicts, which Gradle will resolve for you. You will not want to perform manual dependency resolution.
    Plus, your project will be easier to compile for others because Gradle will make sure the dependency is loaded and present. Whereas a "system derby" would need to be installed in some other way before running Gradle.
    File dependencies have a myriad of other problems as well, so I would urge you to have Gradle handle this for you and use the syntax from your question:

    dependencies {
        implementation group: 'org.apache.derby', name: 'derby', version: '10.5.3.0'
    }
    

    Here's a little more info on so-called "configurations", like testCompile and implementation to help you choose the right ones.

    All of this will work perfectly with Vim and terminal. So well in fact that I often call Gradle from a terminal while developing in IntelliJ. 😊