Search
  • Money Tips | Product Reviews

Gradle Dependencies - What is it all about?

Updated: Jul 8

Gradle dependencies

Gradle dependencies are like any other dependencies that are required for your application code. Dependencies are external libraries that you need to run your application. If your code is connecting to Oracle DB and does CRUD operations, you might need the oracle JDBC dependency


implementation group: 'com.oracle', name: 'ojdbc14', version: '10.2.0.4.0'

You might need several other dependencies such as JUnit, Spock, SL4J, AWS SDK depending on what your application is.


How can I declare a dependency on the Gradle project?

First, we need to understand that there two types of namely runtime and compile-time.

You need to define the scope where you need the dependencies with the help configuration block in the build/grade file as shown below.

For dependencies that are used by tests (test scope), use the test implementation configuration, and for compiling the application code, use


configurations {
    smokeTest.extendsFrom testImplementation
}

dependencies {
runTimeOnly 
    testImplementation 'junit:junit:4.13'
    smokeTest 'org.apache.httpcomponents:httpclient:4.5.5'}
    

Then there is also dependency types - two types:

  • Direct dependencies - libraries that are used directly by your project

  • Transitive dependencies - libraries your dependencies that your direct dependencies need in turn for their code to compile properly.


A configuration can extend another configuration as shown below.

Image Source; Gradle.org


What are the different kinds of Gradle dependencies?

Module dependencies

These are the most commonly used dependency types. You simply specify them in your build.gradle file. If you notice, the dependency is represented as a string notation. You can also specify it as a map notation.


String notation:

dependencies {
   implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
    }

Map notation:

dependencies {
   implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
}


File dependencies

Some dependencies are standalone jar files in your local or network drive or pure text or excel files. These are unlike the dependencies that you normally pull from JFrog Artifactory which hosts these dependency files.


Project dependencies

If your project has many sub-projects or sub-modules, you can simply your dependency configuration. If a dependency is used by many sub-projects, you can make a sub-project use dependencies from another project.

Here, ProjectB depends on ProjectA's dependencies.

ProjectB/build.gradle

    dependencies {
    implementation project(':ProjectA')}
    

Gradle distribution-specific dependencies

Gradle API dependency

dependencies {
    implementation gradleApi()}

Gradle TestKit dependency

dependencies {
    testImplementation gradleTestKit()}

Local Groovy dependency

dependencies {
    implementation localGroovy()}

Where do I specify the Artifactory location?


Now that you know how to declare dependencies, it is time to add to declaration for resolving an artifact for your declared dependencies.

The repository you want might be a publicly available repository such as Maven Central, Bintray JCenter, or Google Android.


Example for Maven Central:

buildscript {
   repositories {
    mavenCentral()
    }
}

dependencies {
    generic "com.github.jnr:jffi:1.2.7@jar"
}

You can also declare multiple repositories like this. See how it also uses a custom maven URL.

repositories {
    mavenCentral()
    maven {
        url "https://maven.springframework.org/release"}
    maven {
        url "https://maven.restlet.com"}}

Declaring custom Maven repository URL

repositories {
    maven {
        url "http://repo.yourcompany.com/maven2"
   }
}

There is a lot more to dependencies than what is explained in this post. Head to the Gradle documentation site to learn even more.


Most popular:

Selenium 4 Java and Python - Chrome DevTools to capture XHR response body

How To Record Using JMeter Chrome Extension

Click here to check out more posts like these.



Recommended