1. 程式人生 > >Gradle專案之Spring.profile.開發.測試.生產環境的配置和切換之一鍵部署

Gradle專案之Spring.profile.開發.測試.生產環境的配置和切換之一鍵部署

apply plugin: 'org.hidetake.ssh'
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.7
targetCompatibility = 1.7
group 'com.xyf'
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}
war {
    archiveName 'xyfapp.war'
}
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
} resources { srcDir 'src/main/config' } } } //version = '1.0.0.SNAPSHOT' version = '1.0.0.RELEASE' webAppDirName = 'src/main/webapp' repositories { //mavenCentral() maven { url 'http://1.1.1.1:8081/nexus/content/groups/public/' } } buildscript{ repositories { jcenter() } dependencies { classpath 'org.hidetake:gradle-ssh-plugin:2.6.0'
} } configurations { compile.exclude module: 'jcl-over-slf4j' compile.exclude module: 'XmlSchema' } dependencies { compile project(':bo') compile project(':basebo') compile group: 'com.alibaba', name: 'fastjson', version: '1.2.37' compile 'org.springframework:spring-core:4.1.2.RELEASE'
compile 'org.springframework:spring-beans:4.1.2.RELEASE' compile 'org.springframework:spring-context:4.1.2.RELEASE' compile 'org.springframework:spring-context-support:4.1.2.RELEASE' compile 'org.springframework:spring-expression:4.1.2.RELEASE' compile 'org.springframework:spring-jdbc:4.1.2.RELEASE' compile 'org.springframework:spring-tx:4.1.2.RELEASE' compile 'org.springframework:spring-web:4.1.2.RELEASE' compile 'org.springframework:spring-webmvc:4.1.2.RELEASE' compile 'org.springframework:spring-aop:4.1.2.RELEASE' compile 'org.springframework.data:spring-data-commons:1.8.4.RELEASE' compile 'org.springframework.data:spring-data-redis:1.4.1.RELEASE' compile 'org.springframework.amqp:spring-rabbit:1.4.0.RELEASE' compile 'com.fasterxml.jackson.core:jackson-core:2.1.3' compile 'com.fasterxml.jackson.core:jackson-annotations:2.1.4' compile 'com.fasterxml.jackson.core:jackson-databind:2.1.3' compile 'commons-beanutils:commons-beanutils-core:1.8.3' compile 'commons-dbcp:commons-dbcp:1.4' compile 'commons-lang:commons-lang:2.6' compile 'commons-pool:commons-pool:1.6' compile 'commons-logging:commons-logging:1.2' compile 'commons-digester:commons-digester:1.7' compile 'org.mybatis:mybatis:3.2.8' compile 'org.mybatis:mybatis-spring:1.2.2' compile 'org.quartz-scheduler:quartz:2.2.1' compile 'org.quartz-scheduler:quartz-jobs:2.2.1' compile 'commons-lang3:commons-lang3:3.4' compile 'javax.validation:validation-api:1.1.0.Final' compile 'org.hibernate:hibernate-validator:5.1.3.Final' compile 'redis.clients:jedis:2.6.1' compile 'net.sf.ehcache:ehcache:2.9.0' compile 'com.rabbitmq:amqp-client:3.4.3' compile 'org.aspectj:aspectjweaver:1.8.4' compile 'log4j:log4j:1.2.17' compile 'org.slf4j:slf4j-log4j12:1.7.7' compile 'mysql:mysql-connector-java:5.1.34' testCompile 'org.springframework:spring-test:4.1.2.RELEASE' testCompile "junit:junit:4.12" compile 'org.apache.httpcomponents:httpclient:4.3.5' compile 'org.apache.httpcomponents:httpclient-cache:4.3.5' compile 'org.apache.httpcomponents:httpmime:4.3.5' compile 'dom4j:dom4j:1.6.1' compile 'com.google.code.gson:gson:2.6.2' compile 'commons-io:commons-io:1.3.2' compile 'antlr:antlr:2.7.7' compile 'com.sdicons.jsontools:jsontools-core:1.7' compile 'org.json:json:20140107' compile 'org.bouncycastle:bcprov-ext-jdk16:1.45' compile 'xmlpull:xmlpull:1.1.3.1' compile 'xpp3:xpp3_min:1.1.4c' compile 'com.thoughtworks.xstream:xstream:1.4.1' compile 'org.bouncycastle:bcprov-jdk15:1.45' compile 'org.apache.shiro:shiro-core:1.2.2' compile 'org.apache.shiro:shiro-web:1.2.2' compile 'org.apache.shiro:shiro-spring:1.2.2' compile 'com.alibaba:aliyun-sdk-mns:1.1.8' compile 'org.apache.http:httpclient:4.4.1' compile 'org.apache.http:httpcore:4.4.1' compile 'org.apache.http:httpcore-nio:4.4.1' compile 'org.apache.http:httpasyncclient:4.1' compile 'com.swetake:swetakeQRcode:1.1' providedCompile 'javax.servlet:servlet-api:2.5' providedRuntime 'javax.servlet:jstl:1.2' compile group: 'taglibs', name: 'standard', version: '1.1.2' // https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3' compile fileTree(dir: 'src/main/webapp/WEB-INF/lib', include: '*.jar') compile group: 'jdom', name: 'jdom', version: '1.0' compile group: 'com.google.zxing', name: 'core', version: '3.3.0' compile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b08' compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.6.5' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.36' } ssh.settings { knownHosts = allowAnyHosts } remotes { deployServer_test { host = '1.1.1.1' user = 'root' password = 'password' } deployServer_pro { host = '2.2.2.2' user = 'root' password = 'password' } } //測試部署 task shutdownTomcat_test() { doLast{ ssh.run { session(remotes.deployServer_test) { println 'shutdown tomcat...' executeScript '''#!/bin/sh cd /app/t8080/bin ./shutdown.sh ''' } } } } task del_test(dependsOn:shutdownTomcat_test) { doLast{ ssh.run { session(remotes.deployServer_test) { println 'start deleting...' executeScript '''#!/bin/sh rm -rf /app/backups/xyfapp mv /app/t8080/webapps/xyfapp /app/backups/ rm -f /app/t8080/webapps/xyfapp.war ''' } } } } task copy_test(dependsOn:del_test) { doLast{ ssh.run { session(remotes.deployServer_test) { println 'start copying war...' put from: buildDir.toString() + '/libs/xyfapp.war', into: '/app/t8080/webapps' executeScript '''#!/bin/sh cd /app/t8080/webapps unzip -oq xyfapp.war -d xyfapp/ ''' } } } } task deploy_test(dependsOn:copy_test) { doLast{ ssh.run { session(remotes.deployServer_test) { println 'start tomcat...' executeScript '''#!/bin/sh cd /app/t8080/bin ./startup.sh ''' } } } } //生產部署 task shutdownTomcat_pro() { doLast{ ssh.run { session(remotes.deployServer_pro) { println 'shutdown tomcat...' executeScript '''#!/bin/sh cd /xyf/t8080/bin ./shutdown.sh ''' } } } } task del_pro(dependsOn:shutdownTomcat_pro) { doLast{ ssh.run { session(remotes.deployServer_pro) { println 'start deleting...' executeScript '''#!/bin/sh rm -rf /xyf/backups/xyfapp mv /xyf/t8080/webapps/xyfapp /xyf/backups/ rm -f /xyf/t8080/webapps/xyfapp.war ''' } } } } task copy_pro(dependsOn:del_pro) { doLast{ ssh.run { session(remotes.deployServer_pro) { println 'start copying war...' put from: buildDir.toString() + '/libs/xyfapp.war', into: '/xyf/t8080/webapps' executeScript '''#!/bin/sh cd /xyf/t8080/webapps unzip -oq xyfapp.war -d xyfapp/ ''' } } } } task deploy_pro(dependsOn:copy_pro) { doLast{ ssh.run { session(remotes.deployServer_pro) { println 'start tomcat...' executeScript '''#!/bin/sh cd /xyf/t8080/bin ./startup.sh ''' } } } }