Gradle的使用教程详解 您所在的位置:网站首页 gradle使用maven仓库 Gradle的使用教程详解

Gradle的使用教程详解

2023-04-10 05:57| 来源: 网络整理| 查看: 265

Gradle的使用教程详解

Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,目前也增加了基于Kotlin语言的kotlin-bas

Gradle的使用教程详解

ed DSL,抛弃了基于XML的各种繁琐配置。

面向java应用为主。当前其支持的语言限于Java、Groovy、Kotlin和Scala,计划未来将支持更多的语言。

一、相关介绍

Gradle是一个好用的构建工具 ,使用它的原因是:

配置相关依赖代码量少,不会像maven一样xml过多 

打包编译测试发布都有,而且使用起来方便 

利用自定义的任务可以完成自己想要的功能

二、安装

     下载地址http://services.gradle.org/distributions/  ,下载你所需要对应的版本,我这里下载的是gradle-4.7-bin.zip。下载后解压到你想要的目录即可,然后设置环境变量:

在cmd模式下查看,出现以下信息证明安装成功:

然后我们可以在在环境变量里配置gradle默认的仓库地址(和maven不太一样):

三、IED中的使用

1、IDEA

使用idea创建一个web的Gradle项目

然后对项目进行打包运行:

双击war

打包完成之后的war文件会在:

然后把war放入对应的tomcat目录即可,这里就不多解释了。

2、Eclipse

eclipse中要自己安装插件,插件路径为:http://download.eclipse.org/buildship/updates/e46/releases/2.x/ 。

四、问题说明

1、解释build.gradle和settings.gradle

首先是一个项目包含group、name、version 。settin

gs.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')

}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有