From ae4dba2bb87e53310c1949861c482ecfd0f812d0 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Sun, 9 May 2021 17:02:25 -0300 Subject: [PATCH] feat: CORDOVA_JAVA_HOME env variable (#1229) * feat: CORDOVA_JAVA_HOME env variable * refactor: Improve CORDOVA_JAVA_HOME env test * fix(test) path separator issue --- bin/templates/cordova/lib/env/java.js | 6 ++++-- spec/unit/java.spec.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bin/templates/cordova/lib/env/java.js b/bin/templates/cordova/lib/env/java.js index 90d53403..2cd720ca 100644 --- a/bin/templates/cordova/lib/env/java.js +++ b/bin/templates/cordova/lib/env/java.js @@ -94,8 +94,10 @@ const java = { } const javacPath = utils.forgivingWhichSync('javac'); - const hasJavaHome = !!environment.JAVA_HOME; - if (hasJavaHome) { + const javaHome = environment.CORDOVA_JAVA_HOME || environment.JAVA_HOME; + if (javaHome) { + // Ensure that CORDOVA_JAVA_HOME overrides + environment.JAVA_HOME = javaHome; // Windows java installer doesn't add javac to PATH, nor set JAVA_HOME (ugh). if (!javacPath) { environment.PATH += path.delimiter + path.join(environment.JAVA_HOME, 'bin'); diff --git a/spec/unit/java.spec.js b/spec/unit/java.spec.js index 0b011791..d19f1b06 100644 --- a/spec/unit/java.spec.js +++ b/spec/unit/java.spec.js @@ -76,6 +76,19 @@ describe('Java', () => { Java.__set__('javaIsEnsured', false); }); + it('CORDOVA_JAVA_HOME overrides JAVA_HOME', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue(''); + + const env = { + CORDOVA_JAVA_HOME: '/tmp/jdk' + }; + + await Java._ensure(env); + + expect(env.JAVA_HOME).toBe('/tmp/jdk'); + expect(env.PATH.split(path.delimiter)).toContain(['', 'tmp', 'jdk', 'bin'].join(path.sep)); + }); + it('with JAVA_HOME / without javac', async () => { spyOn(utils, 'forgivingWhichSync').and.returnValue('');