参考文章:
http://blog.jobbole.com/71999/
http://blog.csdn.net/u011422624/article/details/72886820
参考代码:
git@github.com:xiangqian19831224/gradle-learn.git
一、Gradle安装
步骤一: 下载gradle
https://gradle.org/releases/
步骤二:加压到目录 C:\
步骤三:配置环境变量
电脑右键->属性->高级系统设置->环境变量
系统变量:GRADLE_HOME C:\Gradle\gradle-3.5
PATH *;%GRADLE_HOME%\bin //Path后面补充
二、Gradle简单项目
参考: Example-1
idea构建:
步骤一:File->New-Project->Gradle
书写代码:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } build.gradle:group 'com.gradle.learn' version '1.0-SNAPSHOT' apply plugin: 'java' jar { manifest { attributes 'Main-Class':'HelloWorld' } } sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' }
编译:注:# gradle assemble# gradle assemble 编译源代码,生成jar文件,不执行单元测试 # gradle build 执行项目构建 # gradle clean 删除构建项目 # gradle complile java任务会编译程序中的源代码
运行:
#java -jar build/libs/example-1-1.0-SNAPSHOT.jar
三、依赖管理
参考: example-2
1、build.gradle文件
#外部依赖信息
group 'com.gradle.learn'
version '1.0-SNAPSHOT'
//
apply plugin: 'java'
//生成胖jar包
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'HelloWorld'
}
}
sourceCompatibility = 1.8
//依赖包来源
repositories {
mavenCentral()
}
//依赖包
dependencies {
compile 'log4j:log4j:1.2.17'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
2、编译
3、执行
#java -jar build/libs/example-2-1.0-SNAPSHOT.jar
四、创建发布版本
参考: example-3
创建二进制发布文件: 1、配置applicaition插件: Application插件在项目中添加了5个任务: run任务用以启动应用程序。 startScripts任务会在build/scripts目录中创建启动脚本,这个任务所创建的启动脚本适用于Windows和*nix操作系统。 installApp任务会在build/install/[project name]目录中安装应用程序。 distZip任务用以创建二进制发布并将其打包为一个zip文件。可以在build/distributions目录下找到。 distTar任务用以创建二进制发布并将其打包为一个tar文件。可以在build/distributions目录下找到。 2、配置main函数: mainClassName = 'HelloWorld' 3、配置文件 apply plugin: 'application' apply plugin: 'java' repositories { mavenCentral() } dependencies { compile 'log4j:log4j:1.2.17' testCompile 'junit:junit:4.11' } mainClassName = 'HelloWorld' 4、添加应用程序许可证 将许可证文件加入到二进制发布文件中 apply plugin: 'application' apply plugin: 'java' repositories { mavenCentral() } dependencies { compile 'log4j:log4j:1.2.17' testCompile 'junit:junit:4.11' } mainClassName = 'net.petrikainulainen.gradle.HelloWorld' task copyLicense { outputs.file new File("$buildDir/LICENSE") doLast { copy { from "LICENSE" into "$buildDir" } } } applicationDistribution.from(copyLicense) { into "" } 5、生成二进制包 1> 生成windows平台包 # gradle distZip 2> unix平台 # gradle distTar
五、springboot服务
参考: example-5
步骤一:创建工程
idea->file->new project->Spring Initializr
下一步:
下一步:
选择web-web
下一步:填写工程名和位置
Finshed
步骤二:配置
build.gradle依赖添加
buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile(group: 'org.projectlombok', name: 'lombok', version: '1.16.18') compile(group: 'mysql', name: 'mysql-connector-java', version: '6.0.6') compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0') testCompile('org.springframework.boot:spring-boot-starter-test') }
三、代码书写<略>
四、编译运行<略>