Whenever Gradle searches for dependencies in the repository, first it searches for a module descriptor file (for example, pom.xml
or ivy.xml
). Gradle parses this file and downloads the actual JAR file and its dependencies mentioned in the module descriptor. There might be a case when a module descriptor file is not present. In this case, Gradle directly looks for the JAR file and downloads it.
Gradle enables you to play with your dependencies in different ways. Not only you can download other file formats such as ZIP and WAR, you can also mention different classifiers, if needed.
By default, Gradle downloads file with the .jar
extension. Sometimes, you might need to download either a ZIP file or a WAR file, which does not have any module descriptor. In this scenario, you can explicitly mention the extension of the file:
Dependencies {
runtime group: 'org.mywar', name: 'sampleWeb', version: '1.0', ext: 'war'
}
Sometimes you release the artifacts with special notation (known as classifiers) such as sampleWeb-1.0-dev.war
or sampleWeb-1.0-qa.jar
. To download artifacts with classifiers, Gradle provides the classifier
tag:
dependencies {
runtime group: 'org.mywar', name: 'sampleWeb', version: '1.0', classifier: 'qa', ext:'war'
}
If you do not want to download the existing transitive dependencies and want to replace them with your customized transitive dependencies, Gradle provides the following way:
dependencies { compile module(group:'commons-httpclient', name:'commons-httpclient', version:'3.1') { dependencies "commons-codec:commons-codec:1.1@jar" } }
Here we have used @jar
, which can be used as a replacement for the ext
tag that is used in the preceding example. This code snippet will not download the existing transitive dependencies of commons-httpclient
, but it will download the JAR mentioned inside the curly braces.
When we apply the Java plugin, Gradle automatically gives you some default configurations such as compile and runtime. We can extend this feature and use our own configuration for dependencies. This is an excellent way to group dependencies only needed at build time to achieve particular tasks such as code generators (depending on a templating library), xjc, cxf wsdl to Java, and so on. We can group them under our user-defined configurations. Before using custom configurations under the dependency closure, we need to define it inside the configuration closure. The following is the code snippet of the build_customconf.gradle
file:
apply plugin: 'java' version=1.0 configurations { customDep } repositories { mavenCentral() } dependencies { customDep group: 'junit', name: 'junit', version: '4.11' compile group: 'log4j', name: 'log4j', version: '1.2.16' } task showCustomDep << { FileTree deps = project.configurations.customDep.asFileTree deps.each {File file -> println "File names are "+file.name } }
The following is the output of the preceding code:
$ gradle –b build_customconf.gradle showCustomDep :showCustomDep …. Download https://repo1.maven.org/maven2/junit/junit/4.11/junit-4.11.jar Download https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar File names are junit-4.11.jar File names are hamcrest-core-1.3.jar BUILD SUCCESSFUL
Gradle provides a very convenient way to list out all of the project dependencies from the first level to the nth level. It includes all your transitive dependencies, including manually changed, overridden, and forced dependencies. The dependency tree groups dependencies by configurations such as compile, testCompile, and so on. The following is the code snippet from the build_depreport.gradle
file:
apply plugin: 'java' version=1.0 repositories { mavenCentral() } dependencies { compile group: 'log4j', name: 'log4j', version: '1.2.16' compile 'commons-httpclient:commons-httpclient:3.1' compile 'dom4j:dom4j:1.6.1' }
$ gradle –b build_depreport.gradle dependencies … Root project …. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 default – Configuration for default artifacts. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 runtime – Runtime classpath for source set 'main'. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 testCompile – Compile classpath for source set 'test'. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 testRuntime – Runtime classpath for source set 'test'. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 BUILD SUCCESSFUL
It will show until the child level of all the dependencies for all configurations. You might be surprised to see why other configurations such as runtime and testRuntime are being displayed, though only compile configuration was defined. The following table shows the relationship between different configurations:
Dependency |
Extends |
---|---|
compile |
- |
runtime |
compile |
testCompile |
compile |
testRuntime |
runtime, testCompile |
default |
runtime |
If you want to list out dependencies for only one configuration, you can mention that using –configuration <configuration name>
:
$ gradle –b build_depreport.gradle dependencies –configuration compile :dependencies Root project compile – Compile classpath for source set 'main'. +--- log4j:log4j:1.2.16 +--- commons-httpclient:commons-httpclient:3.1 | +--- commons-logging:commons-logging:1.0.4 | --- commons-codec:commons-codec:1.2 --- dom4j:dom4j:1.6.1 --- xml-apis:xml-apis:1.0.b2 BUILD SUCCESSFUL
Sometimes you might get issues while downloading some transitive dependencies and you do not know which dependency is downloading that JAR file.
Suppose while executing the preceding build_depreport.gradle
script, you are getting issues while fetching the commons-logging
JAR file. It is not the first-level dependency and you do not know which first-level dependency is responsible for this. To get that detail, use the dependencyInsight
command:
$ gradle –b build_depreport.gradle dependencyInsight –dependency commons-logging –configuration runtime :dependencyInsight commons-logging:commons-logging:1.0.4 --- commons-httpclient:commons-httpclient:3.1 --- runtime BUILD SUCCESSFUL
If you do not specify the –configuration
option, it will apply the compile
configuration by default. The other options are runtime
, testCompile
, and so on, as mentioned in the preceding example.
3.138.34.226