环境
- os: win8.1 64
- IDE: idea 14.04
- gralde : 2.7
参考
-
实战Gradle
创建spring mvc 项目
创建工程
Create directories for empty content roots automatically
选项是自动建立文件夹的意思
Use local gradle distribution
是选择本地的gradle
填写项目名
修改build.gradle
apply plugin: 'java'apply plugin: 'war'apply plugin: 'jetty'apply plugin: 'idea'// JDK 7sourceCompatibility = 1.7targetCompatibility = 1.7version = '1.0'compileJava.options.encoding = 'UTF-8'compileTestJava.options.encoding = 'UTF-8'allprojects { repositories { mavenLocal() maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} }}// testString versionJunit = '4.12'String versionHamcrest = '1.3'// java eeString versionServletApi = '3.0.1'String versionJstl = '1.2'// springString versionSpring = '4.2.0.RELEASE'// mysqlString versionMysql = '5.1.32'// alibabaString versionDruid = '1.0.9'// mybatisString versionMybatis = '3.2.8'String versionMybatisSpring = '1.2.2'String versionPagehelper = '4.0.3'String versionMapper = '3.2.2'// jacksonString versionJackson = '2.4.2'// googleString versionGson = '2.2.2'// logString versionLogback = '1.1.3'String versionLogbackExt = '0.1.2'dependencies { // test testCompile group: 'junit', name: 'junit', version: versionJunit testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: versionHamcrest // java ee providedCompile group : 'javax.servlet', name: 'javax.servlet-api', version : versionServletApi providedCompile group: 'javax.servlet', name: 'jstl', version: versionJstl // spring compile group: 'org.springframework', name: 'spring-webmvc', version: versionSpring compile group: 'org.springframework', name: 'spring-aspects', version: versionSpring compile group: 'org.springframework', name: 'spring-jdbc', version: versionSpring // mysql compile group: 'mysql', name: 'mysql-connector-java', version: versionMysql // alibaba compile group: 'com.alibaba', name: 'druid', version: versionDruid // mybatis compile group: 'org.mybatis', name: 'mybatis', version: versionMybatis compile group: 'org.mybatis', name: 'mybatis-spring', version: versionMybatisSpring compile group: 'com.github.pagehelper', name: 'pagehelper', version: versionPagehelper compile group: 'tk.mybatis', name: 'mapper', version: versionMapper // jackson compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: versionJackson // google compile group: 'com.google.code.gson', name: 'gson', version: versionGson // logback compile group: 'ch.qos.logback', name: 'logback-classic', version: versionLogback compile group: 'org.logback-extensions', name: 'logback-ext-spring', version: versionLogbackExt}jettyRun { httpPort = 8082 contextPath = '/'}
PS:由于我已经建立了项目,所以这里的项目名和图中的不一样
需要注意的是如果你使用默认的镜像地址,下载速度会比较慢,推荐使用淘宝的maven
地址
网上有很多使用tomcat的,而我使用的是jetty
,目前感觉没什么问题
顶部的idea
插件加了好像没感觉到什么变化,但是java
war
jerty
这三个插件是必须要加上的
刷新依赖与maven
没什么不同
添加配置文件与controller代码等省略,项目结构如下图
DEBUG
gradle
和maven
的DEBUG在IDEA中不太一样,你需要有两个配置才行
配置jettyRun
其中VM options
中的字符串是
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
配置Remote
这里使用默认配置即可
但是需要注意的是Port中的参数要和你上面配置的jettyRun的VM options中的端口号一致
DEBUG启动
首先你要以debug方式启动Remote Debug,此时控制台输出如下:
然后再以debug方式启动jettyRun
启动会比较慢,但是运行比较流畅
在代码中添加的断点此时已经起作用了,但是你需要切换到Remote Debug选项卡才能看到
整合git进行多模块开发
建立git仓库
如下是我的git仓库
导入idea
使用idea导入git项目的时候一定要注意要选择 import project from external model
,再选择Gradle
,否则你是绝对不可能调出gradle
的窗口的
然后再按照下图的默认配置就可以了
添加根目录下的build脚本
现在的项目应该什么都没有,你需要手工建立build.gradle
和settings.gradle
文件
现在的项目应该是这样的
根据提示的例子,修改根目录下的build.gradle
如下:
allprojects{ group = 'com.laolang.jd' version = '1.0'}subprojects { apply plugin: 'java' apply plugin: 'idea' // JDK 7 sourceCompatibility = 1.7 targetCompatibility = 1.7 //version = '1.0' compileJava.options.encoding = 'UTF-8' compileTestJava.options.encoding = 'UTF-8' allprojects { repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } } ext { // test versionJunit = '4.12' versionHamcrest = '1.3' // java ee versionServletApi = '3.0.1' versionJstl = '1.2' // spring versionSpring = '4.2.0.RELEASE' // mysql versionMysql = '5.1.32' // alibaba versionDruid = '1.0.9' // mybatis versionMybatis = '3.2.8' versionMybatisSpring = '1.2.2' versionPagehelper = '4.0.3' versionMapper = '3.2.2' // jackson versionJackson = '2.4.2' // google versionGson = '2.2.2' // log versionLogback = '1.1.3' versionLogbackExt = '0.1.2' }}
新建第一个module
添加依赖
此时你可以在jd-commons\build.gradle
中添加依赖了,我添加的依赖如下:
archivesBaseName = 'jd-commons'dependencies { // test testCompile group: 'junit', name: 'junit', version: versionJunit testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: versionHamcrest // google compile group: 'com.google.code.gson', name: 'gson', version: versionGson}
你需要刷新依赖,刷新方法
之所以贴出这个图,是因为如果你在导入项目的时候没有选择 Gradle Model 的话,你是绝对不可能调出这个Gradle的窗口的
添加代码
当然你还需要手工建立src\main\java src\main\resources等文件夹,我这里就简单建立一下类以做演示
我新建的文件夹
可能你新建文件夹之后和我的不一样,也就是说你无法在java这个源代码文件夹上创建包,此时你需要打开project structure
,打开方法有多种,这里只说最快的一种:Ctrl+Shift+Alt+S
代码如下:
package com.laolang.jd.commons.utils;import com.google.gson.Gson;public class HttpClientUtils { private static final Gson gson = new Gson(); public static String getName(){ return "commons"; } public static String getJson(Object o ){ return gson.toJson(o); }}
package com.laolang.jd.commons.utils;import static junit.framework.Assert.*;import org.junit.Test;public class HttpClientUtilsTest { @Test public void test01(){ assertEquals(1,1); }}
archivesBaseName = 'jd-commons'dependencies { // test testCompile group: 'junit', name: 'junit', version: versionJunit testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: versionHamcrest // google compile group: 'com.google.code.gson', name: 'gson', version: versionGson}
为了项目演示的完整性,我还写了一个测试类,此时的IDEA中的工程结构如下
然后再添加一个Module,过程不再贴图,只贴一下build.gradle
apply plugin: 'war'apply plugin: 'jetty'dependencies { compile project(":jd-commons") // java ee providedCompile group : 'javax.servlet', name: 'javax.servlet-api', version : versionServletApi providedCompile group: 'javax.servlet', name: 'jstl', version: versionJstl // spring compile group: 'org.springframework', name: 'spring-webmvc', version: versionSpring compile group: 'org.springframework', name: 'spring-aspects', version: versionSpring compile group: 'org.springframework', name: 'spring-jdbc', version: versionSpring // logback compile group: 'ch.qos.logback', name: 'logback-classic', version: versionLogback compile group: 'org.logback-extensions', name: 'logback-ext-spring', version: versionLogbackExt}jettyRun { httpPort = 8082 contextPath = '/'}
最后的工程结构如下
settings.gradle
rootProject.name = 'gradle-jd'include 'jd-commons'include 'jd-manage'
运行项目
此时就可以运行项目了,和之前的运行方法没有什么区别
在浏览器中输入地址就可以看到结果了
添加.gitingore
.gradle.idea*.imlbuild
然后推送到远端
从远端导入项目
此时我的项目显示如下
IDEA导入GIT项目
现在又和之前的一样了
说明
关于gradle的一些配置和使用,请参考《实战gradle》或者其它博客,这里只做简单说明
settings.gradle中的两个include可以合并在一行
include 'jd-commons' , 'jd-manage'
根目录中的build.gradle
allprojects{ // 项目的 groupId group = 'com.laolang.jd' // 项目版本 version = '1.0'}
使用gradle构建时,项目的artifactId和项目名同名
子目录中的build.gradle
// 缺省的归档文件的名字archivesBaseName = 'jd-commons'
本博客使用项目的gitosc地址:
更复杂的多模块构建正在摸索中