diff --git a/action.yml b/action.yml index 0969d5d..7a326fd 100644 --- a/action.yml +++ b/action.yml @@ -43,9 +43,21 @@ inputs: description: 'Path to where the settings.xml file will be written. Default is ~/.m2.' required: false overwrite-settings: - description: 'Overwrite the settings.xml file if it exists. Default is "true".' + description: 'Overwrite the settings.xml file if it exists. Default is "!update-toolchains-only". If explcitly set "true", it will update settings.xml regardless of "update-toolchains-only"' required: false - default: true + # DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'! + update-toolchains-only: + description: 'Update toolchains.xml only. Default is "false". No update of settings.xml, no update of JAVA_HOME, no adding to PATH by default - unless "overwrite-settings", "update-env-javahome" or "add-to-env-path" are not explicitly set "true"' + required: false + default: false + update-env-javahome: + description: 'Update the JAVA_HOME environment variable. Default is "!update-toolchains-only"' + required: false + # DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'! + add-to-env-path: + description: 'Add "/bin" to the PATH environment variable. Default is "!update-toolchains-only"' + required: false + # DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'! gpg-private-key: description: 'GPG private key to import. Default is empty string.' required: false diff --git a/src/auth.ts b/src/auth.ts index c8ea629..66cdd9a 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -10,17 +10,15 @@ import * as constants from './constants'; import * as gpg from './gpg'; import {getBooleanInput} from './util'; -export async function configureAuthentication() { +export async function configureAuthentication( + overwriteSettings: boolean +) { const id = core.getInput(constants.INPUT_SERVER_ID); const username = core.getInput(constants.INPUT_SERVER_USERNAME); const password = core.getInput(constants.INPUT_SERVER_PASSWORD); const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), constants.M2_DIR); - const overwriteSettings = getBooleanInput( - constants.INPUT_OVERWRITE_SETTINGS, - true - ); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; diff --git a/src/constants.ts b/src/constants.ts index 93af286..52a0055 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -11,6 +11,9 @@ export const INPUT_SERVER_USERNAME = 'server-username'; export const INPUT_SERVER_PASSWORD = 'server-password'; export const INPUT_SETTINGS_PATH = 'settings-path'; export const INPUT_OVERWRITE_SETTINGS = 'overwrite-settings'; +export const INPUT_UPDATE_TOOLCHAINS_ONLY = 'update-toolchains-only'; +export const INPUT_UPDATE_JAVA_HOME = 'update-env-javahome'; +export const INPUT_ADD_TO_PATH = 'add-to-env-path'; export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key'; export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; diff --git a/src/distributions/base-installer.ts b/src/distributions/base-installer.ts index 258ac9f..d4589bc 100644 --- a/src/distributions/base-installer.ts +++ b/src/distributions/base-installer.ts @@ -10,7 +10,11 @@ import { JavaInstallerOptions, JavaInstallerResults } from './base-models'; -import {MACOS_JAVA_CONTENT_POSTFIX} from '../constants'; +import { + MACOS_JAVA_CONTENT_POSTFIX, + INPUT_UPDATE_JAVA_HOME, + INPUT_ADD_TO_PATH +} from '../constants'; import os from 'os'; export abstract class JavaBase { @@ -20,6 +24,8 @@ export abstract class JavaBase { protected packageType: string; protected stable: boolean; protected checkLatest: boolean; + protected updateEnvJavaHome: boolean; + protected addToEnvPath: boolean; constructor( protected distribution: string, @@ -36,6 +42,8 @@ export abstract class JavaBase { this.architecture = installerOptions.architecture || os.arch(); this.packageType = installerOptions.packageType; this.checkLatest = installerOptions.checkLatest; + this.updateEnvJavaHome = installerOptions.updateEnvJavaHome; + this.addToEnvPath = installerOptions.addToEnvPath; } protected abstract downloadTool( @@ -163,8 +171,16 @@ export abstract class JavaBase { protected setJavaDefault(version: string, toolPath: string) { const majorVersion = version.split('.')[0]; - core.exportVariable('JAVA_HOME', toolPath); - core.addPath(path.join(toolPath, 'bin')); + if (this.updateEnvJavaHome) { + core.exportVariable('JAVA_HOME', toolPath); + } else { + core.info(`Skip updating env.JAVA_HOME according to ${INPUT_UPDATE_JAVA_HOME}`); + } + if (this.addToEnvPath) { + core.addPath(path.join(toolPath, 'bin')); + } else { + core.info(`Skip adding to env.PATH according to ${INPUT_ADD_TO_PATH}`); + } core.setOutput('distribution', this.distribution); core.setOutput('path', toolPath); core.setOutput('version', version); diff --git a/src/distributions/base-models.ts b/src/distributions/base-models.ts index 82344d5..605e9a3 100644 --- a/src/distributions/base-models.ts +++ b/src/distributions/base-models.ts @@ -3,6 +3,8 @@ export interface JavaInstallerOptions { architecture: string; packageType: string; checkLatest: boolean; + updateEnvJavaHome: boolean; + addToEnvPath: boolean; } export interface JavaInstallerResults { diff --git a/src/setup-java.ts b/src/setup-java.ts index 73baf33..0aaa46f 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -13,6 +13,19 @@ import * as path from 'path'; import {getJavaDistribution} from './distributions/distribution-factory'; import {JavaInstallerOptions} from './distributions/base-models'; +interface IInstallerInputsOptions { + architecture: string; + packageType: string; + checkLatest: boolean; + distributionName: string; + jdkFile: string; + toolchainIds: Array; + updateToolchainsOnly: boolean; + overwriteSettings: boolean; + updateEnvJavaHome: boolean; + addToEnvPath: boolean; +} + async function run() { try { const versions = core.getMultilineInput(constants.INPUT_JAVA_VERSION); @@ -28,6 +41,11 @@ async function run() { constants.INPUT_CACHE_DEPENDENCY_PATH ); const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false); + const updateToolchainsOnly = getBooleanInput(constants.INPUT_UPDATE_TOOLCHAINS_ONLY, false); + const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, !updateToolchainsOnly); + const updateEnvJavaHome = getBooleanInput(constants.INPUT_UPDATE_JAVA_HOME, !updateToolchainsOnly); + const addToEnvPath = getBooleanInput(constants.INPUT_ADD_TO_PATH, !updateToolchainsOnly); + let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID); core.startGroup('Installed distributions'); @@ -40,13 +58,17 @@ async function run() { throw new Error('java-version or java-version-file input expected'); } - const installerInputsOptions: installerInputsOptions = { + const installerInputsOptions: IInstallerInputsOptions = { architecture, packageType, checkLatest, distributionName, jdkFile, - toolchainIds + toolchainIds, + updateToolchainsOnly, + overwriteSettings, + updateEnvJavaHome, + addToEnvPath }; if (!versions.length) { @@ -78,7 +100,7 @@ async function run() { const matchersPath = path.join(__dirname, '..', '..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); - await auth.configureAuthentication(); + await auth.configureAuthentication(overwriteSettings); if (cache && isCacheFeatureAvailable()) { await restore(cache, cacheDependencyPath); } @@ -91,7 +113,7 @@ run(); async function installVersion( version: string, - options: installerInputsOptions, + options: IInstallerInputsOptions, toolchainId = 0 ) { const { @@ -100,14 +122,20 @@ async function installVersion( architecture, packageType, checkLatest, - toolchainIds + toolchainIds, + updateToolchainsOnly, + overwriteSettings, + updateEnvJavaHome, + addToEnvPath } = options; const installerOptions: JavaInstallerOptions = { + version, architecture, packageType, checkLatest, - version + updateEnvJavaHome, + addToEnvPath }; const distribution = getJavaDistribution( @@ -126,6 +154,7 @@ async function installVersion( version, distributionName, result.path, + overwriteSettings || updateToolchainsOnly, toolchainIds[toolchainId] ); @@ -136,12 +165,3 @@ async function installVersion( core.info(` Path: ${result.path}`); core.info(''); } - -interface installerInputsOptions { - architecture: string; - packageType: string; - checkLatest: boolean; - distributionName: string; - jdkFile: string; - toolchainIds: Array; -} diff --git a/src/toolchains.ts b/src/toolchains.ts index cbb667e..3238d29 100644 --- a/src/toolchains.ts +++ b/src/toolchains.ts @@ -19,6 +19,7 @@ export async function configureToolchains( version: string, distributionName: string, jdkHome: string, + updateToolchains: boolean, toolchainId?: string ) { const vendor = @@ -27,10 +28,6 @@ export async function configureToolchains( const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), constants.M2_DIR); - const overwriteSettings = getBooleanInput( - constants.INPUT_OVERWRITE_SETTINGS, - true - ); await createToolchainsSettings({ jdkInfo: { @@ -40,21 +37,21 @@ export async function configureToolchains( jdkHome }, settingsDirectory, - overwriteSettings + updateToolchains }); } export async function createToolchainsSettings({ jdkInfo, settingsDirectory, - overwriteSettings + updateToolchains }: { jdkInfo: JdkInfo; settingsDirectory: string; - overwriteSettings: boolean; + updateToolchains: boolean; }) { core.info( - `Creating ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}` + `Adding a toolchain entry in ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}` ); // when an alternate m2 location is specified use only that location (no .m2 directory) // otherwise use the home/.m2/ path @@ -72,7 +69,7 @@ export async function createToolchainsSettings({ await writeToolchainsFileToDisk( settingsDirectory, updatedToolchains, - overwriteSettings + updateToolchains ); } @@ -147,17 +144,17 @@ async function readExistingToolchainsFile(directory: string) { async function writeToolchainsFileToDisk( directory: string, settings: string, - overwriteSettings: boolean + updateToolchains: boolean ) { const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE); - const settingsExists = fs.existsSync(location); - if (settingsExists && overwriteSettings) { - core.info(`Overwriting existing file ${location}`); - } else if (!settingsExists) { - core.info(`Writing to ${location}`); + const toolchainsExists = fs.existsSync(location); + if (toolchainsExists && updateToolchains) { + core.info(`Updating existing file ${location}`); + } else if (!toolchainsExists) { + core.info(`Creating file ${location}`); } else { core.info( - `Skipping generation of ${location} because file already exists and overwriting is not enabled` + `Skipping update of ${location} since file already exists and updating is not enabled` ); return; }