apply plugin: 'maven'
apply plugin: 'signing'

ext {
	resolveRepoName = { Project project ->
		String version = project.version
		String name = project.name
		resolvedRepoName = "plugins-${resolveVersion(version)}-local"
		logger.lifecycle("For project [$project.name] with " +
				"version [$project.version] the resolved Artifactory repo is [$resolvedRepoName]")
		return resolvedRepoName
	}

	resolveVersion = { String version ->
		if (version.endsWith('BUILD-SNAPSHOT')) return 'snapshot'
		if (version.matches('[0-9].[0-9].[0-9].M[0-9]+')) return 'milestone'
		if (version.matches('[0-9].[0-9].[0-9].RC[0-9]+')) return 'milestone'
		if (version.endsWith('RELEASE')) return 'release'
		return 'snapshot'
	}

	isReleaseVersion = !version.endsWith("SNAPSHOT")
}

uploadArchives.dependsOn { [check] }

ext {
	repositoryUrl = "https://repo.spring.io/${repoPrefix()}-${resolveVersion(version)}-local"
	snapshotRepositoryUrl = "https://repo.spring.io/${repoPrefix()}-snapshot-local"
	repoUrl = isReleaseVersion ? repositoryUrl : snapshotRepositoryUrl
}

afterEvaluate {
	uploadArchives {
		repositories {
			mavenDeployer {
				// POM signature
				if (isReleaseVersion) {
					beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
				}
				// Target repository
				repository(url: repoUrl) {
					authentication(userName: repoUser, password: repoPass)
				}
				pom.project {
					name "$project.name"
					packaging 'jar'
					description 'Consumer Driven Contract Verifier and Stub Runner'
					url 'https://github.com/spring-cloud/spring-cloud-contract'
					inceptionYear '2014'

					scm {
						connection 'scm:git:git@github.com:spring-cloud/spring-cloud-contract.git'
						developerConnection 'scm:git:git@github.com:spring-cloud/spring-cloud-contract.git'
						url 'https://github.com/spring-cloud/spring-cloud-contract'
					}

					licenses {
						license {
							name 'The Apache License, Version 2.0'
							url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
						}
					}

					developers {
						developer {
							id 'jkubrynski'
							name 'Jakub Kubrynski'
							email 'jk ATT codearte DOTT io'
						}
						developer {
							id 'marcingrzejszczak'
							name 'Marcin Grzejszczak'
							email 'marcin ATT grzejszczak DOTT pl'
						}
					}
				}
			}
		}
	}
}

String repoPrefix() {
	return project.name.contains('gradle') ? 'plugins' : 'libs'
}