1.打包普通jar依赖包
apply plugin: 'java'
////////依赖设置////////
dependencies {
compile group: 'org.jboss.netty', name: 'netty', version: '3.2.7.Final'
}
////////测试设置////////
task testAll() {
//命令行执行, 在gradle的runtime下执行测试(保证所有的包已被引用)
doLast {
javaexec {
main = "org.junit.runner.JUnitCore"; args = ['com.test.tests.AllTests']
classpath configurations.runtime + sourceSets.main.output + sourceSets.test.output
workingDir("../")
}
}
}
//跳过自带测试, 全部采用testAll的命令行方式(自带的无法针对单类测试)test {
exclude '**'
}
2.打包可执行jar包
apply plugin: 'java'
////////依赖设置////////
dependencies {
compile group: 'org.perf4j', name: 'perf4j', version: '0.9.16'
}
jar {
from {
(configurations.runtime).collect {
it.isDirectory() ? it : zipTree(it)
}
}
//此行是当出现Invalid signature file digest for Manifest main attributes错误时添加
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
manifest {
attributes(
'Main-Class': 'com.yummy.pressure.entry.PressureServer'
)
}
}
////////测试设置////////
//跳过测试
test {
exclude '**'
}
3.打包war包
build.gradle
apply plugin: 'java'
apply plugin: 'war'
////////依赖设置////////
dependencies {
compile project(':test-service')
compile project(':test-common')
compile group: 'org.perf4j', name: 'perf4j', version: '0.9.16'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
}
////////测试设置////////
//跳过测试
test {
exclude '**'
}
setting.gradle
所有要编译的包
include "test-common", "test-service", "test-web"
4.发布打包好的包
apply plugin: 'java'
apply plugin: 'idea'
////////子项目公用配置
subprojects {
apply plugin: 'idea'
apply plugin: 'java'
idea {
module {
downloadSources = true
}
}
//中央库设置
repositories {
//本地中央库
mavenLocal()
//远程中央库
mavenRepo name: 'Public Repositories', url: "http://192.168.161.26:8080/nexus/content/groups/NewYork-public/"
}
}
//部署所有代码到内网开发机器
task deployAll(dependsOn: ["deployToolDev", "deployDev"]) {
doLast {
}
}
//部署tool代码到内网开发机器
task deployToolDev(dependsOn: ["test-tool-web:build"]) {
doLast {
//部署代码
exec {
executable "sh"
args "-c", "rsync -ztrvlC --delete test-tool-web/build/libs/test-tool-web.war " +
"root@192.168.161.46:/opt/local/jetty/standalone/testToolDev/webapps/test-tool-dev.war"
}
// 远程执行
exec {
executable "sh"
args "-c", "ssh root@192.168.161.46 /opt/local/jetty/standalone/testToolDev/server.sh switch"
}
}
}
//部署game web代码到内网开发机器
task deployDev(dependsOn: ["test-web:build"]) {
doLast {
//部署代码
exec {
executable "sh"
args "-c", "rsync -ztrvlC --delete test-web/build/libs/test-web.war " +
"root@192.168.161.46:/opt/local/jetty/standalone/testDev/webapps/test-dev.war"
}
// 远程执行
exec {
executable "sh"
args "-c", "ssh root@192.168.161.46 /opt/local/jetty/standalone/testDev/server.sh switch"
}
}
}
//部署代码到hudson, 并执行所有测试
task deployHudson(dependsOn: ["deployHudson_sql", "deployHudson_config"]) {
doLast {
/****************************tool**********************/
//部署代码
exec {
executable "sh"
args "-c", "rsync -ztrvlC --delete test-tool-web/build/libs/test-tool-web.war root@192.168.161.17:/opt/local/jetty/standalone/testToolHudson/webapps/game.war"
}
//远程执行
exec {
executable "sh"
args "-c", "ssh root@192.168.161.17 /opt/local/jetty/standalone/testToolHudson/server.sh switch"
}
//恢复配置连接服务器为默认
exec {
executable "sh"
args "-c", "git checkout -- test-tool-web/src/main/webapp/WEB-INF/config-manager-info.xml"
}
/****************************web**********************/
//部署代码
exec {
executable "sh"
args "-c", "rsync -ztrvlC --delete test-web/build/libs/test-web.war root@192.168.161.17:/opt/local/jetty/standalone/testHudson/webapps/game.war"
}
//远程执行
exec {
executable "sh"
args "-c", "ssh root@192.168.161.17 /opt/local/jetty/standalone/testHudson/server.sh switch"
}
//恢复配置连接服务器为默认
exec {
executable "sh"
args "-c", "git checkout -- test-web/src/main/webapp/WEB-INF/config-manager-info.xml"
}
//执行所有测试
project(":test-tool-web").tasks.testAll.execute()
project(":test-idworker-web").tasks.testAll.execute()
//等待10秒
exec {
executable "sh"
args "-c", "sleep 10"
}
project(":test-service").tasks.testAll.execute()
}
}
task deployHudson_sql(dependsOn: ["test-tool-web:build", "test-web:build"]) {
doLast {
//部署数据库
exec {
executable "sh"
args "-c", "mysql -uroot -pabcdefg -h1192.168.161.16 --secure_auth=off test_tool_hudson < test-tool-web/src/main/webapp/WEB-INF/db.sql"
}
}
doLast {
//部署数据库
exec {
executable "sh"
args "-c", "mysql -uroot -pabcdefg -h192.168.161.16 --secure_auth=off test_hudson < test-web/src/main/webapp/WEB-INF/db.sql"
}
}
}
task deployHudson_config() {
doLast {
//使用tool hudson配置文件
exec {
executable "sh"
args "-c", "cp -rf test-tool-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-tool-web/src/main/webapp/WEB-INF/config-manager-info.xml"
}
//使用idworker hudson配置文件
exec {
executable "sh"
args "-c", "cp -rf test-idworker-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-idworker-web/src/main/webapp/WEB-INF/config-manager-info.xml"
}
//使用web hudson配置文件
exec {
executable "sh"
args "-c", "cp -rf test-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-web/src/main/webapp/WEB-INF/config-manager-info.xml"
}
}
}
5.shell给gradle传参
#传参使用-P参数,P后面是key=value格式,即gradle脚本里使用的变量名称
gradle deploy${env} -Pauthor=$HUDSON_USER --info