How can I check for snapshot dependencies and throw an exception if some where found?

How can I check the defined dependencies of the root project and all subproject for snapshot dependencies and throw an Exception if there are dependencies on snapshot. I want to add this check in my build.gradle when building/uploading releases of my project to prevent released versions from depending on snapshots.

Here is what I came up with

task maybeCheckForSnapshotDependencies {
 // Don't check for Snapshot dependencies if this is a snapshot.
 if (isSnapshot) return
 allprojects { project ->
  project.configurations.runtime.each {
   if (it.toString().contains("-SNAPSHOT"))
    throw new Exception("Release build contains snapshot dependencies: " + it)
  }
 }
}
  test { dependsOn maybeCheckForSnapshotDependencies }

I really like to avoid the String.contains() call and replace it with something like isChangingDependency(). But otherwise it seems to be working fine.

1 Like