mirror of
https://github.com/actions/setup-java.git
synced 2026-06-23 16:20:28 +03:00
Add set-default option
This option allows to install an additional JDK without making it the default one. I have wanted this for quite a long time as I'm running custom GitHub Actions with Java, which might require a specific JDK and I don't want to pollute the JDK that is used by the overall workflow calling the action. And I'm apparently not alone as there was a preexisting issue. Fixes #560
This commit is contained in:
@@ -6,6 +6,7 @@ export const INPUT_JAVA_PACKAGE = 'java-package';
|
||||
export const INPUT_DISTRIBUTION = 'distribution';
|
||||
export const INPUT_JDK_FILE = 'jdkFile';
|
||||
export const INPUT_CHECK_LATEST = 'check-latest';
|
||||
export const INPUT_SET_DEFAULT = 'set-default';
|
||||
export const INPUT_SERVER_ID = 'server-id';
|
||||
export const INPUT_SERVER_USERNAME = 'server-username';
|
||||
export const INPUT_SERVER_PASSWORD = 'server-password';
|
||||
|
||||
@@ -20,6 +20,7 @@ export abstract class JavaBase {
|
||||
protected packageType: string;
|
||||
protected stable: boolean;
|
||||
protected checkLatest: boolean;
|
||||
protected setDefault: boolean;
|
||||
|
||||
constructor(
|
||||
protected distribution: string,
|
||||
@@ -36,6 +37,10 @@ export abstract class JavaBase {
|
||||
this.architecture = installerOptions.architecture || os.arch();
|
||||
this.packageType = installerOptions.packageType;
|
||||
this.checkLatest = installerOptions.checkLatest;
|
||||
this.setDefault =
|
||||
installerOptions.setDefault !== undefined
|
||||
? installerOptions.setDefault
|
||||
: true;
|
||||
}
|
||||
|
||||
protected abstract downloadTool(
|
||||
@@ -169,8 +174,15 @@ export abstract class JavaBase {
|
||||
foundJava.path = macOSPostfixPath;
|
||||
}
|
||||
|
||||
core.info(`Setting Java ${foundJava.version} as the default`);
|
||||
this.setJavaDefault(foundJava.version, foundJava.path);
|
||||
if (this.setDefault) {
|
||||
core.info(`Setting Java ${foundJava.version} as the default`);
|
||||
this.setJavaDefault(foundJava.version, foundJava.path);
|
||||
} else {
|
||||
core.info(
|
||||
`Installing Java ${foundJava.version} (not setting as default)`
|
||||
);
|
||||
this.setJavaEnvironment(foundJava.version, foundJava.path);
|
||||
}
|
||||
|
||||
return foundJava;
|
||||
}
|
||||
@@ -310,6 +322,17 @@ export abstract class JavaBase {
|
||||
);
|
||||
}
|
||||
|
||||
protected setJavaEnvironment(version: string, toolPath: string) {
|
||||
const majorVersion = version.split('.')[0];
|
||||
core.setOutput('distribution', this.distribution);
|
||||
core.setOutput('path', toolPath);
|
||||
core.setOutput('version', version);
|
||||
core.exportVariable(
|
||||
`JAVA_HOME_${majorVersion}_${this.architecture.toUpperCase()}`,
|
||||
toolPath
|
||||
);
|
||||
}
|
||||
|
||||
protected distributionArchitecture(): string {
|
||||
// default mappings of config architectures to distribution architectures
|
||||
// override if a distribution uses any different names; see liberica for an example
|
||||
|
||||
@@ -3,6 +3,7 @@ export interface JavaInstallerOptions {
|
||||
architecture: string;
|
||||
packageType: string;
|
||||
checkLatest: boolean;
|
||||
setDefault?: boolean;
|
||||
}
|
||||
|
||||
export interface JavaInstallerResults {
|
||||
|
||||
@@ -69,9 +69,15 @@ export class LocalDistribution extends JavaBase {
|
||||
foundJava.path = macOSPostfixPath;
|
||||
}
|
||||
|
||||
core.info(`Setting Java ${foundJava.version} as default`);
|
||||
|
||||
this.setJavaDefault(foundJava.version, foundJava.path);
|
||||
if (this.setDefault) {
|
||||
core.info(`Setting Java ${foundJava.version} as the default`);
|
||||
this.setJavaDefault(foundJava.version, foundJava.path);
|
||||
} else {
|
||||
core.info(
|
||||
`Installing Java ${foundJava.version} (not setting as default)`
|
||||
);
|
||||
this.setJavaEnvironment(foundJava.version, foundJava.path);
|
||||
}
|
||||
return foundJava;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ async function run() {
|
||||
constants.INPUT_CACHE_DEPENDENCY_PATH
|
||||
);
|
||||
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);
|
||||
const setDefault = getBooleanInput(constants.INPUT_SET_DEFAULT, true);
|
||||
let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID);
|
||||
|
||||
core.startGroup('Installed distributions');
|
||||
@@ -44,6 +45,7 @@ async function run() {
|
||||
architecture,
|
||||
packageType,
|
||||
checkLatest,
|
||||
setDefault,
|
||||
distributionName,
|
||||
jdkFile,
|
||||
toolchainIds
|
||||
@@ -100,6 +102,7 @@ async function installVersion(
|
||||
architecture,
|
||||
packageType,
|
||||
checkLatest,
|
||||
setDefault,
|
||||
toolchainIds
|
||||
} = options;
|
||||
|
||||
@@ -107,6 +110,7 @@ async function installVersion(
|
||||
architecture,
|
||||
packageType,
|
||||
checkLatest,
|
||||
setDefault,
|
||||
version
|
||||
};
|
||||
|
||||
@@ -141,6 +145,7 @@ interface installerInputsOptions {
|
||||
architecture: string;
|
||||
packageType: string;
|
||||
checkLatest: boolean;
|
||||
setDefault: boolean;
|
||||
distributionName: string;
|
||||
jdkFile: string;
|
||||
toolchainIds: Array<string>;
|
||||
|
||||
Reference in New Issue
Block a user