/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Gradle 的安装与配置 + eclipse 里安装 Gradle 插件



一、什么是Gradle


基于Apache Ant和Apache Maven概念的项目自动化构建开源工具,基于Groovy这个特定DSL语言来进行声明项目设置,也增加了基于Kotlin语言的kotlin-based DSL。

简介:Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,build脚本使用Groovy编写。

特点:

①可切换的,像maven一样的基于约定的构建框架(约定优于配置)。
②多工程。
③依赖管理(基于Apache Ivy)。
④支持已有的Maven或者Ivy仓库。⑤支持传递性依赖管理,不需要使用pom.xml和ivy配置文件。




二、安装 Gradle:

1. 下载 gradle 二进制安装包


地址: https://gradle.org/releases/      如下图,下载二进制包。

image


2. 解压安装包到本地

如:D:\develop_soft\gradle-8.0.2-bin\gradle-8.0.2   路径下

image



3. 配置环境变量 (配置方法参照 java):


GRADLE_HOME:C:\software\gradle4.6

image


Path:;%GRADLE_HOME%\bin

image

image

gradle配置仓库

GRADLE_USER_HOME变量的值是本地仓库目录用来保存gradle下载的依赖包。

全局远程仓库配置:在解压的文件夹D:\develop_soft\gradle-8.0.2-bin\gradle-8.0.2\init.d中新建文件init.gradle(下文有个介绍),填充以下内容

image


allprojects{
    repositories {
	    maven{
		allowInsecureProtocol =true
		url= 'https://maven.aliyun.com/repository/public'

		}

        def REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
        //做一些国外不稳定的地址连接的替换
        all {
            ArtifactRepository repo ->
    if (repo instanceof MavenArtifactRepository) {
                def url = repo.url.toString()
                if (url.startsWith('https://artifacts.elastic.co/maven')
				||url.startsWith('https://repo1.maven.org/maven2') 
				|| url.startsWith('https://repo.maven.org/maven2') 
				|| url.startsWith('https://jcenter.bintray.com/')
				||url.startsWith('https://repo.maven.apache.org/maven2')
				) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
    }
}


项目级别远程仓库配置:在每个gradle构建的项目中 的build.gradle文件中新增配置


repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenCentral()
}




4. 查看是否安装成功:


cmd 终端输入:gradle –v 回车


显示如下图则安装成功。

image


三:eclipse 配置 gradle



1、在 eclipse 里配置 gradle 路径


然后,需要把 gradle 的路径配置到 eclipse 中,如下图:

image




2、安装 Gradle 插件:

在 eclipse 里安装 gradle 插件有三种方式:

2-1:第一种:help/install new software 在线安装


(1) 在线安装地址指南:


https://github.com/eclipse/buildship/blob/master/docs/user/Installation.md

通过上面地址找到不同的 eclipse 版本对应的插件版本链接,如下图所示。


image

Update sites for Buildship 3.x
Eclipse Version Type Update Site
2022-03 snapshot https://download.eclipse.org/buildship/updates/e423/snapshots/3.x
2022-03 milestone https://download.eclipse.org/buildship/updates/e423/milestones/3.x
2022-03 release https://download.eclipse.org/buildship/updates/e423/releases/3.x
2021-12 snapshot https://download.eclipse.org/buildship/updates/e422/snapshots/3.x
2021-12 milestone https://download.eclipse.org/buildship/updates/e422/milestones/3.x
2021-12 release https://download.eclipse.org/buildship/updates/e422/releases/3.x
2021-09 snapshot https://download.eclipse.org/buildship/updates/e421/snapshots/3.x
2021-09 milestone https://download.eclipse.org/buildship/updates/e421/milestones/3.x
2021-09 release https://download.eclipse.org/buildship/updates/e421/releases/3.x
2021-06 snapshot https://download.eclipse.org/buildship/updates/e420/snapshots/3.x
2021-06 milestone https://download.eclipse.org/buildship/updates/e420/milestones/3.x
2021-06 release https://download.eclipse.org/buildship/updates/e420/releases/3.x
2021-03 snapshot https://download.eclipse.org/buildship/updates/e419/snapshots/3.x
2021-03 milestone https://download.eclipse.org/buildship/updates/e419/milestones/3.x
2021-03 release https://download.eclipse.org/buildship/updates/e419/releases/3.x
2020-12 snapshot https://download.eclipse.org/buildship/updates/e418/snapshots/3.x
2020-12 milestone https://download.eclipse.org/buildship/updates/e418/milestones/3.x
2020-12 release https://download.eclipse.org/buildship/updates/e418/releases/3.x
2020-09 snapshot https://download.eclipse.org/buildship/updates/e417/snapshots/3.x
2020-09 milestone https://download.eclipse.org/buildship/updates/e417/milestones/3.x
2020-09 release https://download.eclipse.org/buildship/updates/e417/releases/3.x
2020-06 snapshot https://download.eclipse.org/buildship/updates/e416/snapshots/3.x
2020-06 milestone https://download.eclipse.org/buildship/updates/e416/milestones/3.x
2020-06 release https://download.eclipse.org/buildship/updates/e416/releases/3.x
2020-03 snapshot https://download.eclipse.org/buildship/updates/e415/snapshots/3.x
2020-03 milestone https://download.eclipse.org/buildship/updates/e415/milestones/3.x
2020-03 release https://download.eclipse.org/buildship/updates/e415/releases/3.x
2019-12 snapshot https://download.eclipse.org/buildship/updates/e414/snapshots/3.x
2019-12 milestone https://download.eclipse.org/buildship/updates/e414/milestones/3.x
2019-12 release https://download.eclipse.org/buildship/updates/e414/releases/3.x
2019-09 snapshot https://download.eclipse.org/buildship/updates/e413/snapshots/3.x
2019-09 milestone https://download.eclipse.org/buildship/updates/e413/milestones/3.x
2019-09 release https://download.eclipse.org/buildship/updates/e413/releases/3.x
2019-06 snapshot https://download.eclipse.org/buildship/updates/e412/snapshots/3.x
2019-06 milestone https://download.eclipse.org/buildship/updates/e412/milestones/3.x
2019-06 release https://download.eclipse.org/buildship/updates/e412/releases/3.x
2019-03 snapshot https://download.eclipse.org/buildship/updates/e411/snapshots/3.x
2019-03 milestone https://download.eclipse.org/buildship/updates/e411/milestones/3.x
2019-03 release https://download.eclipse.org/buildship/updates/e411/releases/3.x
2018-12 snapshot https://download.eclipse.org/buildship/updates/e410/snapshots/3.x
2018-12 milestone https://download.eclipse.org/buildship/updates/e410/milestones/3.x
2018-12 release https://download.eclipse.org/buildship/updates/e410/releases/3.x
2018-09 snapshot https://download.eclipse.org/buildship/updates/e49/snapshots/3.x
2018-09 milestone https://download.eclipse.org/buildship/updates/e49/milestones/3.x
2018-09 release https://download.eclipse.org/buildship/updates/e49/releases/3.x
Photon (4.8) snapshot https://download.eclipse.org/buildship/updates/e48/snapshots/3.x
Photon (4.8) milestone https://download.eclipse.org/buildship/updates/e48/milestones/3.x
Photon (4.8) release https://download.eclipse.org/buildship/updates/e48/releases/3.x
Update sites for Buildship 2.x
Eclipse Version Type Update Site
Photon (4.8) release https://download.eclipse.org/buildship/updates/e48/releases/2.x
Photon (4.8) milestone https://download.eclipse.org/buildship/updates/e48/milestones/2.x
Photon (4.8) snapshot https://download.eclipse.org/buildship/updates/e48/snapshots/2.x
Oxygen (4.7) release https://download.eclipse.org/buildship/updates/e47/releases/2.x
Oxygen (4.7) milestone https://download.eclipse.org/buildship/updates/e47/milestones/2.x
Oxygen (4.7) snapshot https://download.eclipse.org/buildship/updates/e47/snapshots/2.x
Neon (4.6) release https://download.eclipse.org/buildship/updates/e46/releases/2.x
Neon (4.6) milestone https://download.eclipse.org/buildship/updates/e46/milestones/2.x
Neon (4.6) snapshot https://download.eclipse.org/buildship/updates/e46/snapshots/2.x
Mars (4.5) release https://download.eclipse.org/buildship/updates/e45/releases/2.x
Mars (4.5) milestone https://download.eclipse.org/buildship/updates/e45/milestones/2.x
Mars (4.5) snapshot https://download.eclipse.org/buildship/updates/e45/snapshots/2.x
Luna (4.4) release https://download.eclipse.org/buildship/updates/e44/releases/2.x
Luna (4.4) milestone https://download.eclipse.org/buildship/updates/e44/milestones/2.x
Luna (4.4) snapshot https://download.eclipse.org/buildship/updates/e44/snapshots/2.x
Kepler (4.3) release https://download.eclipse.org/buildship/updates/e43/releases/2.x
Kepler (4.3) milestone https://download.eclipse.org/buildship/updates/e43/milestones/2.x
Kepler (4.3) snapshot https://download.eclipse.org/buildship/updates/e43/snapshots/2.x
Juno (4.2) release https://download.eclipse.org/buildship/updates/e42/releases/2.x
Juno (4.2) milestone https://download.eclipse.org/buildship/updates/e42/milestones/2.x
Juno (4.2) snapshot https://download.eclipse.org/buildship/updates/e42/snapshots/2.x
Update sites for Buildship 1.x
Eclipse Version Update Site
Neon (4.6) https://download.eclipse.org/buildship/updates/e46/releases/1.0
Mars (4.5) https://download.eclipse.org/buildship/updates/e45/releases/1.0
Luna (4.4) https://download.eclipse.org/buildship/updates/e44/releases/1.0
Kepler (4.3) https://download.eclipse.org/buildship/updates/e43/releases/1.0
Juno (4.2) https://download.eclipse.org/buildship/updates/e42/releases/1.0
Indigo (3.7) https://download.eclipse.org/buildship/updates/e37/releases/1.0
Helios (3.6) https://download.eclipse.org/buildship/updates/e36/releases/1.0

The continuous integration server generates nightly snapshot releases each day 23:00 CET which instantly become available at the snapshot update sites above. In regular intervals, the Buildship team also creates new milestone releases and makes them available at the milestone update sites.




找到对应的 eclipse 版本拷贝链接。

(2) 打开 eclipse,依次按以下步骤操作


b04517217062276f148b50e65a7c5f662b5.png


image

image

image


image

image

image

image

image


image





2-2:第二种:help/eclipse MarketPlace 在线安装

(1) 打开 eclipse,依次按以下步骤操作

(2) 搜索 gradle,选择 Buildship 进行安装。

2-3:第三种:离线安装,下载 gradle 插件包

步骤如下:
1、打开网址:https://spring.io/tools/sts/all,找到与自己 eclipse 版本匹配的压缩包,下载下来。
2、解压缩后可以看见这是一个 eclipse 插件形式的包,将需要安装的插件挑出来或将整个文件

里的包放到 eclipse 里对应的文件夹下即可(主要是 features 和 plugins 文件夹)。
3、然后关闭 eclipse,删除 eclipse 安装目录里 configuration\org.eclipse.update 这个文件夹,

再打开 eclipse,即可看见已经安装成功的插件。





四、idea 配置 Gradle

Gradle user home : 就是环境变量中配置的 GRADLE_USER_HOME 地址


image





五、init.gradle介绍


init.gradle文件在build开始之前执行,所以你可以在这个文件配置一些你想预先加载的操作,例如配置build日志输出、配置你的机器信息,比如jdk安装目录,配置在build时的账户鉴权信息(比如仓库或者数据库的认证信息)等等

启用init.gradle文件的方法:

1、在命令行指定文件,例如:gradle –init-script yourdir/init.gradle -q taskName你可以多次输入此命令来指定多个init文件。

2、把以.gradle文件放到USER_HOME/.gradle/目录下。例如init.gradle文件。

3、把以.gradle结尾的文件放到USER_HOME/.gradle/init.d/目录下。例如init.gradle文件。

4、把以.gradle结尾的文件放到GRADLE_HOME/init.d/目录下。例如init.gradle文件。

如果存在上面的4种方式的2种以上,gradle会按上面的1-4序号依次执行这些文件,如果给定目录下存在多个init脚本(.gradle文件),会按拼音a-z顺序执行。

 


六、gradle 的目录结构


gradle 项目默认目录和 maven 项目目录结构一致,都是基于约定大于配置(Convention Over Configuration),其完整的项目目录结构如下:

image



提示:

1、只有war工程才有webapp目录,对于普通的jar工程并没有webapp目录。

2、gradlelew 与 gradle.bat 执行的指定wrapper版本中的gradle指定,不是本地安装的gradle指令。


七、Gradle 中的常用指令


Gradle常用指令 作用
gradle clean 清空build目录
gradle classed 编译业务代码和配置文件
gradle test 编译测试代码,生成测试报告
gradle buid 构建项目
gradle build -x test 跳过测试构建


注意:gradle 的指令要在含有build.gradle的目录执行。

1、查看项目的任务依赖

要查看项目的依赖关系图,我们可以运行以下命令:

$ gradle dependencies
......省略部分输出.......
kaptTestRelease
\--- org.jetbrains.kotlin:kotlin-annotation-processing-gradle:1.2.51
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.51
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.51
     |    \--- org.jetbrains:annotations:13.0
     \--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.51
          +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.51 (*)
          \--- org.jetbrains.kotlin:kotlin-script-runtime:1.2.51

lintChecks - Configuration to apply external lint check jar
No dependencies
......省略部分输出.......


Tips:上面我们运行的命令输出的是整个项目的依赖,有时候我们只需要查查看当前 module 的依赖,那么我们怎么查查看呢?如下,我们输入gradle [module_name]:dependencies,例如我们想要查看 app 模块的依赖,我们可以执行如下命令

//查看APP模块的依赖
$ gradle app:dependencies

2、排除某些任务

前面我么讲了 Gradle 的任务,我们可以用-x选项去排除一个任务。我们在build.gradle中定义的task mTest,我们如果想要跳过mTest,可以用以下的命令

//buid.gradle中定义的Task,mTest
task mTest{
    doLast{
        println "这是测试Task"
    }
}

//在控制台执行命令排除
$ gradle clean build -x mTest

3、查看 Gradle 的 Task

我们在开发过程中有时候难免会要查看 Gradle 构建有哪些任务,那么我们就可以使用下面的命令来查看:

$ gradle task

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
build - Assembles and tests this project.
.........省略部分任务..............

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.
.........省略部分任务..............

我们可以看到这个命令执行后会输出输出各种任务。

Tips: 我们看到这个很多,其实该命令输出的不是所有的命令,要查看项目构建的所有命令,我们需要执行的时候 后面加上 --all如下:


$ gradle task -all
posted @ 2023-03-18 23:28  一品堂.技术学习笔记  阅读(2070)  评论(0编辑  收藏  举报