Skip to content Skip to sidebar Skip to footer

Maven Downloading Same Version of Dependency Again

There are many situations when you want to use the latest version of a item module dependency, or the latest in a range of versions. This can be a requirement during evolution, or you lot may exist developing a library that is designed to piece of work with a range of dependency versions. Yous can easily depend on these constantly changing dependencies by using a dynamic version. A dynamic version can be either a version range (eastward.g. 2.+) or it tin exist a placeholder for the latest version bachelor e.g. latest.integration.

Alternatively, the module you request can change over time fifty-fifty for the same version, a so-chosen changing version. An example of this type of changing module is a Maven SNAPSHOT module, which e'er points at the latest antiquity published. In other words, a standard Maven snapshot is a module that is continually evolving, it is a "changing module".

Using dynamic versions and changing modules can lead to unreproducible builds. As new versions of a item module are published, its API may become incompatible with your source code. Use this feature with caution!

Declaring a dynamic version

Projects might adopt a more ambitious approach for consuming dependencies to modules. For example y'all might desire to ever integrate the latest version of a dependency to consume cutting border features at whatever given time. A dynamic version allows for resolving the latest version or the latest version of a version range for a given module.

Using dynamic versions in a build bears the risk of potentially breaking it. Equally soon as a new version of the dependency is released that contains an incompatible API change your source lawmaking might terminate compiling.

Example 1. Declaring a dependency with a dynamic version

build.gradle

                      plugins {     id 'coffee-library' }  repositories {     mavenCentral() }  dependencies {     implementation 'org.springframework:bound-spider web:5.+' }                    

build.gradle.kts

                      plugins {     `coffee-library` }  repositories {     mavenCentral() }  dependencies {     implementation("org.springframework:spring-web:5.+") }                    

A build browse can effectively visualize dynamic dependency versions and their corresponding, selected versions.

dependency management dynamic dependency build scan

Effigy 1. Dynamic dependencies in build scan

By default, Gradle caches dynamic versions of dependencies for 24 hours. Inside this time frame, Gradle does not try to resolve newer versions from the alleged repositories. The threshold can exist configured as needed for instance if y'all want to resolve new versions before.

Declaring a changing version

A squad might decide to implement a series of features earlier releasing a new version of the application or library. A common strategy to allow consumers to integrate an unfinished version of their artifacts early and oft is to release a module with a and then-chosen irresolute version. A changing version indicates that the feature set is still under agile development and hasn't released a stable version for general availability yet.

In Maven repositories, changing versions are commonly referred to as snapshot versions. Snapshot versions incorporate the suffix -SNAPSHOT. The following example demonstrates how to declare a snapshot version on the Spring dependency.

Example 2. Declaring a dependency with a changing version

build.gradle

                      plugins {     id 'java-library' }  repositories {     mavenCentral()     maven {         url 'https://repo.jump.io/snapshot/'     } }  dependencies {     implementation 'org.springframework:spring-spider web:5.0.3.BUILD-SNAPSHOT' }                    

build.gradle.kts

                      plugins {     `coffee-library` }  repositories {     mavenCentral()     maven {         url = uri("https://repo.spring.io/snapshot/")     } }  dependencies {     implementation("org.springframework:bound-web:five.0.3.BUILD-SNAPSHOT") }                    

Past default, Gradle caches changing versions of dependencies for 24 hours. Inside this time frame, Gradle does not try to resolve newer versions from the declared repositories. The threshold can exist configured every bit needed for case if you want to resolve new snapshot versions before.

Gradle is flexible plenty to treat whatsoever version as changing version e.thousand. if you wanted to model snapshot behavior for an Ivy module. All you demand to practise is to set the holding ExternalModuleDependency.setChanging(boolean) to true.

Controlling dynamic version caching

By default, Gradle caches dynamic versions and changing modules for 24 hours. During that fourth dimension frame Gradle does not contact whatever of the declared, remote repositories for new versions. If you want Gradle to check the remote repository more than frequently or with every execution of your build, and so you volition need to change the time to live (TTL) threshold.

Using a curt TTL threshold for dynamic or changing versions may consequence in longer build times due to the increased number of HTTP(s) calls.

You lot tin override the default cache modes using command line options. You tin can likewise change the enshroud expiry times in your build programmatically using the resolution strategy.

Controlling dependency caching programmatically

You can fine-melody sure aspects of caching programmatically using the ResolutionStrategy for a configuration. The programmatic approach is useful if you would like to change the settings permanently.

By default, Gradle caches dynamic versions for 24 hours. To change how long Gradle volition enshroud the resolved version for a dynamic version, apply:

Example 3. Dynamic version enshroud control

build.gradle

                      configurations.all {     resolutionStrategy.cacheDynamicVersionsFor ten, 'minutes' }                    

build.gradle.kts

                      configurations.all {     resolutionStrategy.cacheDynamicVersionsFor(10, "minutes") }                    

By default, Gradle caches changing modules for 24 hours. To change how long Gradle will enshroud the meta-data and artifacts for a changing module, employ:

Instance 4. Changing module cache command

build.gradle

                      configurations.all {     resolutionStrategy.cacheChangingModulesFor 4, 'hours' }                    

build.gradle.kts

                      configurations.all {     resolutionStrategy.cacheChangingModulesFor(4, "hours") }                    

Controlling dependency caching from the command line

Avoiding network access with offline mode

The --offline command line switch tells Gradle to always utilise dependency modules from the cache, regardless if they are due to exist checked once more. When running with offline, Gradle volition never effort to admission the network to perform dependency resolution. If required modules are non present in the dependency cache, build execution volition fail.

Refreshing dependencies

You can control the beliefs of dependency caching for a singled-out build invocation from the control line. Command line options are helpful for making a selective, ad-hoc choice for a single execution of the build.

At times, the Gradle Dependency Cache can become out of sync with the bodily country of the configured repositories. Perhaps a repository was initially misconfigured, or perhaps a "non-changing" module was published incorrectly. To refresh all dependencies in the dependency cache, employ the --refresh-dependencies option on the control line.

The --refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts. A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded. All the same, where possible Gradle will check if the previously downloaded artifacts are valid before downloading over again. This is washed by comparison published SHA1 values in the repository with the SHA1 values for existing downloaded artifacts.

  • new versions of dynamic dependencies

  • new versions of changing modules (modules which utilise the same version string but can have different contents)

Refreshing dependencies volition cause Gradle to invalidate its listing caches. However:

  • it will perform HTTP Head requests on metadata files just will non re-download them if they are identical

  • information technology will perform HTTP Caput requests on artifact files simply will non re-download them if they are identical

In other words, refreshing dependencies but has an impact if you actually use dynamic dependencies or that you have changing dependencies that you were not aware of (in which case it is your responsibility to declare them correctly to Gradle as irresolute dependencies).

Information technology's a common misconception to think that using --refresh-dependencies will force download of dependencies. This is non the case: Gradle will just perform what is strictly required to refresh the dynamic dependencies. This may involve downloading new listing or metadata files, or even artifacts, but if cypher changed, the impact is minimal.

Using component selection rules

Component pick rules may influence which component example should exist selected when multiple versions are available that match a version selector. Rules are applied against every available version and allow the version to exist explicitly rejected by dominion. This allows Gradle to ignore any component case that does not satisfy conditions set by the rule. Examples include:

  • For a dynamic version similar one.+ certain versions may be explicitly rejected from selection.

  • For a static version like 1.4 an case may be rejected based on extra component metadata such every bit the Ivy branch attribute, allowing an instance from a subsequent repository to be used.

Rules are configured via the ComponentSelectionRules object. Each rule configured will be called with a ComponentSelection object as an argument which contains information near the candidate version being considered. Calling ComponentSelection.pass up(java.lang.String) causes the given candidate version to exist explicitly rejected, in which case the candidate will not be considered for the selector.

The following instance shows a rule that disallows a particular version of a module only allows the dynamic version to choose the side by side best candidate.

Example 5. Component pick dominion

build.gradle

                      configurations {     rejectConfig {         resolutionStrategy {             componentSelection {                 // Have the highest version matching the requested version that isn't '1.5'                 all { ComponentSelection selection ->                     if (selection.candidate.group == 'org.sample' && pick.candidate.module == 'api' && selection.candidate.version == 'i.5') {                         option.refuse("version 1.5 is cleaved for 'org.sample:api'")                     }                 }             }         }     } }  dependencies {     rejectConfig "org.sample:api:ane.+" }                    

build.gradle.kts

                      configurations {     create("rejectConfig") {         resolutionStrategy {             componentSelection {                 // Accept the highest version matching the requested version that isn't '1.5'                 all {                     if (candidate.group == "org.sample" && candidate.module == "api" && candidate.version == "i.v") {                         turn down("version one.5 is broken for 'org.sample:api'")                     }                 }             }         }     } }  dependencies {     "rejectConfig"("org.sample:api:i.+") }                    

Note that version selection is applied starting with the highest version commencement. The version selected will exist the kickoff version establish that all component selection rules have. A version is considered accepted if no rule explicitly rejects information technology.

Similarly, rules can be targeted at specific modules. Modules must exist specified in the form of group:module.

Example 6. Component selection rule with module target

build.gradle

                      configurations {     targetConfig {         resolutionStrategy {             componentSelection {                 withModule("org.sample:api") { ComponentSelection selection ->                     if (option.candidate.version == "1.5") {                         selection.reject("version 1.v is broken for 'org.sample:api'")                     }                 }             }         }     } }                    

build.gradle.kts

                      configurations {     create("targetConfig") {         resolutionStrategy {             componentSelection {                 withModule("org.sample:api") {                     if (candidate.version == "i.5") {                         decline("version 1.v is broken for 'org.sample:api'")                     }                 }             }         }     } }                    

Component selection rules tin also consider component metadata when selecting a version. Possible boosted metadata that can exist considered are ComponentMetadata and IvyModuleDescriptor. Note that this actress information may not always exist available and thus should exist checked for null values.

Example 7. Component selection rule with metadata

build.gradle

                      configurations {     metadataRulesConfig {         resolutionStrategy {             componentSelection {                 // Decline any versions with a status of 'experimental'                 all { ComponentSelection selection ->                     if (pick.candidate.group == 'org.sample' && pick.metadata?.status == 'experimental') {                         selection.decline("don't use experimental candidates from 'org.sample'")                     }                 }                 // Have the highest version with either a "release" branch or a status of 'milestone'                 withModule('org.sample:api') { ComponentSelection option ->                     if (selection.getDescriptor(IvyModuleDescriptor)?.branch != "release" && selection.metadata?.status != 'milestone') {                         selection.reject("'org.sample:api' must be a release branch or take milestone status")                     }                 }             }         }     } }                    

build.gradle.kts

                      configurations {     create("metadataRulesConfig") {         resolutionStrategy {             componentSelection {                 // Reject any versions with a status of 'experimental'                 all {                     if (candidate.group == "org.sample" && metadata?.status == "experimental") {                         turn down("don't use experimental candidates from 'org.sample'")                     }                 }                 // Take the highest version with either a "release" branch or a condition of 'milestone'                 withModule("org.sample:api") {                     if (getDescriptor(IvyModuleDescriptor::class)?.branch != "release" && metadata?.condition != "milestone") {                         reject("'org.sample:api' must have testing co-operative or milestone condition")                     }                 }             }         }     } }                    

Note that a ComponentSelection argument is always required as parameter when declaring a component pick dominion.

taylorfamente.blogspot.com

Source: https://docs.gradle.org/current/userguide/dynamic_versions.html

Postar um comentário for "Maven Downloading Same Version of Dependency Again"