apply plugin: 'java'
def basedir='.'
def projectsDir='../..'
//获取什么分支名
FileTree files =fileTree(dir:"./",include:"build.gradle")
def buildDir=files[0].path.substring(0,files[0].path.lastIndexOf ("\\"))
repositories{
	mavenCentral()
}


configurations{
	ftpAntTask
}
//添加ftp依赖
dependencies {
	ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
		module("commons-net:commons-net:1.4.1") {
			dependencies "oro:oro:2.0.8:jar"
		}
	}
}
//脚本中的依赖
buildscript{
	repositories { mavenCentral() }
	dependencies {
		classpath fileTree(dir:"../../lib",include:"*/*.jar")
	}
}


ant{
	taskdef(name: 'ftp',
                classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
               classpath: configurations.ftpAntTask.asPath)
}
task prepare<<{
	ant{

		mkdir(dir:'plugins')
		mkdir(dir:'libs')
		delete(file:'libs/*.jar')
		delete(includeemptydirs:"true"){
			fileset(dir:"${basedir}/plugins",includes:"**/*")
		}
		echo(message:'dwnload latest jars')
		ftp(server:'192.168.5.86',userid:'fr',password:'ilovejava',remotedir:'report/stable',action:'get'){
			fileset(dir:'./libs/',includes:'*.jar')
		}
	}
}
FileTree buildFile=fileTree(dir:"./",include:"build.xml")

println "--------------------------${buildFile}"
	
//构建每个plugin
task buildAllprj(dependsOn:prepare)<<{
	ant{
		for(file in buildFile){
			def pluginHome=file.path.substring(0,file.path.lastIndexOf ("\\"))
			def pluginName=pluginHome.substring(pluginHome.lastIndexOf ("\\")+1)
			println "pluginHome+++++++++++++++++++++++++++++++${pluginHome}"
			println "pluginName+++++++++++++++++++++++++++++++${pluginName}"

			echo(message:"-----------------------------开始构建${pluginName}----------------------------")
			try{
				sequential(){

					echo(message:pluginHome+"/lib")
					ant.mkdir(dir:pluginHome+"/lib")
					ant.mkdir(dir:pluginHome+"/src")
					echo(message:"build file:@{file}")
					ant(antfile:"${file}",inheritall:"false"){

						property(name:"jdk.home",value:"D:/FineReport/develop/java/jdk1.7")
						property(name:"jdk1.8.home",value:"D:/FineReport/develop/java/jdk1.8")
						property(name:"jdk1.7.home",value:"D:/FineReport/develop/java/jdk1.7")
						property(name:"jdk1.6.home",value:"D:/FineReport/develop/java/jdk1.6u35")
						property(name:"jdk1.5.home",value:"D:/FineReport/develop/java/jdk1.5")
						property(name:"publicLibs",value:"./libs")
						property(name:"destLoc",value:"../plugins")

					}
				}


			}
			catch(Exception e){
				//捕获异常报错
              	print(e)
			}
			echo(message:"-----------------------------${pluginName}构建结束----------------------------")
		}
		delete(dir:'libs')
	}
}
ant{
	fileset(id:'ftp.upload.fileset',dir:'../plugins'){
		include(name:'**/*.zip')
	}
}

//上传到ftp
task ftp_upload(dependsOn:buildAllprj)<<{
	FileTree plugins=fileTree(dir:'../plugins',include:'*/**')

	ant{
		echo(message:"ready for upload plugins")
		echo(message:"ftp target is 192.168.5.86")
		mkdir(dir:'E:/ftp/share/plugins/stable')
		plugins.each{File file->
			def zipDir=file.path.substring(0,file.path.lastIndexOf ("\\"))
			def pluginName=zipDir.substring(zipDir.lastIndexOf ("\\")+1)
			ant.delete(includeEmptyDirs:'true',dir:"E:/ftp/share/plugins/stable/${pluginName}")
		}
		buildFile.each{File file->
			def pluginHome=file.path.substring(0,file.path.lastIndexOf ("\\"))
			FileTree pluginFile=fileTree(dir:"${pluginHome}",include:"*/fr-plugin-*.jar")
			pluginFile.each{File jarFile->
				ant.echo(message:"${jarFile.path}")
				def jarHome=jarFile.path.substring(0,jarFile.path.lastIndexOf ("\\"))
				ant.echo(message:"start delete ${jarHome} ")
				ant.delete(dir:"${jarHome}")
			}
			ant.delete(){
				fileset(dir:"${pluginHome}"){
					include(name:"fr-plugin-*.jar")
				}
			}
		}

		copy(todir:'E:/ftp/share/plugins/stable'){
			fileset(refid:'ftp.upload.fileset')
		}
	}
}