'How to Brew install Ant with specific OpenJDK version?

I'm trying to install Ant such that it is built with OpenJDK@11 instead of the latest OpenJDK? brew install ant installs it with OpenJDK@18 currently. I'm using ant to build OpenCV with Java from source, and I need it to be built with Java 11.

I've run brew edit ant and tried to configure the following:

  def install
    rm Dir["bin/*.{bat,cmd,dll,exe}"]
    libexec.install Dir["*"]
    bin.install_symlink Dir["#{libexec}/bin/*"]
    rm bin/"ant"
    (bin/"ant").write <<~EOS
      #!/bin/bash
      JAVA_HOME="${JAVA_HOME:-#{Formula["openjdk"].opt_prefix}}" exec "#{libexec}/bin/ant" -lib #{HOMEBREW_PREFIX}/share/ant "$@"
    EOS

But haven't had luck with getting the config right. Any help is appreciated!



Solution 1:[1]

This is the diff that you need (basically replace openjdk with openjdk@11):

diff --git a/Formula/ant.rb b/Formula/ant.rb
index 8c2aba387a7..6096a78ac65 100644
--- a/Formula/ant.rb
+++ b/Formula/ant.rb
@@ -11,7 +11,7 @@ class Ant < Formula
     sha256 cellar: :any_skip_relocation, all: "65361546d11f6cd675bc64aae75cd14dc3e77e7ed466b45e871bd09780df23f1"
   end

-  depends_on "openjdk"
+  depends_on "openjdk@11"

   resource "ivy" do
     url "https://www.apache.org/dyn/closer.lua?path=ant/ivy/2.5.0/apache-ivy-2.5.0-bin.tar.gz"
@@ -32,7 +32,7 @@ class Ant < Formula
     rm bin/"ant"
     (bin/"ant").write <<~EOS
       #!/bin/bash
-      JAVA_HOME="${JAVA_HOME:-#{Formula["openjdk"].opt_prefix}}" exec "#{libexec}/bin/ant" -lib #{HOMEBREW_PREFIX}/share/ant "$@"
+      JAVA_HOME="${JAVA_HOME:-#{Formula["openjdk@11"].opt_prefix}}" exec "#{libexec}/bin/ant" -lib #{HOMEBREW_PREFIX}/share/ant "$@"
     EOS

After brew edit ant, and then brew install -s ant, that should be it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chenrui