I want to fail the gradle build if the current project still has snapshot dependencies.
My code so far only looks for java dependencies, missing the .NET ones so it only works for java projects. 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)
Thanks!
L.E. Could something like this work? https://discuss.gradle.org/t/how-can-i-check-for-snapshot-dependencies-and-throw-an-exception-if-some-where-found/4064
This is because of slow internet connection or you haven't configure proxy settings correctly. Gradle needs to download some dependencies , if it cant access the repository it fires this error. All you have to do is check your internet connection and make sure gradle can access the maven repository. Save this answer.
Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.
rootDir. The root directory of this project. The root directory is the project directory of the root project. rootProject.
implementation configuration is used for compile and runtime classpath but it is only exposed to the consumers for their runtime classpath. runtimeOnly configuration is only used for the runtime classpath and it is also exposed to the consumers for their runtime classpath.
So I got it working by tweaking a bit the response of @lance-java, it looks something like:
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
P.S. However, this does not take into account .net dependencies
Something like
Collection<ResolvedArtifact> snapshotArtifacts = project.configurations*.resolvedConfiguration.resolvedArtifacts.filter { it.moduleVersion.id.version.endsWith('-SNAPSHOT') }
if (!snapshotArtifacts.empty) {
// throw exception
}
See 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--
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With