gradle实践

1、安装gradle

2、初始化项目

gradle init (在当前项目下用命令行执行,需要确保gradle加入到环境变量)

3、设置依赖库位置,可以设置1个或多个

repositories {

// You can declare any Maven/Ivy/file repository here.
mavenCentral()  //mvn仓库
maven {url “https://repo.spring.io/libs-release”}
jcenter()  //
flatDir(dirs: “$projectDir/WebContent/WEB-INF/lib”)  //本地包
flatDir(dirs:”D:\\java\\tomcat8\\lib”)

}

4、设置项目依赖包

dependencies {
def springVersion = ‘4.1.1.RELEASE’
compile(
‘org.apache.commons:commons-lang3:3.1’,
‘com-common:cpus-common’,

/* spring */
“org.springframework:spring-context:${springVersion}”,
“org.springframework:spring-beans:${springVersion}”,
“org.springframework:spring-web:${springVersion}”,
“org.springframework:spring-webmvc:${springVersion}”,
“org.springframework:spring-tx:${springVersion}”,
“org.springframework:spring-context-support:${springVersion}”,
)
}

5、支持的插件

apply plugin: ‘java’
apply plugin: ‘war’
apply plugin: ‘jetty’
apply plugin: ‘idea’
apply plugin:’eclipse’
apply plugin: ‘maven’
apply plugin: ‘eclipse-wtp’
————————————–
apply plugin: ‘application’
apply plugin: ‘spring-boot’

6、查看可执行的任务

gradle tasks

7、解决编码错误的问题

[compileJava, compileTestJava]*.options*.encoding = ‘UTF-8’

8、不符合gradle规范(非webapp)的Web项目需要指定webAppDirName

webAppDirName = ‘WebContent’

9、打包时包含所有的xml文件(如果不加如下内容则不会打包在内)

jar {
from(‘src/main/java’) {
include ‘**/*.xml’
}
}

10、对于自运行的applicaiton,需要设置执行入口(main方法所在类)

mainClassName = ‘com.canon.cpus.Application’

11、常用的任务

gradle assemble (runs only the tasks required to create the jar file)
gradle build (runs the full build)
gradle clean test
gradle clean integrationTest
gradle run
gradle distZip (creates a runnable binary distribution that is packaged into a .zip file)
gradle distTar (creates a runnable binary distribution that is packaged into a .tar file)
gradle bootRun
gradle jettyRun
gradle eclipse

12、应用spring-boot插件(apply plugin: ‘spring-boot’)需要增加:

buildscript {
repositories {
jcenter()
}
dependencies {
classpath(
‘org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE’,
‘org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2’
)
}
}

13、应用jetty插件(apply plugin: ‘org.akhikhl.gretty’)需要:

buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘org.akhikhl.gretty:gretty:+’
}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注