英文来源: http://akhikhl.github.io/gretty-doc/Getting-started.html
一、gradle插件
1、使用gretty来运行jetty: gradle appRun
如何安装gretty插件参见附录!
- apply plugin: 'war'
- apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
- targetCompatibility = 1.8
- version = "1.0"
- ext {
- springVersion = "3.2.8.RELEASE"
- }
- repositories{
- mavenCentral()
- }
- [compileJava,compileTestJava,javadoc]*.options*.encoding = "gbk"
- dependencies{
- compile "org.apache.struts:struts2-core:2.1.8.1"
- providedCompile "javax.servlet:javax.servlet-api:3.1.0"
- providedCompile "javax.servlet.jsp:jsp-api:2.2.1-b03"
- testCompile "junit:junit:4.11"
- }
- apply plugin:"war"
- apply plugin:"jetty"
- targetCompatibility = 1.8
- version = "1.0"
- ext {
- springVersion = "3.2.8.RELEASE"
- }
- repositories{
- mavenCentral()
- }
- [compileJava,compileTestJava,javadoc]*.options*.encoding = "gbk"
- dependencies{
- compile "org.apache.struts:struts2-core:2.1.8.1"
- providedCompile "javax.servlet:javax.servlet-api:3.1.0"
- providedCompile "javax.servlet.jsp:jsp-api:2.2.1-b03"
- testCompile "junit:junit:4.11"
- }
- jettyRun{
- webAppSourceDirectory file("$projectDir/src/main/webapp")
- httpPort 8080
- contextPath project.name
- scanIntervalSeconds 0
- reload "automatic"
- }
二、maven的jetty插件
1、方式一:使用maven插件
使用Maven的命令来在jetty中运行web应用: mvn jetty:run
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lyq.action</groupId>
- <artifactId>e14_3</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>e14_3</name>
- <properties>
- <struts2.version>2.3.16.3</struts2.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-config-browser-plugin</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-junit-plugin</artifactId>
- <version>${struts2.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.3</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.5</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>8.1.7.v20120910</version>
- <configuration>
- <stopKey>CTRL+C</stopKey>
- <stopPort>8999</stopPort>
- <systemProperties>
- <systemProperty>
- <name>log4j.configuration</name>
- <value>file:${basedir}/src/main/resources/log4j.properties</value>
- </systemProperty>
- <systemProperty>
- <name>slf4j</name>
- <value>false</value>
- </systemProperty>
- </systemProperties>
- <scanIntervalSeconds>10</scanIntervalSeconds>
- <webAppSourceDirectory>${basedir}/src/main/webapp/</webAppSourceDirectory>
- <webAppConfig>
- <contextPath>/e14_3</contextPath>
- <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
- </webAppConfig>
- </configuration>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- </dependencies>
- </plugin>
- </plugins>
- </build>
- </project>
2、方式二:添加 Jetty 相关依赖以及进行类似下面代码配置:
package com.coderknock.jettystudy;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class WebAppContextWithFolderServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setContextPath("/myapp");
context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml");
context.setResourceBase("E:/share/test/struts2-blank");
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
}
}
三、附录
1、安装gretty的三种方式
1.1、方式一:在app应用的 "build.gradle" 中加入:
- apply plugin: 'war'
- apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
大功告成! 现在你可以使用以下命令启动你的web-app;
- gradle appRun
你也可以使用其他 Gretty tasks (Gretty 任务) 来运行和调试你的web-app 或者根据你的需求 configure Gretty (配置Gretty).
1.2、方式二:从以下URL下载脚本并放置在项目文件夹下安装getty插件:
https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin
- apply from: 'gretty.plugin'
- buildscript {
- repositories {
- jcenter()
- // enable this to use snapshot versions of Gretty:
- // maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
- }
- dependencies {
- classpath 'org.akhikhl.gretty:gretty:+'
- }
- }
- repositories {
- jcenter()
- // enable this to use snapshot versions of Gretty:
- // maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
- }
- apply plugin: 'org.akhikhl.gretty'
2、getty插件的命令
2.1 gradle appRun
编译当前项目,不依赖于war任务,
另有appRunWar、appRunDebug、appRunWarDebug
2.2 gradle appStart
编译当前项目,使用新java线程开启服务,监听端口,等待HTTP请求
不依赖于war任务
不主动关闭服务,即一直在运行,需用gradle appStop关闭
另有appStartWar、appStartDebug、appStartWarDebug
2.3 gradle jetty* / gradle tomcat*
包含Start、Run、Stop等
3.核心特性
3.1 选择servlet 容器
gretty {
// 端口默认8080
// serlvetContainer 支持 jetty7/8/9,tomcat7/8
// contextPath 设置根路径,默认为项目名称
port = 8081
serlvetContainer = 'jetty9'
contextPath = '/'
}
3.2 热部署(Gretty 1.1.5+)
常用属性
scanInterval:监视周期,单位为秒,设置为0等于完全关闭热部署
scanDir:需要监视的文件夹
recompileOnSourceChange:监视源码变动,自动编译
reloadOnClassChange:编译的类发生改变,自动加载
reloadOnConfigChange:WEB-INF或META-INF发生改变
reloadOnLibChange:依赖发生改变
Gretty默认如下
scanInterval 设置为1,每秒扫描改动1次
scanDir默认为下 :
${projectdir}/src/main/java
${projectdir}/src/main/groovy
${projectdir}/src/main/resources
${projectdir}/build/classes/main
${projectdir}/build/resources/main
recompileOnSourceChange、reloadOnClassChange、reloadOnConfigChange 和 reloadOnLibChange默认为true
3.3 快速加载
fastReload属性,默认为true,监听webapp/中的内容,文件发生改变,无需重启。
3.4 添加新的资源目录
// 除了src/main/webapp外,可另外指定资源目录
gretty{
// ...
extraResourceBase 'dir1',
extraResourceBases 'dir2','dir3'
// ...
}
3.5 HTTPS 支持
生成自签名证书,仅在开发时使用
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连接
手动配置
gretty {
sslKeyStorePath = '/some/path/keystore'
sslKeyStorePassword = 'someKeystorePassword'
sslKeyManagerPassword = 'someKeyManagerPassword'
sslTrustStorePath = '/another/path/trust_keystore'
sslTrustStorePassword = 'someTrustStorePassword'
}
3.6 转发(Gretty 1.1.7+)
步骤1:在WEB-INF/web.xml中加入以下内容
<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>
步骤2:创建WEB-INF/filter.groovy,设置转发规则
// 根地址转发到 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)
}
3.7 调试(Debug)
// 为所有的debug命令配置参数
gretty {
debugPort = 5005 // 默认
debugSuspend = true // 默认
}
// 仅针对appRunDebug
gretty {
afterEvaluate {
appRunDebug {
debugPort = 5005
debugSuspend = true
}
}
}
4.产品构建
4.1 gradle buildProduct
生成安装文件
生成目录位于 build/output/${project.name}
结构如下
--build/output/${project.name}
|--conf/ => 配置文件
|--runner/ => servlet container 所需库
|--starter/
|--webapps/ => java web 应用
|--restart.bat/sh
|--run.bat/sh
|--start.bat/sh
|--stop.bat/sh
多应用,需在build.gradle中配置 product,例如
product {
webapp project // include this project
webapp ':ProjectA'
webapp ':ProjectB'
}
4.2 gradle archiveProduct
打包生成的安装文件
生成目录位于 build/output/${project.name}
四、一个使用gretty插件的例子:
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.加入gretty配置.在build.gradle加上2-9行是gretty插件的配置.
b.最后五行是gretty是适配tomcat或jetty的配置.其中不配置servletContainer,默认为jetty9,这个值可以是'jetty7', 'jetty8', 'jetty9', 'tomcat7', 'tomcat8'