Gradle是⼀个基于Apache Ant和Apache Maven概念的项⽬⾃动化构建开源⼯具。它使⽤⼀种基于Groovy的特定领域语⾔(DSL)来声明项⽬设置,⽬前也增加了基于Kotlin语⾔的kotlin-based DSL,抛弃了基于XML的各种繁琐配置。⾯向Java应⽤为主。当前其⽀持的语⾔限于Java、Groovy、Kotlin和Scala,计划未来将⽀持更多的语⾔。⼀、相关介绍
Gradle是⼀个好⽤的构建⼯具 ,使⽤它的原因是:
配置相关依赖代码量少,不会像maven⼀样xml过多 打包编译测试发布都有,⽽且使⽤起来⽅便 利⽤⾃定义的任务可以完成⾃⼰想要的功能⼆、安装
在cmd模式下查看,出现以下信息证明安装成功:
然后我们可以在在环境变量⾥配置gradle默认的仓库地址(和maven不太⼀样):
三、IED中的使⽤1、IDEA
使⽤idea创建⼀个web的Gradle项⽬
然后对项⽬进⾏打包运⾏:
双击war
打包完成之后的war⽂件会在:
然后把war放⼊对应的tomcat⽬录即可,这⾥就不多解释了。2、Eclipse四、问题说明
1、解释build.gradle和settings.gradle
⾸先是⼀个项⽬包含group、name、version 。settings.gradle是⽤来管理多项⽬的,⾥⾯包含了项⽬的name
在build.gradle中,apply是应⽤的插件,如:
这⾥我们⽤了java和war的插件 ,dependencies是⽤于声明这个项⽬依赖于哪些jar
这⾥说明的是,测试编译阶段我们依赖junit的jar。其中包括complile(编译时)runtime(运⾏时)testCompile(测试编译时)testRuntime(测试运⾏时)。repositories是⼀个仓库gradle会根据从上到下的顺序依次去仓库中寻找jar
这⾥我们默认的是⼀个maven的中⼼仓库 ,从gradle源代码中我们看到地址是这样的
这⾥可以进⾏配置,其中mavenLocal()表⽰使⽤本地maven仓库;mavenCentral()使⽤maven中⼼仓库 。使⽤固定的地址,这⾥可以使⽤阿⾥云(maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'})的镜像下载速度会快⼀些,然后也可以使⽤公司内部的私服地址 。
附加,这⾥加上⼀个spring boot的gradle配置⽂件,可以和maven的构建对⽐⼀下
// buildscript 代码块中脚本优先执⾏buildscript {
// ext ⽤于定义动态属性 ext {
springBootVersion = '1.5.2.RELEASE' }
// ⾃定义 Thymeleaf 和 Thymeleaf Layout Dialect 的版本 ext['thymeleaf.version'] = '3.0.3.RELEASE' ext['thymeleaf-layout-dialect.version'] = '2.2.0'
// ⾃定义 Hibernate 的版本
ext['hibernate.version'] = '5.2.8.Final'
// 使⽤了 Maven 的中央仓库(你也可以指定其他仓库) repositories { //mavenCentral() maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/' } }
// 依赖关系 dependencies {
// classpath 声明说明了在执⾏其余的脚本时,ClassLoader 可以使⽤这些依赖项
classpath(\"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}\") }}
// 使⽤插件
apply plugin: 'java'apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
// 打包的类型为 jar,并指定了⽣成的打包的⽂件名称和版本jar {
baseName = 'springboot-test' version = '1.0.0'}
// 指定编译 .java ⽂件的 JDK 版本sourceCompatibility = 1.8
// 默认使⽤了 Maven 的中央仓库。这⾥改⽤⾃定义的镜像库repositories { //mavenCentral() maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/' }}
// 依赖关系dependencies {
// 该依赖对于编译发⾏是必须的
compile('org.springframework.boot:spring-boot-starter-web')
// 添加 Thymeleaf 的依赖
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
// 添加 Spring Security 依赖
compile('org.springframework.boot:spring-boot-starter-security')
// 添加 Spring Boot 开发⼯具依赖
//compile(\"org.springframework.boot:spring-boot-devtools\")
// 添加 Spring Data JPA 的依赖
compile('org.springframework.boot:spring-boot-starter-data-jpa')
// 添加 MySQL连接驱动 的依赖
compile('mysql:mysql-connector-java:6.0.5')
// 添加 Thymeleaf Spring Security 依赖,与 Thymeleaf 版本⼀致都是 3.x
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE')
// 添加 Apache Commons Lang 依赖
compile('org.apache.commons:commons-lang3:3.5')
// 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依 testCompile('org.springframework.boot:spring-boot-starter-test') }
到此这篇关于Gradle的使⽤教程详解的⽂章就介绍到这了,更多相关Gradle使⽤内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
因篇幅问题不能全部显示,请点此查看更多更全内容