├── .gitignore ├── LICENSE.TXT ├── SSH ├── build.gradle ├── libs │ └── JavaHelloWorld.jar └── src │ └── main │ ├── java │ └── com │ │ └── atlassc │ │ └── gradleWebApp │ │ └── action │ │ ├── IndexAction.java │ │ └── UploadAction.java │ ├── resources │ ├── spring-config.xml │ └── struts.xml │ └── webapp │ ├── WEB-INF │ └── web.xml │ ├── home.jsp │ └── index.jsp ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── readme.md └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .gitignore support plugin (hsz.mobi) 2 | ### Android template 3 | # Built application files 4 | *.apk 5 | *.ap_ 6 | 7 | # Files for the Dalvik VM 8 | *.dex 9 | 10 | # Java class files 11 | *.class 12 | 13 | # Generated files 14 | bin/ 15 | gen/ 16 | 17 | # Gradle files 18 | .gradle/ 19 | build/ 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Log Files 28 | *.log 29 | 30 | # .idea file 31 | .idea/ 32 | *.iml 33 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | GradleWebAppExample Copyright [2014] ShinChven@gmail.com 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /SSH/build.gradle: -------------------------------------------------------------------------------- 1 | /** 2 | * 说明: 3 | * 1、使用Intellij IDEA 开发时,如果需要使用自带的Tomcat 运行方式, 4 | * 而非Gradle RunWar方式启动,需要手动在Project Structure 中的Modules 选中Web支持 5 | * 配置Web.xml 和WebRoot路径到开发工具。 6 | * 2、关于工程结构: 7 | * 一般你的工程结构需要是: 8 | * src/main/java -- java 代码 9 | * src/main/resources -- 配置文件 10 | * src/main/webapp -- WebRoot 11 | * 关于工程结构可以在使用了 apply plugin: 'war' 插件之后 12 | * 于war{}配置,详情请查看gradle 的war 插件页面 13 | * http://www.gradle.org/ 14 | */ 15 | 16 | /** 17 | * 加入所需要的插件 18 | */ 19 | apply plugin: 'war' 20 | //apply plugin: 'tomcat' 21 | //apply plugin: 'java' 22 | apply plugin: 'idea' 23 | 24 | 25 | 26 | buildscript { 27 | repositories { 28 | jcenter() 29 | maven { 30 | url "http://download.java.net/maven/2" 31 | } 32 | maven { url 'http://repo.spring.io/plugins-release' } 33 | } 34 | 35 | dependencies { 36 | classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8' 37 | classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.1' 38 | } 39 | } 40 | 41 | configurations { 42 | moreLibs 43 | } 44 | 45 | repositories { 46 | flatDir { dirs "lib" } 47 | jcenter() 48 | } 49 | 50 | /** 51 | * 使用War 插件配置 52 | */ 53 | war { 54 | from '/src/main/webapp' // adds a file-set to the root of the archive 55 | webInf { from '/src/main/webapp/WEB-INF/' } // adds a file-set to the WEB-INF dir. 56 | classpath fileTree('lib') // adds a file-set to the WEB-INF/lib dir. 57 | classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir. 58 | webXml = file('/src/main/webapp/WEB-INF/web.xml') // copies a file to WEB-INF/web.xml 59 | } 60 | 61 | /** 62 | * 配置仓库 63 | */ 64 | repositories { 65 | jcenter() 66 | maven { 67 | url "http://download.java.net/maven/2" 68 | } 69 | maven { url 'http://repo.spring.io/plugins-release' } 70 | //maven { url 'http://maven.oschina.net/content/groups/public/' } 71 | } 72 | 73 | /** 74 | * jar 依赖 75 | */ 76 | dependencies { 77 | // 将libs 文件夹中的jar 全部打包进工程 78 | compile fileTree(dir: 'libs', include: ['*.jar']) 79 | 80 | // compile tomcat 81 | // def tomcatVersion = '7.0.42' 82 | // tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", 83 | // "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}" 84 | // tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") { 85 | // exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj' 86 | // } 87 | 88 | // compile junit 89 | //testCompile group: 'junit', name: 'junit', version: '4.11' 90 | compile 'junit:junit:4.11' 91 | 92 | // compile java ee 7 93 | compile 'javax:javaee-api:7.0' 94 | 95 | // compile springframework 96 | compile 'org.springframework:spring-aop:4.0.6.RELEASE' 97 | compile 'org.springframework:spring-beans:4.0.6.RELEASE' 98 | compile 'org.springframework:spring-aspects:4.0.6.RELEASE' 99 | compile 'org.springframework:spring-context:4.0.6.RELEASE' 100 | compile 'org.springframework:spring-web:4.0.6.RELEASE' 101 | compile 'org.springframework:spring-orm:4.0.6.RELEASE' 102 | compile 'org.springframework:spring-tx:4.0.6.RELEASE' 103 | 104 | // compile struts2 105 | compile('org.apache.struts:struts2-core:2.3.16.3') { 106 | configurations.all { // exclude javassist transitive dependency 107 | exclude group: 'javassist', module: 'javassist' 108 | } 109 | } 110 | compile 'org.apache.struts:struts2-spring-plugin:2.3.16.3' 111 | compile 'org.apache.struts:struts2-convention-plugin:2.3.16.3' 112 | 113 | // compile hibernate 114 | compile 'org.hibernate:hibernate-core:4.3.6.Final' 115 | 116 | // data source 117 | compile 'commons-dbcp:commons-dbcp:1.4' 118 | 119 | // compile mysql connector 120 | compile 'mysql:mysql-connector-java:5.1.+' 121 | 122 | // compile jstl 123 | compile 'jstl:jstl:1.2' 124 | compile 'taglibs:standard:1.1.2' 125 | 126 | compile 'org.json:json:20140107' 127 | 128 | } 129 | /** 130 | * 使用idea 插件配置下载Javadoc 和 源码 131 | */ 132 | //idea { 133 | // module { 134 | // downloadJavadoc = true 135 | // downloadSources = true 136 | // } 137 | //} 138 | -------------------------------------------------------------------------------- /SSH/libs/JavaHelloWorld.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/GradleWebApp/a5ade4324738b5e4eb5101e90252203ca5a24f8c/SSH/libs/JavaHelloWorld.jar -------------------------------------------------------------------------------- /SSH/src/main/java/com/atlassc/gradleWebApp/action/IndexAction.java: -------------------------------------------------------------------------------- 1 | package com.atlassc.gradleWebApp.action; 2 | 3 | import com.opensymphony.xwork2.ActionSupport; 4 | import org.apache.struts2.convention.annotation.*; 5 | 6 | @Action("index") 7 | @ParentPackage("basePackage") 8 | @Namespace("/") 9 | @Results({@Result(name = "success", location = "home.jsp")}) 10 | public class IndexAction extends ActionSupport { 11 | 12 | private String name="Gradle"; 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | @Override 23 | public String execute() throws Exception { 24 | System.out.println(name); 25 | return SUCCESS; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SSH/src/main/java/com/atlassc/gradleWebApp/action/UploadAction.java: -------------------------------------------------------------------------------- 1 | package com.atlassc.gradleWebApp.action; 2 | 3 | import com.opensymphony.xwork2.ActionContext; 4 | import com.opensymphony.xwork2.ActionSupport; 5 | import org.apache.struts2.ServletActionContext; 6 | import org.apache.struts2.convention.annotation.*; 7 | import org.json.JSONObject; 8 | 9 | import javax.servlet.ServletInputStream; 10 | import javax.servlet.http.HttpServletRequest; 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.File; 13 | import java.io.FileOutputStream; 14 | import java.io.PrintWriter; 15 | 16 | /** 17 | * 文件上传的Action,此Action 中将接收一个byte[] 数据包,其中包括json 对象和图片文件。

18 | * bytes[0-3] 为json 对象的byte 数组长度jsonBytesLength

19 | * json对象的数据位于bytes[4] - bytes[jsonBytesLength]

20 | * 图片数据位于json对象之后的所有数据bytes[1+bytes[0]] - bytes[bytes.length]

21 | * Created by ShinChven on 2014/10/22. 22 | */ 23 | @Action(value = "file") 24 | @ParentPackage("basePackage") 25 | @Namespace("/") 26 | @InterceptorRefs({ 27 | @InterceptorRef(value = "fileUpload", params = {"allowedTypes", "binary/octet-stream"}), 28 | @InterceptorRef("basicStack") 29 | }) 30 | public class UploadAction extends ActionSupport { 31 | @Override 32 | public String execute() throws Exception { 33 | 34 | ActionContext context = ActionContext.getContext(); 35 | HttpServletRequest request = (HttpServletRequest) context 36 | .get(ServletActionContext.HTTP_REQUEST); 37 | ServletInputStream in = request.getInputStream(); 38 | 39 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 40 | // 读取流 41 | byte[] b = new byte[1024 * 50]; //限制流的读取大小 42 | int len = 0; 43 | while ((len = in.read(b)) > 0) { 44 | System.out.println("writing: " + len); 45 | baos.write(b, 0, len); 46 | } 47 | 48 | // 拆解数据包,读取JSON 49 | byte[] bucket = baos.toByteArray(); 50 | 51 | byte[] jsonLengthBytes = new byte[4]; 52 | System.arraycopy(bucket, 0, jsonLengthBytes, 0, 4); 53 | String jsonLengthStr = new String(jsonLengthBytes); 54 | int jsonLength = Integer.parseInt(jsonLengthStr); 55 | System.out.println("jsonLength:" + jsonLength); 56 | 57 | byte[] jsonBytes = new byte[jsonLength]; 58 | System.arraycopy(bucket, 4, jsonBytes, 0, jsonLength); 59 | String jsonStr = new String(jsonBytes, "UTF-8"); 60 | 61 | JSONObject json = new JSONObject(jsonStr); 62 | String type = json.getString("type"); 63 | System.out.println("json:"+json.toString()); 64 | 65 | // 保存路径 66 | // String realPath = ServletActionContext.getServletContext().getRealPath( 67 | // "/"); 68 | 69 | String realPath = "D:\\"+String.valueOf(System.currentTimeMillis()); 70 | realPath += "." + type; 71 | File file = new File(realPath); 72 | 73 | int imageBytesLength = bucket.length - jsonLengthBytes.length - jsonBytes.length; 74 | byte[] imgBytes = new byte[imageBytesLength]; 75 | int srcPos = jsonLengthBytes.length + jsonBytes.length; 76 | System.out.println("起点:" + srcPos); 77 | System.out.println("image 大小:" + imageBytesLength); 78 | System.out.println("数据包大小:" + bucket.length); 79 | System.arraycopy(bucket, srcPos, imgBytes, 0, imageBytesLength); 80 | 81 | 82 | // 保存图片文件 83 | FileOutputStream fos = new FileOutputStream(file); 84 | fos.write(imgBytes); 85 | fos.flush(); 86 | fos.close(); 87 | in.close(); 88 | 89 | PrintWriter writer = ServletActionContext.getResponse().getWriter(); 90 | writer.print(SUCCESS); 91 | writer.flush(); 92 | System.out.println("result:" + file.exists()); 93 | return null; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /SSH/src/main/resources/spring-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | org.hibernate.dialect.MySQL5Dialect 33 | true 34 | true 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /SSH/src/main/resources/struts.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /SSH/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | openSessionInViewFilter 10 | org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 11 | 12 | 13 | openSessionInViewFilter 14 | /* 15 | 16 | 17 | contextConfigLocation 18 | classpath*:spring-config.xml 19 | 20 | 21 | 22 | org.springframework.web.context.ContextLoaderListener 23 | 24 | 25 | 26 | struts2 27 | org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 28 | 29 | 30 | struts2 31 | /* 32 | REQUEST 33 | FORWARD 34 | 35 | 36 | -------------------------------------------------------------------------------- /SSH/src/main/webapp/home.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | 4 | 5 | 6 | 7 | 8 | hello, there! 9 | 10 | 11 | -------------------------------------------------------------------------------- /SSH/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <% 2 | request.getRequestDispatcher("index").forward(request,response); 3 | %> -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | // NOTE: Do not place your application dependencies here; they belong 9 | // in the individual module build.gradle files 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | jcenter() 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/GradleWebApp/a5ade4324738b5e4eb5101e90252203ca5a24f8c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Dec 23 13:05:35 CST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip 7 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 使用Gradle 构建JavaWebApp 2 | 3 | Gradle是很强大的构建工具,不过如果你在天朝,你可能需要一个强大的网络环境。 4 | 本项目不具备太大实战意义,只是探索性地使用Gradle 构建Java Web项目,本人自己也只是使用它当学生和测试的模板。 5 | 本项目还在不间断施工中,请多多包涵。 6 | 如果在「食用」过程中发现Gradle build起来真TM恶心,建议赶紧回头使用Maven,Maven 在天朝使用确实更加顺畅。 7 | 8 | ## Gradle的基本结构 9 | 10 | 一般使用Gradle 来构建一个项目,首先会有一个project文件夹,然后在project文件夹中会有一个或者多个module。project表示一个工作空间,而其中的module则是具体的应用,也可以是应用所依赖的library。 11 | 12 | ### settings.gradle 13 | 14 | 在此文件中使用include ':folder' 语句配置某个文件夹为module文件夹,只有在这里配置了,开发工具才会增目标文件夹中运行build.gradle文件构建module。 15 | 16 | ### build.gradle 17 | 18 | Gradle项目中使用build.gradle 文件对项目进行构建: 19 | 20 | 1、根文件夹中的build.gradle 文件一般用于配置一些整个工程下的通用配置,比如repository之类的。 21 | 22 | 2、module 中的build.gradle 配置module 的构建。 23 | 24 | ## SSH module 25 | 26 | 这是一个Gradle 构建的Struts2 Hiberante4 Springframeworks4 应用,详细配置请查看其中的build.gradle 文件。该module基本已经填完坑,可以拉下来直接运行。 27 | 28 | Good luck! 29 | 30 | ## SpringWebMVC 31 | 32 | 请参考这个仓库:https://github.com/ShinChven/SpringApp 33 | 34 | ## 构建War 项目 35 | 36 | 当你在module 的build.gradle中添加了war 插件(apply plugin: 'war')时,IDE会识别该module 为Java Web项目, 37 | 你可以使用一些配置来定义War包的结构,比如: 38 | 39 | ```groovy 40 | configurations { 41 | moreLibs // 配置该module 在构建时,既加载dependencies中的依赖,还加载「散装」的jar包 42 | } 43 | 44 | war { 45 | from '/src/main/webapp' // 指定WebRoot 路径 46 | webInf { from '/src/main/webapp/WEB-INF/' } // 指定 WEB-INF 文件夹路径. 47 | classpath fileTree('lib') // 指定 WEB-INF/lib 文件夹路径. 48 | classpath configurations.moreLibs // build 「散装」的jar包 49 | webXml = file('/src/main/webapp/WEB-INF/web.xml') // WEB-INF/web.xml 文件路径 50 | } 51 | ``` 52 | 53 | 在配置好war 以后,Gradle build 时会自动生成这样的项目结构: 54 | 55 |   一般你的工程结构需要是: 56 | 57 |    src/main/java -- java 代码 58 | 59 |    src/main/resources -- 配置文件 60 | 61 |    src/main/webapp -- WebRoot 62 | 63 |    build.gradle -- gradle 构建脚本 64 | 65 |    settings.gradle -- gradle 项目设置,在其中你可以配置目录下哪些文件夹是一个module 66 | 67 |    关于工程结构的配置请参照DEMO中build.gradle的war{}部分, 68 | 69 |    详情请查看gradle 的war 插件页面: http://www.gradle.org/docs/current/userguide/war_plugin.html 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'GradleWebApp' 2 | include ':SSH' 3 | --------------------------------------------------------------------------------