mac下安装多个版本的gradle[开发环境.gradle.mac.brew]

这里写自定义目录标题

思路

# $ brew install 指定版本的gradle.rb文件,例如:
$ brew install /Volumes/TmpDownload/gradle.rb

在安装过一次后,再安装另外的版本时,会出现类似以下的提示

Error: gradle 旧版本 is already installed
To install 新版本, first run `brew unlink gradle`.

提示运行brew unlink gradle,再使用相应的brew install 指定版本的gradle.rb文件就可以了。

几个版本的gradle.rb文件内容

gradle.rb文件内容可以根据https://github.com/Homebrew/homebrew-core/里的Formula/gradle.rb文件的历史提交信息来取得,其中有的版本需要做修改,如2.14版本里之前用的是sha1要改成sha256。

要查看gradle.rb文件的历史提交信息可以切换到相应的目录,使用命令:

git log master -- Formula/gradle.rb

来查看,并取得相应版本的内容,以下列出几个版本的内容

5.6.4

class Gradle < Formula
  desc "Open-source build automation tool based on the Groovy and Kotlin DSL"
  homepage "https://www.gradle.org/"
  url "https://services.gradle.org/distributions/gradle-5.6.4-all.zip"
  sha256 "abc10bcedb58806e8654210f96031db541bcd2d6fc3161e81cb0572d6a15e821"

  bottle :unneeded

  depends_on :java => "1.8+"

  def install
    rm_f Dir["bin/*.bat"]
    libexec.install %w[bin docs lib media samples src]
    (bin/"gradle").write_env_script libexec/"bin/gradle", Language::Java.overridable_java_home_env
  end

  test do
    assert_match version.to_s, shell_output("#{bin}/gradle --version")
  end
end

4.10.2

class Gradle < Formula
  desc "Open-source build automation tool based on the Groovy and Kotlin DSL"
  homepage "https://www.gradle.org/"
  url "https://services.gradle.org/distributions/gradle-4.10.2-all.zip"
  sha256 "b7aedd369a26b177147bcb715f8b1fc4fe32b0a6ade0d7fd8ee5ed0c6f731f2c"

  bottle :unneeded

  option "with-all", "Installs Javadoc, examples, and source in addition to the binaries"

  depends_on :java => "1.7+"

  def install
    rm_f Dir["bin/*.bat"]
    libexec.install %w[bin lib]
    libexec.install %w[docs media samples src] if build.with? "all"
    (bin/"gradle").write_env_script libexec/"bin/gradle", Language::Java.overridable_java_home_env
  end

  test do
    assert_match version.to_s, shell_output("#{bin}/gradle --version")
  end
end

3.5

class Gradle < Formula
  desc "Build system based on the Groovy language"
  homepage "https://www.gradle.org/"
  url "https://services.gradle.org/distributions/gradle-3.5-all.zip"
  sha256 "d84bf6b6113da081d0082bcb63bd8547824c6967fe68704d1e3a6fde822b7212"

  bottle :unneeded

  option "with-all", "Installs Javadoc, examples, and source in addition to the binaries"

  depends_on :java => "1.7+"

  def install
    libexec.install %w[bin lib]
    libexec.install %w[docs media samples src] if build.with? "all"
    bin.install_symlink libexec/"bin/gradle"
  end

  test do
    ENV.java_cache
    assert_match version.to_s, shell_output("#{bin}/gradle --version")
  end
end

2.14

class Gradle < Formula
  desc "Build system based on the Groovy language"
  homepage "https://www.gradle.org/"
  url "https://downloads.gradle.org/distributions/gradle-2.14.1-bin.zip"
  sha256 "cfc61eda71f2d12a572822644ce13d2919407595c2aec3e3566d2aab6f97ef39"

  devel do
    url "https://downloads.gradle.org/distributions/gradle-3.0-milestone-2-bin.zip"
    sha256 "5c3e8e9a38c92ae85e05df609c6c1b6f51e5a08b39a26d61bd8a7044268135e7"
    version "3.0-milestone-2"
  end

  bottle :unneeded

  depends_on :java => "1.6+"

  def install
    libexec.install %w[bin lib]
    bin.install_symlink libexec/"bin/gradle"
  end

  test do
    ENV.java_cache
    assert_match version.to_s, shell_output("#{bin}/gradle --version")
  end
end

最终效果

$ brew info gradle
gradle: stable 6.2.2
Open-source build automation tool based on the Groovy and Kotlin DSL
https://www.gradle.org/
/usr/local/Cellar/gradle/2.14.1 (172 files, 47.4MB)
  Built from source on 2020-03-06 at 11:22:27
/usr/local/Cellar/gradle/3.5 (182 files, 77.7MB)
  Built from source on 2020-03-06 at 11:20:44
/usr/local/Cellar/gradle/4.10.2 (203 files, 83.7MB)
  Built from source on 2020-03-06 at 11:15:17
/usr/local/Cellar/gradle/5.6.4 (14,318 files, 245.3MB)
  Built from source on 2020-03-06 at 10:17:27
/usr/local/Cellar/gradle/6.0.1 (14,178 files, 247.9MB) *
  Built from source on 2019-12-10 at 15:33:14
From: https://mirrors.aliyun.com/homebrew/homebrew-core.git/Formula/gradle.rb
......

其中带星号*的那行是当前在使用的版本

切换版本

# brew switch gradle 想切换的版本,例如:
brew switch gradle 4.1 

参考

https://cloud.tencent.com/developer/article/1190055
https://blog.csdn.net/aa464971/article/details/84860937

相关文章

方法: 打开一个新的可跨域的chrome窗口实现方法: 1. 打开终...
MacBook Pro 14价格和配置对比 尺寸 SOC&#x9;内存&#...
现在给大家介绍一下如何查询MAC的生产日期,希望对你查询MAC...
1、点击【编辑虚拟机设置】,转到【CD/DVD (SATA)】,选中【...
注意:用哪个版本的 Python 运行安装脚本,pip 就被关联到哪...
Mac 安装nvm