import org.gradle.api.artifacts.result.UnresolvedDependencyResult;

buildscript {
	repositories {
		mavenLocal()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}")
	}
}

repositories {
	mavenLocal()
	mavenCentral()
}

configurations {
	springBootStarter
}

dependencies {
	springBootStarter "org.springframework.boot:${project.starter}:${project.bootVersion}"
}

apply plugin: 'spring-boot'

task checkCommonsLogging {
	doFirst {
		def commonsLogging = resolvedDependencies
				.find { it.selected.id.group == 'commons-logging' }
		if (commonsLogging) {
			throw new GradleException("${project.starter} pulls in commons-logging")
		}
	}
}

task checkSpring {
	doFirst {
		def wrongSpring = resolvedDependencies
				.findAll{it.selected.id.group == 'org.springframework'}
				.findAll{it.selected.id.version != project.springVersion}
				.collect {it.selected.id}
		if (wrongSpring) {
			throw new GradleException("${project.starter} pulled in ${wrongSpring as Set}")
		}
	}
}

def getResolvedDependencies() {
	def allDependencies = configurations.springBootStarter.incoming
			.resolutionResult.allDependencies
					.split { it instanceof UnresolvedDependencyResult }

	def unresolved = allDependencies.first()
	def resolved = allDependencies.last()
	if (unresolved) {
		throw new GradleException("Resolution of ${project.starter} failed: ${unresolved}")
	}
	resolved
}
