Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System Java now works on macOS #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.{y,ya}ml]
indent_size = 2
delgurth marked this conversation as resolved.
Show resolved Hide resolved
112 changes: 107 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
TERM: dumb
steps:
- uses: actions/checkout@v2
- uses: mstksg/get-package@v1
- uses: delgurth/get-package@v3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason the replace the GitHub Action here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there was a specific reason, v1 installed packages twice on newer Ubuntu images, once via brew and once via the normal apt-get. mstksg/get-package#5

with:
brew: bash coreutils jq shellcheck
apt-get: jq shellcheck
brew: bash coreutils jq shellcheck fish xonsh zsh
apt-get: jq shellcheck zsh fish xonsh bash
- name: Run ShellCheck
run: |
shellcheck -V
Expand All @@ -34,14 +34,116 @@ jobs:
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
. asdf/asdf.sh
. $PWD/asdf/asdf.sh
asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version adoptopenjdk-8.0.252+9.1.openj9-0.20.0 java -version
- name: macOS Check java_home integration
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
. asdf/asdf.sh
. $PWD/asdf/asdf.sh
asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version zulu-8.52.0.23 /usr/libexec/java_home -V 2>&1 | grep "Zulu 8.52.0.23"
- name: Setup JAVA_HOME setting
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
. $PWD/asdf/asdf.sh
asdf plugin add java "$GITHUB_WORKSPACE"
asdf install java adoptopenjdk-8.0.252+9.1.openj9-0.20.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the changes in #140, this (and all subsequent usages of this identifier in this file) should probably be adoptopenjdk-openj9-8.0.252+9.1.openj9-0.20.0 or adoptopenjdk-8.0.252+9.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will see what is needed now to get things to work again properly. Been a long time since I last worked on this...

asdf global java adoptopenjdk-8.0.252+9.1.openj9-0.20.0
echo ". $PWD/asdf/asdf.sh" >> $HOME/.zshrc
echo ". $HOME/.asdf/plugins/java/set-java-home.zsh" >> $HOME/.zshrc
mkdir -p $HOME/.config/fish/functions/
ln -s $HOME/.asdf/plugins/java/set-java-home.fish $HOME/.config/fish/functions/asdf_update_java_home.fish
echo "source $PWD/asdf/asdf.fish" >> $HOME/.config/fish/config.fish
echo "asdf_update_java_home" >> $HOME/.config/fish/config.fish
- name: Run JAVA_HOME setting test fish
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0
shell: fish {0}
- name: Run JAVA_HOME setting test bash
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
. $PWD/asdf/asdf.sh
. set-java-home.bash
_asdf_java_prompt_command
echo $JAVA_HOME | grep -q adoptopenjdk-8.0.252+9.1.openj9-0.20.0
shell: bash {0}
- name: Run JAVA_HOME setting test xonsh
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$RAISE_SUBPROC_ERROR = True
source-bash $PWD/asdf/asdf.sh
source set-java-home.xsh
echo $JAVA_HOME | grep -q adoptopenjdk-8.0.252+9.1.openj9-0.20.0
shell: xonsh {0}
- name: Run JAVA_HOME setting test zsh
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
source $HOME/.zshrc
asdf_update_java_home
echo $JAVA_HOME | grep -q adoptopenjdk-8.0.252+9.1.openj9-0.20.0
shell: zsh {0}
- name: Setup system tool-version test on macOS
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
. $PWD/asdf/asdf.sh
. set-java-home.bash
asdf install java zulu-16.28.11
mkdir system
cd system
echo "java system" > .tool-versions
- name: Run system tool-version test on macOS with fish
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd system
asdf_update_java_home
echo $JAVA_HOME | grep -q "/Library/Java/JavaVirtualMachines/"
shell: fish {0}
- name: Run system tool-version test on macOS with bash
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
. $PWD/asdf/asdf.sh
. set-java-home.bash
cd system
_asdf_java_prompt_command
echo $JAVA_HOME | grep -q "/Library/Java/JavaVirtualMachines/"
shell: bash {0}
- name: Run system tool-version test on macOS with xonsh
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$RAISE_SUBPROC_ERROR = True
source-bash $PWD/asdf/asdf.sh
source set-java-home.xsh
cd system
echo $JAVA_HOME | grep -q "/Library/Java/JavaVirtualMachines/"
shell: xonsh {0}
- name: Run system tool-version test on macOS with zsh
if: matrix.os == 'macOS-latest'
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
source $HOME/.zshrc
cd system
asdf_update_java_home
echo $JAVA_HOME | grep -q "/Library/Java/JavaVirtualMachines/"
shell: zsh {0}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove this file again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this since I'm working from IntelliJ IDEA. This way I prevent my IDEA setup to be checked in, which I think is preferable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,51 @@
- [unzip](http://infozip.sourceforge.net/UnZip.html)
- [jq](https://stedolan.github.io/jq/) (only for updating the release data)

_Besides bash (the shell used by the maintainer) this plugin **should** work on Fish, Zsh and Xonsh_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in the GitHub workflows should show that it's working. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I wanted to make clear what was guaranteed. Getting the Xonsh part to work properly took me quite some time, which annoyed me a bit. That's what made me add this line.


## Install

```
asdf plugin-add java https://github.com/halcyon/asdf-java.git
asdf plugin add java
```

## Use

Check [asdf](https://asdf-vm.github.io/asdf/) for instructions on how to install & manage versions of Java.

## Install
## Install new Java version

List candidate JDKs:

`asdf list-all java`

Install a candidate listed from the previous command like this:

`asdf install java adopt-openjdk-12.0.2+10.2`
`asdf install java adoptopenjdk-12.0.2+10.1`

Select an installed candidate for use like this:

`asdf global java adopt-openjdk-12.0.2+10.2`
`asdf global java adoptopenjdk-12.0.2+10.1`

## JAVA_HOME
To set JAVA_HOME in your shell's initialization add the following:

`. ~/.asdf/plugins/java/set-java-home.bash`

For zsh shell, instead use:

`. ~/.asdf/plugins/java/set-java-home.zsh`
### Bash
`echo ". ~/.asdf/plugins/java/set-java-home.bash" >> ~/.bashrc`

For fish shell, instead use:
### Zsh
`echo ". ~/.asdf/plugins/java/set-java-home.zsh" >> ~/.zshrc`

`. ~/.asdf/plugins/java/set-java-home.fish`

For xonsh shell, instead use:
### Fish
```
mkdir -p ~/.config/fish/functions/
ln -s ~/.asdf/plugins/java/set-java-home.fish ~/.config/fish/functions/asdf_update_java_home.fish
echo "asdf_update_java_home" >> ~/.config/fish/config.fish
```
_the mkdir is only needed in case you didn't add any functions yet_

`source ~/.asdf/plugins/java/set-java-home.xsh`
### Xonsh
`echo "source ~/.asdf/plugins/java/set-java-home.xsh" >> ~/.xonshrc`

## macOS Integration
Some applications in macOS use `/usr/libexec/java_home` to set java home.
Expand Down
29 changes: 17 additions & 12 deletions set-java-home.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ function _asdf_java_absolute_dir_path {
}

function _asdf_java_update_java_home() {
local java_path
java_path="$(asdf which java)"
if [[ -n "${java_path}" ]]; then
export JAVA_HOME
JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")"
fi
local java_path
java_path="$(asdf which java)"
if [[ -n "${java_path}" ]]; then
if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then
export JAVA_HOME
JAVA_HOME="$(/usr/libexec/java_home)"
else
export JAVA_HOME
JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")"
fi
fi
}

function _asdf_java_prompt_command() {
if [[ "${PWD}" == "${LAST_PWD}" ]]; then
return
fi
LAST_PWD="${PWD}"
_asdf_java_update_java_home
if [[ "${PWD}" == "${LAST_PWD}" ]]; then
return
fi
LAST_PWD="${PWD}"
_asdf_java_update_java_home
}

if ! [[ "${PROMPT_COMMAND:-}" =~ _asdf_java_prompt_command ]]; then
PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
27 changes: 19 additions & 8 deletions set-java-home.fish
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
function asdf_update_java_home --on-event fish_prompt
set --local java_path (asdf which java)
if test -n "$java_path"
set --local full_path (builtin realpath "$java_path")

# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
# in order to get the correct JAVA_HOME directory
set -gx JAVA_HOME (dirname (dirname "$full_path"))
end
set --local java_path (asdf which java)
if test -n "$java_path"
set --local full_path (builtin realpath "$java_path")

switch (uname)
case Darwin
if test $java_path = "/usr/bin/java"
set -gx JAVA_HOME (/usr/libexec/java_home)
else
# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
# in order to get the correct JAVA_HOME directory
set -gx JAVA_HOME (dirname (dirname "$full_path"))
end
case '*'
# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
# in order to get the correct JAVA_HOME directory
set -gx JAVA_HOME (dirname (dirname "$full_path"))
end
end
end
11 changes: 9 additions & 2 deletions set-java-home.xsh
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env xonsh

def asdf_update_java_home() -> None:
$java_path=$(asdf which java)
import xonsh.platform
$java_path=$(asdf which java).rstrip('\n')
if len($java_path) > 0:
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
if xonsh.platform.ON_DARWIN:
if $java_path == '/usr/bin/java':
$JAVA_HOME=$(/usr/libexec/java_home).rstrip('\n')
else:
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
else:
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
del $java_path

@events.on_chdir
Expand Down
17 changes: 11 additions & 6 deletions set-java-home.zsh
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
asdf_update_java_home() {
local java_path
java_path="$(asdf which java)"
if [[ -n "${java_path}" ]]; then
export JAVA_HOME
JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")"
fi
local java_path
java_path="$(asdf which java)"
if [[ -n "${java_path}" ]]; then
if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then
export JAVA_HOME
JAVA_HOME="$(/usr/libexec/java_home)"
else
export JAVA_HOME
JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")"
fi
fi
}

autoload -U add-zsh-hook
Expand Down