首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle:是否有一种方法可以在传递依赖项中不允许版本范围?

Gradle:是否有一种方法可以在传递依赖项中不允许版本范围?
EN

Stack Overflow用户
提问于 2014-10-04 06:27:54
回答 1查看 795关注 0票数 3

当我在Gradle配置中声明了快照存储库时,是否有一种方法可以防止版本范围从解析到快照版本之间的传递依赖关系?或者,我是否可以完全禁止传递依赖项中的版本范围?例如,考虑这个非常简单的Gradle项目:

代码语言:javascript
复制
repositories {
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

apply plugin: "java"

dependencies {
    compile "org.reactfx:reactfx:1.4"
    compile "org.fxmisc.undo:undofx:1.0.1"
}

这将导致版本冲突和解决,因为undofx依赖于reactfx和版本[1.4,1.5) (最近的1.4.x版本可用)。下面是reactfx的依赖洞察力

代码语言:javascript
复制
gradlew dependencyInsight --dependency reactfx
代码语言:javascript
复制
org.reactfx:reactfx:1.4.1-SNAPSHOT (conflict resolution)

org.reactfx:reactfx:1.4 -> 1.4.1-SNAPSHOT
\--- compile

org.reactfx:reactfx:[1.4,1.5) -> 1.4.1-SNAPSHOT
\--- org.fxmisc.undo:undofx:1.0.1
     \--- compile

Maven还将将reactfx:1.4.1-SNAPSHOT解析为undofx的依赖项。但是,一旦将reactfx依赖项添加到项目中,Maven就会使用我声明为第一级依赖项的版本来解决冲突。下面是我用来测试它的Maven POM:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>example</groupId>
    <artifactId>example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>maven-central</id>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
        <repository>
            <id>oss-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.fxmisc.undo</groupId>
            <artifactId>undofx</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.reactfx</groupId>
            <artifactId>reactfx</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

</project>

这也是我对Gradle期望的那种解决问题的行为,但这只是基于假设。我想,如果undofx允许任何1.4.x版本的reactfx,并且唯一的其他声明版本的reactfx在这个范围内,冲突将通过使用我声明的版本来解决。

但是,如果任何传递依赖正在使用ranged版本,那么我对冲突解决的兴趣就不如失败构建。我更愿意识别那些依赖项并将它们设置为特定的版本。如果我没有创建上面的冲突,我想我不会注意到这个使用版本范围的版本。

使用版本范围识别和处理传递依赖关系的最简单方法是什么?

已更新

根据Peter的回答,这里列出了我用来做这件事的代码的完整列表。注意,它使用的是标记为@Incubating的Gradle API的特性。

代码语言:javascript
复制
import org.gradle.api.artifacts.component.ModuleComponentSelector

if(!project.plugins.hasPlugin(JavaPlugin)) {
    apply plugin: "java"
}

repositories {
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

dependencies {
    compile "org.fxmisc.undo:undofx:1.0.1" // depends on reactfx:[1.4,1.5)
}

configurations {
    //noinspection GroovyAssignabilityCheck
    all {
        /*
        Once the dependencies in all configurations are resolved check the
        version of all module (not project) components that were resolved
        successfully.  Modules already using a forced version will be skipped.
         */
        incoming.afterResolve { // ResolvableDependencies
            it.resolutionResult.allDependencies { // DependencyResult
                if(it instanceof ResolvedDependencyResult
                        && it.requested instanceof ModuleComponentSelector) {
                    if(!it.selected.selectionReason.forced) {
                        checkVersion((ModuleComponentSelector) it.requested)
                    }
                }
            }
        }
    }
}

/**
 * Check the version of the requested module and throw and exception if it's
 * using a version range.
 *
 * @param requested The module component to check.
 */
void checkVersion(ModuleComponentSelector requested) {
    def version = requested.version

    if(version.endsWith(")")
            || version.equals("LATEST")
            || version.equals("RELEASE")) {
        throw new GradleException(
                "${requested} uses a version range.  Try force.")
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-04 06:51:40

您可以使用configuration.getIncoming() API将声明的版本与解析的版本进行比较(例如,在configuration.getIncoming().afterResolve()钩子中),如果它们不相同,则失败。要获得“声明版本胜利”(而不是Gradle的“最高版本胜利”)的Maven冲突解决行为,可以使用configuration.getResolutionStrategy().force() API。要强制每个版本冲突都显式地解决(使用force()),请使用configuration.getResolutionStrategy().failOnVersionConflict()。有关API的详细信息,请参阅分级构建语言参考

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26190313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档