import org.apache.commons.configuration2.builder.fluent.Parameters import org.apache.commons.configuration2.* import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler import groovy.json.JsonBuilder import groovy.json.JsonSlurper import groovy.json.StringEscapeUtils apply plugin: 'java' apply plugin: 'war' println 'Gradle....开始编译' println "运行的gradle版本: $gradle.gradleVersion" buildscript { repositories { mavenCentral() } dependencies { classpath 'org.apache.commons:commons-configuration2:2.1.1', 'commons-beanutils:commons-beanutils:1.9.3'} } //获取当前运行的文件夹,及ROOT文件夹的路径。必须在ROOT下运行 def webAppRootDir = System.getProperty("user.dir") ext.webAppRootDir = webAppRootDir //System.getProperty("user.dir") // 个性化lib文件的存放路径 ext.customLibPath = System.getenv('CUSTOM_LIB_PATH') // 消毒追溯系统的父项目的工程文件所在的路径 ext.sstsProjectPath = System.getenv('SSTS_PROJECT_PATH') //读取version.properties文件中的版本信息 File propsFile = new File(webAppRootDir + "/version.properties") def props = new Properties() props.load(propsFile.newDataInputStream()) //全局版本 ext.globalVersion = props.getProperty("globalVersion") ext.svnRevision = props.getProperty("svnRevision") println "发布包的globalVersion: " + globalVersion println "发布包的svn版本: " + svnRevision println "webAppRootDir: " + webAppRootDir println "环境变量CUSTOM_LIB_PATH: " + customLibPath println "环境变量SSTS_PROJECT_PATH: " + sstsProjectPath if(customLibPath == null){ throw new GradleException('需要的环境变量CUSTOM_LIB_PATH没有设置,编译终止!') } if(sstsProjectPath == null){ throw new GradleException('需要的环境变量SSTS_PROJECT_PATH没有设置,编译终止!') } def getDate() { println "调用 getDate()" def date = new Date() def formattedDate = date.format('yyyy-MM-dd HH:mm:ss') return formattedDate } Parameters params = new Parameters() char aChar = ',' FileBasedConfigurationBuilder jdbcBuilder = new FileBasedConfigurationBuilder(PropertiesConfiguration.class) .configure(params.properties() .setFileName(webAppRootDir + "/WEB-INF/classes/jdbc.properties") .setListDelimiterHandler(new DefaultListDelimiterHandler(aChar))); Configuration jdbcConfig = jdbcBuilder.getConfiguration() ext.jdbcConfig = jdbcConfig ext.jdbcBuilder = jdbcBuilder // 获取jdbc.properties文件中定义的项目的名字 def projectName = jdbcConfig.getString("project") ext.webAppLibDir = webAppRootDir + "/WEB-INF/lib" ext.webInfDir = webAppRootDir + "/WEB-INF" // 检测命令行中是否指定了newProjectName参数 if (project.hasProperty('newProjectName')) { println "检测到命令行输入参数,newProjectName = " + project.newProjectName newProjectName = project.newProjectName jdbcConfig.setProperty("project", newProjectName) } else{ throw new GradleException('没有命令行参数newProjectName,程序退出!') } projectName = newProjectName ext.projectName = projectName buildscript { repositories { mavenCentral() } dependencies { classpath group: 'org.zeroturnaround', name: 'gradle-jrebel-plugin', version: '1.1.2' } } //TODO:拷贝文件 ext.webAppClassesDir = "../ssts-web/src/main/webapp/WEB-INF/classes" configurations { // other configurations e.g. - providedCompile.exclude module: 'commons-logging' all*.transitive = false } // 1. Get dependencies from Maven local repository // 2. Get dependencies from Maven central repository repositories { ivy { // URL can refer to a local directory url "../localrepo/" } mavenLocal() mavenCentral() jcenter() } war{ buildProject() archiveName 'ssts-web-' + projectName + '-' + globalVersion + '-R' + svnRevision + '.war' destinationDir = file("${sstsProjectPath}/ssts-web/build/libs") from '.' exclude 'build' exclude '.gradle' exclude 'build.gradle' exclude 'gradle' exclude 'gradlew' exclude 'gradlew.bat' // 排除webapp/disinfectsystem/config文件夹下非本项目的配置文件 new File(webAppRootDir + "/disinfectsystem/config").eachDir{ if (it.name != projectName && it.name != 'default'){ exclude 'disinfectsystem/config/' + it.name + '/**' } } println "发布包路径:${destinationDir},文件名:${archiveName}" //copy the files from /src/conf to the /conf folder in the war file. //from("src/conf"){ // into("/conf") //} } def prepareSpringSecurityXmlFile_Method(){ println "运行任务2:准备spring security xml文件,使用标准的或者项目定制的xml文件" //将spring security xml从folder1或者folder2拷贝到WEB-INF/spring文件夹 //folder1:src\main\webapp\disinfectsystem\config\{projectName}\spring-security //folder2:src\main\webapp\WEB-INF\spring-security-standard def springSecurityFileCustomized = webAppRootDir + "/disinfectsystem/config/" + projectName + "/spring/security/applicationContext-acegi-security.xml" def springSecurityFileStandard = webAppRootDir + "/WEB-INF/spring/security-standard/applicationContext-acegi-security.xml" def existCustomizedFile = file(springSecurityFileCustomized).exists() println "项目个性化的springSecurity文件是否存在 = " + existCustomizedFile def springSecurityFileSource = springSecurityFileStandard if (existCustomizedFile){ springSecurityFileSource = springSecurityFileCustomized } def springSecurityDirTarget = webAppRootDir + "/WEB-INF/spring" // 如果spring security的目标文件夹不存在,则创建 File file = new File(springSecurityDirTarget); if (!file.exists()) { file.mkdir(); } copy { from springSecurityFileSource into springSecurityDirTarget } } def copyToWebappClasses_Method(){ println "运行任务3:copyToWebappClasses:拷贝classes与资源文件到WEB-INF/classes" // 直接拷贝resources文件夹的内容到WEB-INF/classes copy { from webAppRootDir + "/src/main/resources" into webAppClassesDir include '**/*.*' } // TODO:需要把classes文件拷贝到WIN-INF/classes下面 copy { from './build/classes/java/main' into webAppClassesDir include '**/*.*' } } // 更新lib包文件 // 先删除WEB-INF\lib下的各个项目的个性化依赖的包,公共依赖的包不删除。 // 然后拷贝新项目的个性化依赖的包到WEB-INF\lib文件夹,采用查表法拷贝,即根据项目名称查表获得该项目依赖的包 def updateLibFiles_Method(){ println "运行任务:updateLibFiles_Method:更新项目依赖的jar包到WEB-INF/lib" //读取项目个性化lib包的定义文件 def libsJsonFile = file("${sstsProjectPath}/ssts-web/projectCustomLibs.json") def projectCustomLibList = new groovy.json.JsonSlurper().parseText(libsJsonFile.getText('UTF-8')) def toCopyLibs = null // 遍历配置,删除WEB-INF\lib下的个性化jar包 projectCustomLibList.eachWithIndex { projectLibsConfig, index -> println "遍历配置文件:projectCustomLibs.json" println "配置项目${index+1}: ${projectLibsConfig.projectName} ${projectLibsConfig.libs}" // 遍历项目的lib包定义,删除所有个性化的lib包 projectLibsConfig.libs.eachWithIndex { myLibsConfig, myIndex -> if ("project" == "${myLibsConfig.type}"){ println "name = ${myLibsConfig.name}" delete fileTree(webAppLibDir) { include "${myLibsConfig.name}*.jar" } } else if ("maven" == "${myLibsConfig.type}"){ println "group = ${myLibsConfig.name.group},name = ${myLibsConfig.name.name},version = ${myLibsConfig.name.version}" //compile group: "${myLibsConfig.name.group}", name: "${myLibsConfig.name.name}", version: "${myLibsConfig.name.version}" delete fileTree(webAppLibDir) { include "${myLibsConfig.name.name}-${myLibsConfig.name.version}.jar" } } } if (projectName == "${projectLibsConfig.projectName}"){ println "projectName = " + projectName println "项目${projectLibsConfig.projectName}具有个性化的lib包,个性化lib如下:" // 遍历项目的lib包定义 toCopyLibs = projectLibsConfig.libs println "toCopyLibs1=${toCopyLibs}" } } // 拷贝本项目个性化的jar包到WEB-INF\lib if (toCopyLibs != null){ println "toCopyLibs2=${toCopyLibs}" toCopyLibs.eachWithIndex { myLibsConfig, myIndex -> println "${projectName}个性化jar包${myIndex + 1}: type = ${myLibsConfig}.type," def jarFile = null if ("project" == "${myLibsConfig.type}"){ jarFile = "${myLibsConfig.name}-${globalVersion}.jar" } else if ("maven" == "${myLibsConfig.type}"){ jarFile = "${myLibsConfig.name.name}-${myLibsConfig.name.version}.jar" } if (!file("${customLibPath}/${svnRevision}/${projectName}/${jarFile}").exists()){ throw new GradleException("${customLibPath}/${svnRevision}/${projectName}/${jarFile}文件不存在,切换项目失败!") } else{ println "拷贝文件${jarFile}到${webAppLibDir}" copy { from "${customLibPath}/${svnRevision}/${projectName}" into webAppLibDir include jarFile } } } } } def prepareWebXmlFile_Method(){ println "运行任务5:prepareWebXmlFile:准备web.xml文件,使用标准的或者项目定制的web.xml文件" def webXmlFileCustomized = webAppRootDir + "/disinfectsystem/config/" + projectName + "/web-xml/web.xml" def webXmlFileStandard = webAppRootDir + "/WEB-INF/web-xml-standard/web.xml" def existCustomizedFile = file(webXmlFileCustomized).exists() println "项目个性化的web.xml文件是否存在 = " + existCustomizedFile def webXmlFileSource = webXmlFileStandard if (existCustomizedFile){ webXmlFileSource = webXmlFileCustomized } // 拷贝各项目的web.xml到WEB-INF文件夹 copy { from webXmlFileSource into webInfDir } } // 根据jdbc.properties文件里面的projectName,拷贝 def buildProject(){ //prepareSpringSecurityXmlFile.dependsOn cleanDir // 拷贝spring security及logon.jsp文件 println "开始切换项目,请稍候......" getDate() //确保存在ROOT/disinfectsystem/config/{projectName}文件夹 def projectConfigFolder = webAppRootDir + "/disinfectsystem/config/" + projectName + "/" def existProjectConfigFolder = file(projectConfigFolder).exists() if (!existProjectConfigFolder){ throw new GradleException("ROOT/disinfectsystem/config文件夹下没有属于项目${projectName}的文件夹," + "程序退出!") } prepareSpringSecurityXmlFile_Method() // 更新lib包文件 updateLibFiles_Method() // 拷贝web.xml文件 prepareWebXmlFile_Method() //更新jdbc.properties文件中的project名字 jdbcConfig.setProperty("project", projectName) jdbcBuilder.save() } /* task createWar(type : War){ buildProject() archiveName 'ssts-web-' + projectName + '-' + globalVersion + '-R' + svnRevision + '.war' destinationDir = file("${sstsProjectPath}/ssts-web/build/libs") from '.' exclude 'build' exclude '.gradle' exclude 'build.gradle' exclude 'gradle' exclude 'gradlew' exclude 'gradlew.bat' // 排除webapp/disinfectsystem/config文件夹下非本项目的配置文件 new File(webAppRootDir + "/disinfectsystem/config").eachDir{ if (it.name != projectName && it.name != 'default'){ exclude 'disinfectsystem/config/' + it.name + '/**' } } //copy the files from /src/conf to the /conf folder in the war file. //from("src/conf"){ // into("/conf") //} } */