Using Gradle with Maven Part 2: Dependencies and Repositories

Build tools such as Maven and Ivy scores over ANT as they have dependency management support. There are even open source hosted binaries on sites like Maven Central, so that you don’t have to download all the required dependencies yourself, but can just ask the build tool to get it for you. For simple Hello World programs, you might not require any other libraries, but an enterprise project will have lot of dependencies to other libraries. For instance a Spring or hibernate program will have dependencies on their framework jars. Spring Framework page currently provide you options to get the jar dependencies through Maven or Gradle, rather than downloading the jar dependencies yourself. Refer to Spring Note Book under Frameworks for more details.

Gradle offers both an Ivy and Maven dependency and repository compatibility layer. A library is declared as a desired external dependency by a one-line listing in a dependencies block. The current Spring Framework page requires you to add below dependency to your build file to get started with Spring:

dependencies {

    compile 'org.springframework:spring-context:4.0.5.RELEASE'

}

This can also be written in a more verbose form (following Maven) as:

dependencies {

    compile group: 'org.springframework', name: 'spring-context', version: '4.0.5.RELEASE'

}

Here group, name and version are Gradle’s properties corresponding to Maven coordinates in the POM file groupId, artifactId and version respectively.

Here the compile phrase is referred to as the configurations and external dependencies are applied to these configuration scopes.  There are six configurations (or scopes) for the java plug-in: compile, default, testCompile, testRuntime, archives and runtime. Unlike the fixed scopes of Maven, a Gradle plug-in can introduce new scopes, and all scopes (from all plug-ins specified) are defined with dependencies in the dependencies block.

 

Repositories

As we have seen, Gradle allow us to use Maven or Ivy repositories easily. We have also seen, how to tell Gradle to use the Maven Central repository:

repositories {

    mavenCentral()

}

Similarly, you can use use jcenter() for Maven JCenter repository and mavenLocal() for Local Maven repository. Gradle uses the same logic as Maven to identify the location of your local Maven cache. If a local repository location is defined in a settings.xml, this location will be used. The settings.xml in USER_HOME/.m2 takes precedence over one in M2_HOME/conf. If no settings.xml is available, Gradle uses the default location USER_HOME/.m2/repository.

You can also specify another (project specific) repository easily, if needed. In older Gradle versions, you could specify another (project specific) repository with an addition of a URL to the mavenRepo configuration element of repositories as:

repositories {

  mavenRepo(urls: '<maven-repo-url>')

}

However this is deprecated in latest Gradle and you might get a warning like:

The RepositoryHandler.mavenRepo() method has been deprecated and is scheduled to be remove d in Gradle 2.0. Please use the maven() method instead.

According to the documentation for the Gradle DSL, maven can accept a closure or an action. So we can replace the above mavenRepo method with a closure as:

maven {

  url '<maven-repo-url>'

}

 

Flat directory repository

You can also specify a flat directory where you have put the required jar files to resolve dependencies using the flatDir configuration of the repository:

repositories {

  flatDir name: 'localDiskRepoName', dirs: 'lib1'

}

If there are more directories, you can use the syntax:

repositories {

  flatDir dirs: ['lib1', 'lib2']

}

Here lib1 and lib2 directories are in the same level as build.gradle file. You can also specify absolute or relative paths (with proper escaping if necessary):

flatDir dirs: ['D:\\GRADLE\\lib1', '../GRADLE/lib2']

Another thing to note here is that, when you specify flatDir to specify a flat directory, you need to specify all required dependencies, unlike before. So for compiling a basic Spring code, instead of specifying only compile 'org.springframework:spring-context:4.0.5.RELEASE', you need to specify as:

dependencies {

    compile 'org.springframework:spring-context:4.0.5.RELEASE'

    compile 'org.springframework:spring-core:4.0.5.RELEASE'

    compile 'org.springframework:spring-beans:4.0.5.RELEASE'

}

Gradle will add up the name and version part above, and add .jar to it, and search the folder specified as flatDir. E.g. spring-core-4.0.5.RELEASE.jar.

 

Reference

http://www.gradle.org/docs/current/userguide/dependency_management.html

Book: Building and Testing with Gradle by Tim Berglund and Matthew McCullough.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)