Skip to content

Commit 9dfe9fa

Browse files
committed
System Java now works on macOS
Fixes #132 Added special case for macOS in all 4 set-java-home scripts. Also created tests to validate this. Unfortunately xonsh does not seem to have a way to exit with other status then 0, so this will not report errors (yet). Updated the README.md to contain information on how to use the set-java-home scripts. Cloned mstksg/get-package and released delgurth/get-package@v3 because it was using linuxbrew on Ubuntu and because I needed the apt-update (not released by mstksg) and -y flag in the apt-get install (when running github action on https://github.com/nektos/act) added .editorconfig because of mixed indent styles added .gitignore to ignore Intellij directory asdf/asdf.sh needs $PWD in front as soon as you change directories added tests for JAVA_HOME setting added tests for macOS JAVA_HOME setting in case version is set to system Unfortunately the tests are not exactly as it would be on a system because of the way github actions spawn shells. Have to call the bash function _asdf_java_prompt_command and the fish & zsh function asdf_update_java_home to actually update the JAVA_HOME.
1 parent cedde6d commit 9dfe9fa

8 files changed

Lines changed: 188 additions & 46 deletions

File tree

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 4
4+
insert_final_newline = true
5+
trim_trailing_whitespace = true
6+
7+
[*.{y,ya}ml]
8+
indent_size = 2

.github/workflows/tests.yml

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
TERM: dumb
1717
steps:
1818
- uses: actions/checkout@v2
19-
- uses: mstksg/get-package@v1
19+
- uses: delgurth/get-package@v3
2020
with:
21-
brew: bash coreutils jq shellcheck
22-
apt-get: jq shellcheck
21+
brew: bash coreutils jq shellcheck fish xonsh zsh
22+
apt-get: jq shellcheck zsh fish xonsh bash
2323
- name: Run ShellCheck
2424
run: |
2525
shellcheck -V
@@ -34,14 +34,114 @@ jobs:
3434
env:
3535
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3636
run: |
37-
. asdf/asdf.sh
37+
. $PWD/asdf/asdf.sh
3838
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
3939
- name: macOS Check java_home integration
40+
if: matrix.os == 'macOS-latest'
4041
env:
4142
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4243
run: |
4344
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
4445
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
4546
. asdf/asdf.sh
4647
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"
48+
- name: Setup JAVA_HOME setting
49+
env:
50+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
run: |
52+
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
53+
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
54+
. $PWD/asdf/asdf.sh
55+
asdf plugin add java "$GITHUB_WORKSPACE"
56+
asdf install java adoptopenjdk-8.0.252+9.1.openj9-0.20.0
57+
asdf global java adoptopenjdk-8.0.252+9.1.openj9-0.20.0
58+
echo ". $PWD/asdf/asdf.sh" >> $HOME/.zshrc
59+
echo ". $HOME/.asdf/plugins/java/set-java-home.zsh" >> $HOME/.zshrc
60+
mkdir -p $HOME/.config/fish/functions/
61+
ln -s $HOME/.asdf/plugins/java/set-java-home.fish $HOME/.config/fish/functions/asdf_update_java_home.fish
62+
echo "source $PWD/asdf/asdf.fish" >> $HOME/.config/fish/config.fish
63+
echo "asdf_update_java_home" >> $HOME/.config/fish/config.fish
64+
- name: Run JAVA_HOME setting test fish
65+
env:
66+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67+
run: |
68+
echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0
69+
shell: fish {0}
70+
- name: Run JAVA_HOME setting test bash
71+
env:
72+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
run: |
74+
. $PWD/asdf/asdf.sh
75+
. set-java-home.bash
76+
_asdf_java_prompt_command
77+
echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0
78+
shell: bash {0}
79+
- name: Run JAVA_HOME setting test xonsh (returns success even though it might fail!)
80+
env:
81+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
82+
run: |
83+
source-bash $PWD/asdf/asdf.sh
84+
source set-java-home.xsh
85+
echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0
86+
shell: xonsh {0}
87+
- name: Run JAVA_HOME setting test zsh
88+
env:
89+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90+
run: |
91+
source $HOME/.zshrc
92+
asdf_update_java_home
93+
echo $JAVA_HOME | grep adoptopenjdk-8.0.252+9.1.openj9-0.20.0
94+
shell: zsh {0}
95+
- name: Setup system tool-version test on macOS
96+
if: matrix.os == 'macOS-latest'
97+
env:
98+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
99+
run: |
100+
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
101+
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
102+
. $PWD/asdf/asdf.sh
103+
. set-java-home.bash
104+
asdf install java zulu-16.28.11
105+
mkdir system
106+
cd system
107+
echo "java system" > .tool-versions
108+
- name: Run system tool-version test on macOS with fish
109+
if: matrix.os == 'macOS-latest'
110+
env:
111+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112+
run: |
113+
cd system
114+
asdf_update_java_home
115+
echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/"
116+
shell: fish {0}
117+
- name: Run system tool-version test on macOS with bash
118+
if: matrix.os == 'macOS-latest'
119+
env:
120+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
121+
run: |
122+
. $PWD/asdf/asdf.sh
123+
. set-java-home.bash
124+
cd system
125+
_asdf_java_prompt_command
126+
echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/"
127+
shell: bash {0}
128+
- name: Run system tool-version test on macOS with xonsh (returns success even though it might fail!)
129+
if: matrix.os == 'macOS-latest'
130+
env:
131+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
132+
run: |
133+
source-bash $PWD/asdf/asdf.sh
134+
source set-java-home.xsh
135+
cd system
136+
echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/"
137+
shell: xonsh {0}
138+
- name: Run system tool-version test on macOS with zsh
47139
if: matrix.os == 'macOS-latest'
140+
env:
141+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142+
run: |
143+
source $HOME/.zshrc
144+
cd system
145+
asdf_update_java_home
146+
echo $JAVA_HOME | grep "/Library/Java/JavaVirtualMachines/"
147+
shell: zsh {0}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,51 @@
1111
- [unzip](http://infozip.sourceforge.net/UnZip.html)
1212
- [jq](https://stedolan.github.io/jq/) (only for updating the release data)
1313

14+
_Besides bash (the shell used by the maintainer) this plugin **should** work on Fish, Zsh and Xonsh_
15+
1416
## Install
1517

1618
```
17-
asdf plugin-add java https://github.com/halcyon/asdf-java.git
19+
asdf plugin add java
1820
```
1921

2022
## Use
2123

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

24-
## Install
26+
## Install new Java version
2527

2628
List candidate JDKs:
2729

2830
`asdf list-all java`
2931

3032
Install a candidate listed from the previous command like this:
3133

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

3436
Select an installed candidate for use like this:
3537

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

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

41-
`. ~/.asdf/plugins/java/set-java-home.bash`
42-
43-
For zsh shell, instead use:
44-
45-
`. ~/.asdf/plugins/java/set-java-home.zsh`
43+
### Bash
44+
`echo ". ~/.asdf/plugins/java/set-java-home.bash" >> ~/.bashrc`
4645

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

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

53-
`source ~/.asdf/plugins/java/set-java-home.xsh`
57+
### Xonsh
58+
`echo "source ~/.asdf/plugins/java/set-java-home.xsh" >> ~/.xonshrc`
5459

5560
## macOS Integration
5661
Some applications in macOS use `/usr/libexec/java_home` to set java home.

set-java-home.bash

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@ function _asdf_java_absolute_dir_path {
55
}
66

77
function _asdf_java_update_java_home() {
8-
local java_path
9-
java_path="$(asdf which java)"
10-
if [[ -n "${java_path}" ]]; then
11-
export JAVA_HOME
12-
JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")"
13-
fi
8+
local java_path
9+
java_path="$(asdf which java)"
10+
if [[ -n "${java_path}" ]]; then
11+
if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then
12+
export JAVA_HOME
13+
JAVA_HOME="$(/usr/libexec/java_home)"
14+
else
15+
export JAVA_HOME
16+
JAVA_HOME="$(dirname "$(_asdf_java_absolute_dir_path "${java_path}")")"
17+
fi
18+
fi
1419
}
1520

1621
function _asdf_java_prompt_command() {
17-
if [[ "${PWD}" == "${LAST_PWD}" ]]; then
18-
return
19-
fi
20-
LAST_PWD="${PWD}"
21-
_asdf_java_update_java_home
22+
if [[ "${PWD}" == "${LAST_PWD}" ]]; then
23+
return
24+
fi
25+
LAST_PWD="${PWD}"
26+
_asdf_java_update_java_home
2227
}
2328

2429
if ! [[ "${PROMPT_COMMAND:-}" =~ _asdf_java_prompt_command ]]; then
25-
PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
30+
PROMPT_COMMAND="_asdf_java_prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
2631
fi

set-java-home.fish

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
function asdf_update_java_home --on-event fish_prompt
2-
set --local java_path (asdf which java)
3-
if test -n "$java_path"
4-
set --local full_path (builtin realpath "$java_path")
5-
6-
# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
7-
# in order to get the correct JAVA_HOME directory
8-
set -gx JAVA_HOME (dirname (dirname "$full_path"))
9-
end
2+
set --local java_path (asdf which java)
3+
if test -n "$java_path"
4+
set --local full_path (builtin realpath "$java_path")
5+
6+
switch (uname)
7+
case Darwin
8+
if test $java_path = "/usr/bin/java"
9+
set -gx JAVA_HOME (/usr/libexec/java_home)
10+
else
11+
# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
12+
# in order to get the correct JAVA_HOME directory
13+
set -gx JAVA_HOME (dirname (dirname "$full_path"))
14+
end
15+
case '*'
16+
# `builtin realpath` returns $JAVA_HOME/bin/java, so we need two `dirname` calls
17+
# in order to get the correct JAVA_HOME directory
18+
set -gx JAVA_HOME (dirname (dirname "$full_path"))
19+
end
20+
end
1021
end

set-java-home.xsh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#!/usr/bin/env xonsh
22

33
def asdf_update_java_home() -> None:
4-
$java_path=$(asdf which java)
4+
import xonsh.platform
5+
$java_path=$(asdf which java).rstrip('\n')
56
if len($java_path) > 0:
6-
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
7+
if xonsh.platform.ON_DARWIN:
8+
if $java_path == '/usr/bin/java':
9+
$JAVA_HOME=$(/usr/libexec/java_home).rstrip('\n')
10+
else:
11+
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
12+
else:
13+
$JAVA_HOME=$(dirname $(dirname $(realpath $java_path))).rstrip('\n')
714
del $java_path
815

916
@events.on_chdir

set-java-home.zsh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
asdf_update_java_home() {
2-
local java_path
3-
java_path="$(asdf which java)"
4-
if [[ -n "${java_path}" ]]; then
5-
export JAVA_HOME
6-
JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")"
7-
fi
2+
local java_path
3+
java_path="$(asdf which java)"
4+
if [[ -n "${java_path}" ]]; then
5+
if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then
6+
export JAVA_HOME
7+
JAVA_HOME="$(/usr/libexec/java_home)"
8+
else
9+
export JAVA_HOME
10+
JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")"
11+
fi
12+
fi
813
}
914

1015
autoload -U add-zsh-hook

0 commit comments

Comments
 (0)