gradle-crossbuild-scala
gradle-crossbuild-scala copied to clipboard
Feature 'dependency validation'
Hey i had written some code in my free time to validate if all dependencies are not depending on a scala version without a scala version tag. This might be usefull for companies that just started to cross compile there libs.
def traceDependency(DependencyResult d, List<DependencyResult> dfrom) {
def deps = d.from.dependents
if (deps.size() == 0 || d.from.toString().startsWith("project")) {
dfrom.plus(d.from).reverse().join(" -> ")
} else {
deps.collect { traceDependency(it, dfrom.plus(d.from)) }.flatten()
}
}
task validateScalaDependencies {
doLast {
def problems = project.subprojects.collect { subProject ->
if (subProject.configurations.getNames().contains("runtimeClasspath")) {
def dependencies = subProject.configurations.runtimeClasspath.incoming.getResolutionResult().getAllDependencies()
def untaggedScalaDependenciesTraces = dependencies.findAll {
String[] groupNameVersion = it.from.toString().split(':')
String[] requestedGroupNameVersion = it.requested.toString().split(':')
//for dependnecies that depend on scala
(requestedGroupNameVersion[0] == "org.scala-lang"
// without a scala tag
&& !(
groupNameVersion[1].endsWith("_2.12")
|| groupNameVersion[1].endsWith("_2.11")
|| groupNameVersion[1].endsWith("_2.10"))
//ignore projects
&& !it.from.toString().startsWith("project")
//ignore scala libs
&& groupNameVersion[0] != "org.scala-lang"
)
}.collect {
"${it.from} traces: \n -- ${traceDependency(it, [it.requested]).join("\n -- ")}"
}
println("")
if (untaggedScalaDependenciesTraces.size() != 0) {
println("${subProject.name} : CROSS COMPILATION ERRORS :(")
println(" * ${untaggedScalaDependenciesTraces.join("*")}")
return false
} else {
println("${subProject.name} : IS OK :)")
return true
}
}
}.contains(false)
if(problems) {
throw new GradleException("INVALID CROSS BUILD FAILED")
}
}
}