gradle的gretty插件

既然gradle2.0内置的jetty插件只支持jetty 6.1.25版本,而jetty 6.1.25是不支持servlet 3的新特性,那也要找解决方法吧.<<gradle的jetty插件>>也提出可以使用第三方插件gretty,它不仅支持jetty servlet容器,还支持tomcat servlet等多种容器.配置也相对简单.
gretty的document:http://akhikhl.github.io/gretty-doc/index.html


下文是一个使用gretty插件的例子:

1.build.gradle

apply plugin: "war"
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:1.2.4'
    }
}
apply plugin: 'org.akhikhl.gretty'
group = "org.exam"
version = "1.0"
ext {
    jvmVersion="1.8"
    springVersion = "4.2.1.RELEASE"
    sl4jVersion="1.7.7"
}
repositories {
    mavenCentral()
}
[compileJava, compileTestJava, javadoc]*.options*.encoding = "UTF-8"
configurations {
    all*.exclude module: 'commons-logging'
}
dependencies {
    compile("org.slf4j:jcl-over-slf4j:$sl4jVersion")
    compile("org.slf4j:slf4j-log4j12:$sl4jVersion")
    compile("org.springframework:spring-webmvc:$springVersion")
    providedCompile("javax.servlet:javax.servlet-api:3.1.0")
    compile("commons-fileupload:commons-fileupload:1.3.1")
    compile("com.fasterxml.jackson.core:jackson-databind:2.3.1")
    compile("org.apache.taglibs:taglibs-standard-impl:1.2.1")
    testCompile("org.springframework:spring-test:$springVersion")
    testCompile("junit:junit:4.12")
}
/* 解决设置版本不起作用问题 */
tasks.withType(JavaCompile) {
    sourceCompatibility = jvmVersion
    targetCompatibility = jvmVersion
}
gretty {
    port = 8080
    contextPath ="/${project.name}"
    servletContainer = 'jetty9'
}

a.加入greety配置.在build.gradle加上2-9行是gretty插件的配置.
b.最后五行是gretty是适配tomcat或jetty的配置.其中不配置servletContainer,默认为jetty9,这个值可以是'jetty7', 'jetty8', 'jetty9', 'tomcat7', 'tomcat8'
2.导入当前这个gradle项目到IDEA去开发.
3.启动测试:
双击gradle面板上的appStart就可以启动jetty或tomcat(看servletContainer配的是什么),想停止servlet容器,点Run面板上的STOP按钮.当然,如果使用的servletContainer是tomcat,可以用tomcatStart;jetty就可以用jettyStart.我个人更喜欢在cmd命令来使用.
启动:d:\gradle\bin\gradle -i appStart
停止:d:\gradle\bin\gradle appStop




另:
当servlet容器是tomcat8,当使用appStop来停止会报警告
2014-07-31 16:14:03.863 WARN  - The web application [/test] appears to have started a thread named [
SeedGenerator Thread] but has failed to stop it. This is very likely to create a memory leak. Stack
trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.Object.wait(Object.java:503)
 sun.security.provider.SeedGenerator$ThreadedSeedGenerator.run(SeedGenerator.java:296)
 java.lang.Thread.run(Thread.java:722)
应该是可以忽略,最后可以打war包,再部署来观察,或许问题会消失.


源码:http://download.csdn.net/detail/xiejx618/7701173

你可能感兴趣的:(gradle,gretty)