1. 新建文件夹比如zypt,进入文件夹执行gradle init
2.生成的build.gradle中添加
subprojects { apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' version = '1.0'
// JVM 版本号要求 sourceCompatibility = 1.7 targetCompatibility = 1.7
// java编译的时候缺省状态下会因为中文字符而失败 [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' ext {
//springVersion = '4.2.1.RELEASE' springVersion = '2.5.6' hibernateVersion='3.6.10.Final' struts2Version = '2.3.20' mysqlVersion = '5.1.36' } repositories { mavenCentral() } jar { manifest { attributes("Implementation-Title": "Gradle") } } configurations { // 所有需要忽略的包定义在此 //all*.exclude group: 'commons-httpclient' //all*.exclude group: 'commons-logging' //all*.exclude group: 'commons-beanutils', module: 'commons-beanutils' } dependencies { // 通用依赖 compile( //logback 与 activeMQ-bin冲突 //"ch.qos.logback:logback-classic:1.1.3", //"ch.qos.logback:logback-core:1.1.3", 'org.slf4j:slf4j-log4j12:1.7.12', "org.slf4j:slf4j-api:1.7.12", 'log4j:log4j:1.2.17',
//json "com.google.code.gson:gson:2.3.1", //"org.json:json:20141113", 'dom4j:dom4j:1.6.1', "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1", 'javax.transaction:jta:1.1',
//db "mysql:mysql-connector-java:$mysqlVersion", "com.alibaba:druid:1.0.15", "com.mchange:c3p0:0.9.5.1",
//apache commons 'commons-codec:commons-codec:1.10', //'commons-configuration:commons-configuration:1.1', 'xml-security:xmlsec:1.3.0', 'commons-lang:commons-lang:2.6', 'commons-collections:commons-collections:3.2.1',
'net.sf.ehcache:ehcache:2.10.0', 'cglib:cglib-nodep:3.1', 'org.apache.neethi:neethi:3.0.3',
//http client 'org.apache.httpcomponents:httpclient:4.3.3'
) // 依赖mave中不存在的jar ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar') ext.rootProjectLibs = new File(rootProject.rootDir, 'libs').getAbsolutePath() ext.jarTree += fileTree(dir: rootProjectLibs, include: '**/*.jar') compile jarTree
// 测试依赖 testCompile(
//"org.springframework:spring-test:$springVersion", "junit:junit:4.11"
) } // 显示当前项目下所有用于 compile 的 jar. task listJars(description: 'Display all compile jars.') << { configurations.compile.each { File file -> println file.name } }
tasks.withType(JavaCompile) { }}
3.生成的setting.gradle中添加
include 'zypt-core','zypt-web'rootProject.name = 'zypt'
4.创建子模块
创建目录 F:\workspace\zypt\zypt-core\src\main\java
F:\workspace\zypt\zypt-core\src\test\java
F:\workspace\zypt\zypt-web\src\main\java
F:\workspace\zypt\zypt-web\src\test\java
5.完善配置文件
在F:\workspace\zypt\zypt-core下新建build.gradle内容为:
archivesBaseName = 'zypt-core'
在F:\workspace\zypt\zypt-web下新建build.gradle内容为:
apply plugin:"war"
apply plugin: 'eclipse-wtp'dependencies{
// 依赖 core 模块 //compile project(":zypt-core") compile( "org.springframework:spring-context:$springVersion", "org.springframework:spring-context-support:$springVersion", "org.springframework:spring-core:$springVersion", "org.springframework:spring-jdbc:$springVersion", "org.springframework:spring-jms:$springVersion", "org.springframework:spring-orm:$springVersion", "org.springframework:spring-test:$springVersion", "org.springframework:spring-tx:$springVersion", "org.springframework:spring-web:$springVersion", "org.springframework:spring-beans:$springVersion", "org.springframework:spring-aop:$springVersion",
"org.apache.struts:struts2-core:$struts2Version", "org.apache.struts:struts2-codebehind-plugin:$struts2Version", "org.apache.struts:struts2-config-browser-plugin:$struts2Version", "org.apache.struts:struts2-convention-plugin:$struts2Version", "org.apache.struts:struts2-dojo-plugin:$struts2Version", "org.apache.struts:struts2-json-plugin:$struts2Version", //"org.apache.struts:struts2-spring-plugin:$struts2Version", "org.apache.struts.xwork:xwork-core:2.3.20",
//hibernate //"org.hibernate:hibernate-core:$hibernateVersion", //'org.hibernate:ejb3-persistence:1.0.2.GA', //'org.hibernate:hibernate-commons-annotations:3.2.0.Final', //'org.hibernate:hibernate-annotations:3.5.6-Final', //'org.hibernate:hibernate-entitymanager:3.6.10.Final', //'org.hibernate:hibernate-validator:3.1.0.GA',
//apache commons
//jms //'javax.jms:jms:1.1', //'org.apache.cxf:cxf-rt-core:2.7.8', //'org.apache.cxf:cxf-bundle:2.7.8', //'org.apache.cxf:cxf-rt-frontend-jaxws:2.7.8', //'org.apache.cxf:cxf-rt-frontend-jaxrs:2.7.8', 'org.apache.ws.xmlschema:xmlschema-core:2.2.1', 'wsdl4j:wsdl4j:1.6.3', 'antlr:antlr:2.7.7',
//mail 'org.apache.commons:commons-email:1.4',
//poi 'org.apache.poi:poi:3.9',
//html "org.jsoup:jsoup:1.8.3",
//ativeMQ 'org.apache.activemq:activemq-all:5.9.0',
//quartz 'org.quartz-scheduler:quartz:1.8.6'
)
//系统提供的依赖 providedCompile(
//"javax.servlet:javax.servlet-api:2.5", "javax.servlet.jsp:jsp-api:2.2.1-b03",
//"javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1", //'javax.servlet.jsp.jstl:jstl:1.2', "javax.servlet:servlet-api:2.5", //'javax.servlet:jstl:1.1.2', 'taglibs:standard:1.1.2' )
//runtime 'javax.servlet:jstl:1.1.2'
//def tomcatVersion = '7.0.59' // tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", // "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", // "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"}
task jarWithoutResources(type: Jar) {
baseName project.name
from("$buildDir/classes/main") }
war{
dependsOn jarWithoutResources // from("$projectDir/src/main/resources") { // include "*.*" // into("WEB-INF/classes") // } classpath=classpath - sourceSets.main.output
classpath fileTree(dir:libsDir, include:"${project.name}-${version}.jar") }
task('jarPath')<<{
configurations.runtime.resolve().each {
print it.toString()+";"
}
println(); } //eclipse 配置eclipse { wtp { //contextPath = "WebContent" //deployName = "zypt-web" facet{ facet name: 'jst.web', version: '2.5' facet name: 'java', version: '1.7' } component { resource sourcePath: 'src/main/java', deployPath: '/WEB-INF/classes' resource sourcePath: 'WebContent', deployPath: '/' resource sourcePath: '/remote', deployPath: '/WEB-INF/classes' } }}eclipse.wtp.facet.file.withXml { provider -> provider.asNode().fixed.find { it.@facet == 'jst.java' }.@facet = 'jst2.java'}//为了使'src/yewu/java' 不在Refresh All操作之后被Eclipse删除sourceSets { main { java { srcDir 'src/main/java' } resources { srcDir 'src/resources' } }
yewu{ java { srcDir 'src/yewu/java' }
}}
6.在Eclipse中完善Gradle配置
7. 在Eclipse中导入,选择F:\workspace\zypt\,点击按钮“Build Model”,系统将自动把主模块、子模块加入IDE中
8.为了便于在Eclipse使用Tomcat调试Web程序,所以在zypt-web子模块加入了apply plugin:”war” apply plugin: ‘eclipse-wtp’,在src/main/文件夹下新建webapp文件夹,把WEB-INF和jsp放到此文件下,相对于 WebContent。
9.按照Dynamic Web Project 项目发布,调试web程序即可。
10.有些jar是自有,或者依赖冲突,可以在zypt下新建libs,把jar放到此文件夹下。