Dependency Management in Gradle

In Gradle, dependency managenment is made up of two things. They are as following:

  1. Gradle should know the needs of the project to build or to run. These files are the dependencies of the project.
  2. Gradle needs to build and upload thing which a project produces. These outgoing files are the publications of the project.

In Gradle, mostly projects are not independent. Projects need files which were built by other projects for compilation or to test. For example, if I  use hibernate in a project, I need a hibernate jar in the classpath when the source code is compiled.

These files are the dependency of the project. In Gradle, we can tell the dependencies of a project, and then Gradle takes care of finding these dependencies and make it available in the build. These dependencies can be downloaded from remote Maven or Ivy repository, or you can build your own dependency on a project and include it as a dependency. This process is known as dependency resolution.

This feature provides an advantage over Ant. Using Ant, we have the ability to specify absolute or relative paths of jars to load. In Gradle, we just have to declare the name of the dependencies and layer to determine the dependency. Similar behavior can be achieved from Ant when we add Apache Ivy, but Gradle is better in this case.

But, a dependency of a project can itself have a dependency. For example, in hibernate-core, we require many libraries which should be present on the classpath. So, when Gradle runs a test of a project, it searches the dependencies and makes it available. This is known as transitive dependencies.

The purpose of the projects is to build files which can be used outside of the project.

These files form the publications of a project. When you declare the publication of a project, the Gradle takes care of building it and publishing it.

Declaring dependencies

Example:

build.gradle
apply plugin: 'java' 
repositories { 
mavenCentral() 
}
dependencies { 
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' 
testCompile group: 'junit', name: 'junit', version: '4.+' 
} 

In the above example, for compiling the project, Hibernate core 3.6.7.Final is required. Hibernate core as well as its dependency is also required at runtime. To test the project junit>=4.0 is required. It also take cares of the dependencies that are required.

Dependency configurations

In Gradle, dependency configuration is a set of dependencies and artifacts. Below are the three main purposes for configuration:

1) Declaring Dependencies

Plugins use configurations, which makes easy for building authors to declare other external artifacts which are used for the execution of tasks, which are defined by the plugins.

2) Resolving Dependencies

The plugin uses configuration and finds inputs for the tasks which are defined.

3) Exposing Artifacts for Consumption

The plugin uses configurations which are used to define artifacts for other project consumption.

External dependencies

In Gradle, there are many types of dependencies, one of them is an external dependency. These dependencies are built outside of current build, and then stored in a repository such as a Maven central or Ivy repository or a directory in the local file system.

Example:

build.gradle
dependencies { 
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' 
} 

External dependencies has three attributes group, name and version.

The shorthand property for defining external dependencies is “group:name:version”

Repositories

Gradle searches the files for external dependencies in the repository.  A repository is the collection of files which are organized by group, name, and version. Gradle can understand many different repository formats, such as Maven and Ivy.

By default, Gradle does not have any repositories defined. There should be at least one external dependency defined before using it. One of the options is to use the Maven central repository.

Example:

build.gradle
repositories { 
mavenCentral() 
}