centos7 linux安装gradle指定版本

centos7安装gradle指定版本

  • 1. 下载指定安装的版本
  • 2. 新建存放版本目录并解压
  • 3. 配置环境变量
  • 4. 配置国内镜像
    • 4.1 全局配置
    • 4.2 项目单独配置

1. 下载指定安装的版本

假如安装版本是4.10.3

$ wget https://downloads.gradle-dn.com/distributions/gradle-4.10.3-all.zip

2. 新建存放版本目录并解压

$ mkdir /opt/gradle
$ unzip -d /opt/gradle gradle-4.10.3-all.zip
$ ls /opt/gradle/gradle-4.10.3
LICENSE  NOTICE  bin  getting-started.html  init.d  lib  media

3. 配置环境变量

编辑配置文件

$ vi ~/.bash_profile

在文件中添加一行export

export PATH=$PATH:/opt/gradle/gradle-4.10.3/bin

使配置文件生效

$ source ~/.bash_profile

$ gradle -v
------------------------------------------------------------
Gradle 4.10.3
------------------------------------------------------------

4. 配置国内镜像

这是可选项,按需操作。

可进行全局配置或者是单独项目配置,设置全局配置后,则不需要单独配置。

4.1 全局配置

新增并编辑文件

vi ~/.gradle/init.gradle

新增以下内容并保存

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
        def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
        def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
                if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
                    remove repo
                }
                if (url.startsWith('https://plugins.gradle.org/m2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GRADLE_PLUGIN_URL."
                    remove repo
                }
            }
        }
        maven { url ALIYUN_REPOSITORY_URL }
        maven { url ALIYUN_JCENTER_URL }
        maven { url ALIYUN_GOOGLE_URL }
        maven { url ALIYUN_GRADLE_PLUGIN_URL }
    }
}

4.2 项目单独配置

在项目中找到文件build.gradle,并修改

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/jcenter/'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }        
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/jcenter/'}
    }
}

你可能感兴趣的:(运维笔记)