Gradle在Mac上安裝及Idea中配置


前言

筆者在安裝Gradle的過程中發現過程有點繁瑣且安裝教程比較少,因此記錄一下安裝過程,方便同行參考

環境信息

macOS:10.15.5
IntelliJ IDEA:2020.3
Gradle:6.8.2
Java:1.8.0_151
Homebrew: 3.0.1

進入Gradle官網

點此進入Gradle官網

點擊安裝

安裝文檔

包管理安裝

安裝brew

本次安裝使用brew安裝,brew可以簡化 macOS 和 Linux 操作系統上軟件的安裝

//brew是ruby開發的,需要確認ruby是否已安裝,默認是已經安裝的
//檢查ruby是否安裝
huanliu@B-LW01J1WL-0028 ~ % ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

//安裝brew命令,此命令可以進入官網查看
//輸入命令之后需要輸入一次密碼
//安裝時間稍長,不要着急
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"


//檢查brew是否安裝成功
huanliu@B-LW01J1WL-0028 ~ % brew --version
Homebrew 3.0.1

正式安裝Gradle

//命令
//筆者安裝需要10分鍾左右,主要是下載資源時間較長,主要看網速了
brew install gradle

//檢查是否安裝成功
huanliu@B-LW01J1WL-0028 ~ % gradle -v

Welcome to Gradle 6.8.2!

Here are the highlights of this release:
 - Faster Kotlin DSL script compilation
 - Vendor selection for Java toolchains
 - Convenient execution of tasks in composite builds
 - Consistent dependency resolution

For more details see https://docs.gradle.org/6.8.2/release-notes.html


------------------------------------------------------------
Gradle 6.8.2
------------------------------------------------------------

Build time:   2021-02-05 12:53:00 UTC
Revision:     b9bd4a5c6026ac52f690eaf2829ee26563cad426

Kotlin:       1.4.20
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          15.0.1 (Oracle Corporation 15.0.1+9)
OS:           Mac OS X 10.15.5 x86_64

Idea中配置Gradle

gradle安裝目錄

Idea配置

Gradle user home : 安裝目錄 + libexec
本例:/usr/local/Cellar/gradle/6.8.2/libexec

Gradle鏡像配置

//全局配置,在${USER_HOME}/.gradle/目錄下創建 init.gradle 文件,添加如下內容即可
allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        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
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

//單個項目,在項目中的 build.gradle 添加如下內容
buildscript {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
                maven{ url 'http://maven.aliyun.com/nexus/content/repositories/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 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM