Skip to content

Commit

Permalink
Add JDK17 support (#15)
Browse files Browse the repository at this point in the history
* Use latest go-utils, go-steputils dependencies

* Step description updates

* Add JDK17 support

* Fix error handling

* Improve error logging

* Fix java bin path on JDK8

* Clean up command creation
  • Loading branch information
ofalvai authored Apr 14, 2022
1 parent 63a7856 commit 207ceac
Show file tree
Hide file tree
Showing 27 changed files with 685 additions and 612 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Set java version
# Set Java version

[![Step changelog](https://shields.io/github/v/release/bitrise-steplib/bitrise-step-set-java-version?include_prereleases&label=changelog&color=blueviolet)](https://github.com/bitrise-steplib/bitrise-step-set-java-version/releases)

## 📚 Description
This Step helps you activate pre-installed Java (JDK) version on the build virtual machine.

This step helps you in setting an already installed java version on the virtual machine. Mind you, that this step can't install any java version, it's only for changing between the already installed versions.
<details>
<summary>Description</summary>

If you want to install other java version you can do it by [this guide](https://devcenter.bitrise.io/infrastructure/virtual-machines/#switching-to-a-java-version-not-installed-on-our-android-stacks).
This Step is not supposed to install any Java version on the fly. You can only pick from the already installed versions.
If you want to install other Java versions, check out [Using a Java version not installed on our Android stacks](https://devcenter.bitrise.io/infrastructure/virtual-machines/#using-a-java-version-not-installed-on-our-android-stacks).

### Troubleshooting
### Configuring the Step
1. In the **Java version to be set globally for the build** input, select an installed Java version you wish to use during the build run.

If the step fails to set the java version, you can use these [scripts](https://devcenter.bitrise.io/infrastructure/virtual-machines/#managing-java-versions) as a temporary workaround.
### Troubleshooting
If the Step fails to set the Java version, you can use these [scripts](https://devcenter.bitrise.io/infrastructure/virtual-machines/#managing-java-versions) as a temporary workaround.

### Useful links

[Managing Java versions on Bitrise](https://devcenter.bitrise.io/infrastructure/virtual-machines/#managing-java-versions)
- [Managing Java versions on Bitrise](https://devcenter.bitrise.io/infrastructure/virtual-machines/#managing-java-versions)

</details>

Expand All @@ -31,15 +34,15 @@ You can also run this step directly with [Bitrise CLI](https://github.com/bitris

| Key | Description | Flags | Default |
| --- | --- | --- | --- |
| `set_java_version` | Select the installed java version you want to use during the build run. You can check [here](https://github.com/bitrise-io/bitrise.io/tree/master/system_reports) which java versions are installed on each Bitrise stack. | required | `11` |
| `set_java_version` | Select the installed Java version you want to use during the build run. You can check [in system reports](https://github.com/bitrise-io/bitrise.io/tree/master/system_reports) which java versions are installed on each Bitrise stack. | required | `11` |
</details>

<details>
<summary>Outputs</summary>

| Environment Variable | Description |
| --- | --- |
| `JAVA_HOME` | JAVA_HOME is an environment variable points to the file system location where the JDK or JRE was installed. |
| `JAVA_HOME` | JAVA_HOME is an environment variable that points to the path where the JDK (Java Development Kit) is installed. |
</details>

## 🙋 Contributing
Expand Down
11 changes: 11 additions & 0 deletions e2e/bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ workflows:
inputs:
- set_java_version: 11

test_java17:
envs:
- EXPECTED_VERSION: 17
after_run:
- _check_version
steps:
- path::./:
title: Execute step
inputs:
- set_java_version: 17

_check_version:
summary: Compares current JDK version to $EXPECTED_VERSION
description: Based on https://stackoverflow.com/a/56243046
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module github.com/bitrise-steplib/bitrise-step-set-java-version

go 1.14
go 1.17

require (
github.com/bitrise-io/go-steputils v0.0.0-20210831050118-9a8de76b2f19
github.com/bitrise-io/go-utils v0.0.0-20210901122528-80582f68ed74
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.2
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.6
)

require (
github.com/bitrise-io/go-steputils v1.0.1 // indirect
github.com/bitrise-io/go-utils v1.0.1 // indirect
)
33 changes: 22 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
github.com/bitrise-io/go-steputils v0.0.0-20210831050118-9a8de76b2f19 h1:ZLuZ7EqpV2mgxEZTZrWxFjDo8t0F576QQLyschuuJRE=
github.com/bitrise-io/go-steputils v0.0.0-20210831050118-9a8de76b2f19/go.mod h1:deuDfnw5Hot7Uajr7gsM5hNKAlKS3iIAPpZgfZSDNOk=
github.com/bitrise-io/go-utils v0.0.0-20210830144330-49d11743a4fd/go.mod h1:Vi4MHnaZVL3PVoPPA/Yp6g2pzntkDH8LGiRSY7qw6KQ=
github.com/bitrise-io/go-utils v0.0.0-20210901122528-80582f68ed74 h1:zEcuic3XCiW16aDX/EII17WNljqA+Yg0Y3szG6ctAw0=
github.com/bitrise-io/go-utils v0.0.0-20210901122528-80582f68ed74/go.mod h1:Vi4MHnaZVL3PVoPPA/Yp6g2pzntkDH8LGiRSY7qw6KQ=
github.com/bitrise-io/go-steputils v1.0.1 h1:lwPl2W1njfANrBoTCkuqOOYbTha263ZFqoWQH0fwhaY=
github.com/bitrise-io/go-steputils v1.0.1/go.mod h1:YIUaQnIAyK4pCvQG0hYHVkSzKNT9uL2FWmkFNW4mfNI=
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.2 h1:WfhgPqLyg+VPNb6istzlJqalk81kb9Wt9IcQIQTOsxE=
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.2/go.mod h1:OC0mHpjD/bqmsHlhG+FWgTouBbcJvmyx896PDP3dRBs=
github.com/bitrise-io/go-utils v1.0.1 h1:e7mepVBkVN1DXRPESNXb0djEw6bxB6B93p/Q74zzcvk=
github.com/bitrise-io/go-utils v1.0.1/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0RSY3tRI1heY=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.1/go.mod h1:sy+Ir1X8P3tAAx/qU/r+hqDjHDcrMjIzDEvId1wqNc4=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.6 h1:hwxfNzeqqte0iMw558Z2n42WZwwuXAuuvluo766etQc=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.6/go.mod h1:mysJdafur1ytXda0TeRsV+GxYCJFDU0QcCmYBgQf2Fc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
105 changes: 35 additions & 70 deletions javasetter/javasetter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package javasetter

import (
"os"
"path/filepath"
"runtime"

"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/log"
)

// JavaVersion ...
Expand All @@ -15,6 +15,7 @@ type JavaVersion string
const (
JavaVersion8 = JavaVersion("8")
JavaVersion11 = JavaVersion("11")
JavaVersion17 = JavaVersion("17")
)

// Platform ...
Expand All @@ -33,23 +34,19 @@ func (j JavaSetter) platform() Platform {
return MacOS
}

// JavaSetter ...
type JavaSetter struct {
logger log.Logger
cmdFactory command.Factory
}

// New ...
func New(logger log.Logger, cmdFactory command.Factory) *JavaSetter {
return &JavaSetter{logger: logger, cmdFactory: cmdFactory}
}

// Result ...
type Result struct {
JavaHome string
}

// SetJava ...
func (j JavaSetter) SetJava(version JavaVersion) (Result, error) {
j.logger.Println()
j.logger.Infof("Checking platform")
Expand All @@ -69,108 +66,76 @@ func (j JavaSetter) SetJava(version JavaVersion) (Result, error) {

func (j JavaSetter) setJavaMac(version JavaVersion) (Result, error) {
if version == JavaVersion8 {
version = JavaVersion("1.8")
version = "1.8"
}

//
// jenv global
cmdJenv := j.cmdFactory.Create(
"jenv",
[]string{"global", string(version)},
&command.Opts{
Stdout: os.Stdout,
Stderr: os.Stderr,
})
cmdJenv := j.cmdFactory.Create("jenv", []string{"global", string(version)}, nil)

j.logger.Printf("$ %s", cmdJenv.PrintableCommandArgs())
if _, err := cmdJenv.RunAndReturnExitCode(); err != nil {
if output, err := cmdJenv.RunAndReturnTrimmedCombinedOutput(); err != nil {
j.logger.Warnf(output)
return Result{}, err
}

//
// jenv prefix
cmdPrefix := j.cmdFactory.Create(
"jenv",
[]string{"prefix"},
nil,
)
cmdPrefix := j.cmdFactory.Create("jenv", []string{"prefix"}, nil)

j.logger.Printf("$ %s", cmdPrefix.PrintableCommandArgs())
javaHome, err := cmdPrefix.RunAndReturnTrimmedOutput()
javaHome, err := cmdPrefix.RunAndReturnTrimmedCombinedOutput()
if err != nil {
j.logger.Warnf(javaHome)
return Result{}, err
}
return Result{JavaHome: javaHome}, nil
}

func (j JavaSetter) setJavaUbuntu(version JavaVersion) (Result, error) {
javaPath, javaCPath, javadocPath, javaHome := func() (string, string, string, string) {
switch version {
case JavaVersion8:
mainDir := "/usr/lib/jvm/java-8-openjdk-amd64"
return mainDir+"/jre/bin/java", mainDir+"/bin/javac", mainDir+"/bin/javadoc", mainDir
case JavaVersion11:
mainDir := "/usr/lib/jvm/java-11-openjdk-amd64"
return mainDir+"/bin/java", mainDir+"/bin/javac", mainDir+"/bin/javadoc", mainDir
default:
return "", "", "", ""
}
}()
var javaHome, javaPath string
switch version {
case JavaVersion8:
javaHome = "/usr/lib/jvm/java-8-openjdk-amd64"
javaPath = filepath.Join(javaHome, "jre/bin/java")
case JavaVersion11:
javaHome = "/usr/lib/jvm/java-11-openjdk-amd64"
javaPath = filepath.Join(javaHome, "bin/java")
case JavaVersion17:
javaHome = "/usr/lib/jvm/java-17-openjdk-amd64"
javaPath = filepath.Join(javaHome, "bin/java")
}

javacPath := filepath.Join(javaHome, "bin/javac")
javadocPath := filepath.Join(javaHome, "bin/javadoc")

//
// update-alternatives javac
cmd := j.cmdFactory.Create(
"sudo",
[]string{
"update-alternatives",
"--set",
"javac",
javaCPath,
},
&command.Opts{
Stdout: os.Stdout,
Stderr: os.Stderr,
},
)
cmd := j.cmdFactory.Create("sudo", []string{"update-alternatives", "--set", "javac", javacPath}, nil)

j.logger.Printf("$ %s", cmd.PrintableCommandArgs())
if _, err := cmd.RunAndReturnExitCode(); err != nil {
if output, err := cmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
j.logger.Warnf(output)
return Result{}, err
}

//
// update-alternatives java
cmd = j.cmdFactory.Create(
"sudo",
[]string{
"update-alternatives",
"--set",
"java",
string(javaPath),
},
nil,
)
cmd = j.cmdFactory.Create("sudo", []string{"update-alternatives", "--set", "java", javaPath}, nil)

j.logger.Printf("$ %s", cmd.PrintableCommandArgs())
if _, err := cmd.RunAndReturnExitCode(); err != nil {
if output, err := cmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
j.logger.Warnf(output)
return Result{}, err
}

//
// update-alternatives javadoc
cmd = j.cmdFactory.Create(
"sudo",
[]string{
"update-alternatives",
"--set",
"javadoc",
javadocPath,
},
nil,
)
cmd = j.cmdFactory.Create("sudo", []string{"update-alternatives", "--set", "javadoc", javadocPath}, nil)

j.logger.Printf("$ %s", cmd.PrintableCommandArgs())
if _, err := cmd.RunAndReturnExitCode(); err != nil {
if output, err := cmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
j.logger.Warnf(output)
return Result{}, err
}

Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"os"

"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/stepenv"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/env"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-steputils/v2/stepconf"
"github.com/bitrise-io/go-steputils/v2/stepenv"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/env"
"github.com/bitrise-io/go-utils/v2/log"
)

func main() {
Expand Down
Loading

0 comments on commit 207ceac

Please sign in to comment.