博客逐步迁移至 极客兔兔的小站html
Java Web应用开发时常使用Gradle来进行项目管理,能够十分便利地解决包依赖等问题。war插件的出现,让项目部署成为一个复制粘贴的过程,那有没有办法让Java web应用的部署,就像windows下安装软件,双击一下就能够呢?又或者Java web应用开发过程当中,有没有办法自动检测项目变化,自动编译与加载呢?java
gretty支持热部署、HTTPS、转发、调试、自动化运行环境等诸多特性,让开发和部署变得更加简单。本文将介绍gretty插件的最经常使用的几种特性和使用方法,适合对使用过Java web和Gradle的童鞋。若有错误,请不吝指出,很是感谢;如本文对你有用,右下角点个推荐吧!git
// JDK6+,Gradle 1.10+ // build.gradle buildscript { repositories { jcenter() } dependencies { classpath 'org.akhikhl.gretty:gretty:+' } } apply plugin: 'org.akhikhl.gretty'
或github
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
gretty { // 端口默认8080 // serlvetContainer 支持 jetty7/8/9,tomcat7/8 // contextPath 设置根路径,默认为项目名称 port = 8081 serlvetContainer = 'jetty9' contextPath = '/' }
经常使用属性web
Gretty默认以下windows
scanDir默认为下 :tomcat
${projectdir}/src/main/java
${projectdir}/src/main/groovy
${projectdir}/src/main/resources
${projectdir}/build/classes/main
${projectdir}/build/resources/mainapp
recompileOnSourceChange、reloadOnClassChange、reloadOnConfigChange 和 reloadOnLibChange默认为truewebapp
webapp/
中的内容,文件发生改变,无需重启。// 除了src/main/webapp外,可另外指定资源目录 gretty{ // ... extraResourceBase 'dir1', extraResourceBases 'dir2','dir3' // ... }
gretty { httpsEnabled = true // httpEnabled = false 禁用http // httpsPort = 443 httpsPort默认为 8443 }
certificate → "${project.buildDir}/ssl/cert"
key-store → "${project.buildDir}/ssl/keystore"
key-store and key-manager passwords→"${project.buildDir}/ssl/properties"
key-store → 配置HTTPS链接gradle
gretty { sslKeyStorePath = '/some/path/keystore' sslKeyStorePassword = 'someKeystorePassword' sslKeyManagerPassword = 'someKeyManagerPassword' sslTrustStorePath = '/another/path/trust_keystore' sslTrustStorePassword = 'someTrustStorePassword' }
<filter> <filter-name>RedirectFilter</filter-name> <filter-class>org.akhikhl.gretty.RedirectFilter</filter-class> </filter> <filter-mapping> <filter-name>RedirectFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
// 根地址转发到 index.html filter relPath: '/', { forward 'index.html' } // 旧地址转发到新地址 filter relPath: '/old/path', { redirect contextPath + '/new/path' } // 地址参数转为查询参数 filter relPath: ~'/path/(.*)', { matches -> redirect new URIBuilder(requestURI).setPath(contextPath + '/anotherPath') .setQuery(matches.relPath[0][1]) } // 将HTTP流量所有转发至HTTPS filter scheme: 'http', { redirect new URIBuilder(requestURI).setScheme('https').setPort(httpsPort) }
// 为全部的debug命令配置参数 gretty { debugPort = 5005 // 默认 debugSuspend = true // 默认 }
// 仅针对appRunDebug gretty { afterEvaluate { appRunDebug { debugPort = 5005 debugSuspend = true } } }
--build/output/${project.name} |--conf/ => 配置文件 |--runner/ => servlet container 所需库 |--starter/ |--webapps/ => java web 应用 |--restart.bat/sh |--run.bat/sh |--start.bat/sh |--stop.bat/sh
product { webapp project // include this project webapp ':ProjectA' webapp ':ProjectB' }
本文同步发布在 Github,后期将不断更新,欢迎关注。