Gradle build script defines a process to build projects; each project contains some dependencies and some publications. Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path.
Publications means the outcomes of the project, such as test class files, build files and war files.
All the projects are not self-contained. They need files which are built by the other projects to compile and test the source files. For example, in order to use Hibernate in the project, you need to include some Hibernate JARs in the classpath. Gradle uses some special script to define the dependencies, which needs to be downloaded.
Gradle handles building and publishing the outcomes. Publishing is based on the task that you define. It might want to copy the files to local directory, or upload them to a remote Maven or lvy repository or you might use the files from another project in the same multi-project build. We can call the process of publishing a task as publication.
Declaring Your Dependencies
Dependency configuration defines a set of dependencies. You can use this feature to declare external dependencies, which you want to download from the web. This defines different standers such as follows.
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.+' }
Dependency Configurations
Dependency configuration defines a set of dependencies. You can use this feature to declare external dependencies, which you want to download from the web. This defines the following different standard configurations.
- Compile − The dependencies required to compile the production source of the project.
- Runtime − The dependencies required by the production classes at runtime. By default, it also includes the compile time dependencies.
- Test Compile − The dependencies required to compile the test source of the project. By default, it includes compiled production classes and the compile time dependencies.
- Test Runtime − The dependencies required to run the tests. By default, it includes runtime and test compile dependencies.
External Dependencies
External dependencies are one of the type of dependencies. This is a dependency on some files built outside on the current build, and stored in a repository of some kind, such as Maven central, or a corporate Maven or lvy repository, or a directory I which is the local file system.
The following code snippet is to define the external dependency. Use this code in build.gradle file.
dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' }
An external dependency is declaring the external dependencies and the shortcut form looks like “group: name: version”.
Repositories
While adding external dependencies, Gradle looks for them in a repository. A collection of files, organised by group, name and version is termed as a repository. By default, Gradle does not define any repositories. We have to define at least one repository explicitly. The following code snippet defines how to define maven repository. Use this code in build.gradle file.
Use the following code in build.gradle file −
repositories { mavenCentral() }
Following code is to define remote maven. Use this code in build.gradle file.
repositories { maven { url "https://www.adglob.in/maven2" } }
Publishing Artifacts
Dependency configurations are also used to publish files. These published files are called artifacts. Usually, we use plug-ins to define artifacts. However, you do need to tell Gradle, where to publish the artifacts.
You can achieve this by attaching repositories to the upload archives task. Take a look at the following syntax for publishing Maven repository. While executing, Gradle will generate and upload a Pom.xml as per the project requirements.
Use this code in build.gradle file.
apply plugin: 'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/obb/adglob/") } } }