Home > OS >  How do I add libraries to my Gradle file?
How do I add libraries to my Gradle file?

Time:01-13

It's probably very simple, but only to people who know what they are doing. I have a Java program that imports these two:

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

As an aside, I don't want to use the lang3 package but the lang package.

I do not have anything in my Gradle file about these. When I try to build the file, it gives me errors for these two, saying the packages do not exist.

My questions are:

  • Do I need to add them as "compile" or as "api"?
  • What is the exact syntax? I have lines that look like this: api group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
  • How do I find the right name (or should I just invent one)? and the version?

CodePudding user response:

Anything your code needs (besides basic JRE classes) is a dependency for your code. Gradle manages these dependencies, usually downloading them from a repository.

First you need to find such a repository. You probably have repositories already configured in your build.gradle, like so:

buildscript {
    repositories {
        mavenCentral()
        // ...
    }
}

That means Gradle will try to download dependencies from Maven Central. You can either do a web search for "gradle" and your dependency, or go to repository and search, or check the implementation ss

the $lifecycle_version might be somethiing like 1.2.3 or some version number.

this is what I got (not exactly sure if this is right)-

implementation 'org.apache.commons:commons-lang3:3.12.0'

got from maven repo ss

Once done, you'll be able to import the respective libraries.

  •  Tags:  
  • Related