Murry Lee
Murry Lee

Reputation: 41

How do I add 3rd party libraries with gradle?

I am trying to learn gradle from this site but I do not know how to add a source for 3rd party libraries. Should I add the following to the build.gradle file?

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile "joda-time:joda-time:2.2"
}

jar {
    baseName = 'gs-gradle'
    version =  '2.3'
}

Upvotes: 4

Views: 3986

Answers (1)

real_paul
real_paul

Reputation: 574

I recommend learning gradle from their official site which contains more comprehensive documentation than the Spring site. After grasping the basics of gradle you can venture into using gradle for a Spring project as it adds complexity on top of plain gradle especially when using the Spring boot plugin.

In order to add dependencies to a (Java) project you have to get the GAV coordinates (GroupId:ArtifactId:Version). These can be obtained from the project websites or directly from the public (maven) repositories. The popular jCenter() and mavenCentral() are already build in.

Definining a repository is done using the repositories block:

repositories {
    mavenCentral()
}

Then you have to specify to which Configuration you want to add you dependency; the following snippet adds joda-time to the build-in compile configuration which means that it will be added to the compile classpath:

dependencies {
    compile "joda-time:joda-time:2.2"
}

So yes, your script is correct for adding the joda-time library but it might be a good idea to delve further into the gradle documentation to understand what it does.

Upvotes: 5

Related Questions