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

问题描述 投票:5回答:2

如果当前项目仍具有快照依赖项,我想使gradle构建失败。

到目前为止,我的代码只查找java依赖项,缺少.NET项,因此它只适用于java项目。我想让它适用于所有项目。

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")
                }
            }
        })
    }
}

我需要一些帮助,以便将此代码段生成适用于所有类型的依赖项(不仅仅是java)

谢谢!

L.E.这样的事可以吗? https://discuss.gradle.org/t/how-can-i-check-for-snapshot-dependencies-and-throw-an-exception-if-some-where-found/4064

java .net gradle groovy
2个回答
1
投票

就像是

Collection<ResolvedArtifact> snapshotArtifacts = project.configurations*.resolvedConfiguration.resolvedArtifacts.filter { it.moduleVersion.id.version.endsWith('-SNAPSHOT') }
if (!snapshotArtifacts.empty) {
   // throw exception
}

https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/Configuration.html#getResolvedConfiguration-- https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ResolvedConfiguration.html#getResolvedArtifacts--


1
投票

所以我通过调整@ 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

附:但是,这并没有考虑.net依赖关系

© www.soinside.com 2019 - 2024. All rights reserved.