简体   繁体   English

Gradle - 如果项目仍然具有 SNAPSHOT 依赖项,则抛出异常

[英]Gradle - throw exception if project still has SNAPSHOT dependencies

I want to fail the gradle build if the current project still has snapshot dependencies.如果当前项目仍然具有快照依赖项,我想使 gradle 构建失败

My code so far only looks for java dependencies, missing the .NET ones so it only works for java projects.到目前为止,我的代码只查找 java 依赖项,缺少 .NET 依赖项,因此它仅适用于 java 项目。 I want to make it work for all projects.我想让它适用于所有项目。

def addSnapshotCheckingTask(Project project) {
    project.tasks.withType(JavaCompile) { compileJava ->
        project.tasks.create(compileJava.name + 'SnapshotChecking', {
            onlyIf {
                project.ext.isRelease || project.ext.commitVersion != null
            }
            compileJava.dependsOn it
            doLast {
                def snapshots = compileJava.classpath
                        .filter { project.ext.isRelease || !(it.path ==~ /(?i)${project.rootProject.projectDir.toString().replace('\\', '\\\\')}.*build.libs.*/) }
                        .filter { it.path =~ /(?i)-SNAPSHOT/  }
                        .collect { it.name }
                        .unique()
                if (!snapshots.isEmpty()) {
                    throw new GradleException("Please get rid of snapshots for following dependencies before releasing $snapshots")
                }
            }
        })
    }
}

I need some help in generifying this snippet to be applicable to all types of dependencies(not just java)我需要一些帮助来泛化此代码段以适用于所有类型的依赖项(不仅仅是 Java)

Thanks!谢谢!

LE Could something like this work? LE 这样的事情可行吗? https://discuss.gradle.org/t/how-can-i-check-for-snapshot-dependencies-and-throw-an-exception-if-some-where-found/4064 https://discuss.gradle.org/t/how-can-i-check-for-snapshot-dependencies-and-throw-an-exception-if-some-where-found/4064

So I got it working by tweaking a bit the response of @lance-java, it looks something like: 所以我通过调整@ lance-java的响应来实现它,它看起来像:

    Task snapshotCheckingTask = project.tasks.create('snapshotCheckingTask', {
        doLast {
            def snapshots = new ArrayList()
            def projectConfigurations = project.configurations.findAll { true }

            projectConfigurations.each {
                if (it.isCanBeResolved()) {
                    it.resolvedConfiguration.resolvedArtifacts.each {
                        if (it.moduleVersion.id.version.endsWith('-SNAPSHOT')) {
                            snapshots.add(it)
                        }
                    }
                }
            }
            if (!snapshots.isEmpty()) {
                throw new GradleException("Please get rid of snapshots for following dependencies before releasing $snapshots")
            } else {
                throw new GradleException("Hah, no snapshots!")
            }
        }
    })
    project.tasks.release.dependsOn snapshotCheckingTask

cc @Eugene cc @Eugene

PS However, this does not take into account .net dependencies PS然而,这并没有考虑.net依赖关系

Here's what I came up with using Gradle 7.2 and the modern Kotlin DSL:这是我使用 Gradle 7.2 和现代 Kotlin DSL 的想法:

tasks.register("releaseEnforcement") {
    group = "verification"
    description = "Check whether there are any SNAPSHOT dependencies."
    doLast {
        val violations = project.configurations
            .filter { it.name == "compileClasspath" || it.name == "runtimeClasspath" }
            .flatMap { configuration ->
                configuration.resolvedConfiguration.resolvedArtifacts
                    .map { it.moduleVersion.id }
                    .filter { it.version.endsWith("-SNAPSHOT") }
            }
            .toSet()
        if (violations.isNotEmpty()) {
            error("Snapshot dependencies found:\n\n${violations.joinToString(separator = "\n")}")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM