apply plugin: 'maven'

// Create a source jar for uploading
task sourceJar(type: Jar) {
	classifier = 'sources'
	from sourceSets.main.java.srcDirs
	include '**/*.java', '**/*.aj'
}

artifacts {
	archives sourceJar
}

// Configuration for SpringSource s3 maven deployer
configurations {
	deployerJars
}
dependencies {
	deployerJars "org.springframework.build.aws:org.springframework.build.aws.maven:3.0.0.RELEASE"
}

install {
	repositories.mavenInstaller {
		customizePom(pom, project)
	}
}

def customizePom(pom, gradleProject) {
	pom.whenConfigured { p ->
		p.dependencies.findAll{ it.scope == "optional" }.each {
			it.scope = "compile"
			it.optional = true
		}
		p.dependencies.findAll{ it.groupId == "org.springframework" }.each {
			it.version = null
		}
		// sort to make pom dependencies order consistent to ease comparison of older poms
		p.dependencies = p.dependencies.sort { dep ->
			"$dep.scope:$dep.optional:$dep.groupId:$dep.artifactId"
		}
	}
	def isWar = project.hasProperty('war')
	pom.project {
		name = gradleProject.name
		if(isWar) {
			packaging = "war"
		}
		description = gradleProject.name
		url = 'http://spring.io/spring-security'
		organization {
			name = 'spring.io'
			url = 'http://spring.io/'
		}
		licenses {
			license {
				name 'The Apache Software License, Version 2.0'
				url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
				distribution 'repo'
			}
		}
		scm {
			url = 'https://github.com/spring-projects/spring-security'
			connection = 'scm:git:git://github.com/spring-projects/spring-security'
			developerConnection = 'scm:git:git://github.com/spring-projects/spring-security'
		}
		developers {
			developer {
				id = 'rwinch'
				name = 'Rob Winch'
				email = 'rwinch@gopivotal.com'
			}
		}

		if(!gradleProject.name.endsWith('-bom')) {
			dependencyManagement {
				dependencies {
					dependency {
						groupId 'org.springframework'
						artifactId 'spring-framework-bom'
						version project.springVersion
						type 'pom'
						scope 'import'
					}
				}
			}
		}
		if(isWar) {
			properties {
				'm2eclipse.wtp.contextRoot' '/' + project.war.baseName
			}
		}
		if(project.snapshotBuild) {
			repositories {
				repository {
					id 'spring-snapshot'
					url 'https://repo.spring.io/snapshot'
				}
			}
		} else if(!project.releaseBuild) {
			repositories {
				repository {
					id 'spring-milestone'
					url 'https://repo.spring.io/milestone'
				}
			}
		}
	}

	// http://forums.gradle.org/gradle/topics/after_upgrade_gradle_to_2_0_version_the_maven_pom_not_support_build_property
	pom.withXml {
		def plugins = asNode().appendNode('build').appendNode('plugins')
		plugins
			.appendNode('plugin')
				.appendNode('artifactId','maven-compiler-plugin').parent()
				.appendNode('configuration')
					.appendNode('source','1.7').parent()
					.appendNode('target','1.7')
		if(isWar) {
			plugins
				.appendNode('plugin')
					.appendNode('artifactId','maven-war-plugin').parent()
					.appendNode('version','2.3').parent()
					.appendNode('configuration')
						.appendNode('failOnMissingWebXml','false')
		}
	}
}

task generatePom {
	group = 'Build'
	description = 'Generates a Maven pom.xml'

	ext.generatedPomFileName = "pom.xml"
	onlyIf { install.enabled }

	inputs.files(fileTree(project.rootProject.rootDir).include("**/*.gradle").files)
	inputs.files(new File(project.rootProject.rootDir, Project.GRADLE_PROPERTIES))
	outputs.files(generatedPomFileName)

	doLast() {
		def p = pom {}
		customizePom(p, project)
		p.writeTo(generatedPomFileName)
	}
}

build.dependsOn generatePom
