如果当前项目仍然具有快照 依赖项,我想失败 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
最佳答案
所以我通过稍微调整@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
抄送@尤金
附言但是,这没有考虑 .net 依赖性
关于java - Gradle - 如果项目仍然具有 SNAPSHOT 依赖项,则抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276252/