All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.gradleplugins.runnerkit.providers.JavaHomeProvider Maven / Gradle / Ivy

There is a newer version: 0.0.148
Show newest version
package dev.gradleplugins.runnerkit.providers;

import dev.gradleplugins.runnerkit.GradleExecutionContext;
import dev.gradleplugins.runnerkit.InvalidRunnerConfigurationException;
import org.apache.commons.lang3.SystemUtils;

import java.io.File;
import java.util.Collections;
import java.util.Map;

import static java.util.Collections.singletonMap;

public final class JavaHomeProvider extends AbstractGradleExecutionProvider implements GradleExecutionEnvironmentVariableProvider {
    public static JavaHomeProvider current() {
        return fixed(JavaHomeProvider.class, SystemUtils.getJavaHome());
    }

    public static JavaHomeProvider inherited() {
        return noValue(JavaHomeProvider.class);
    }

    @Override
    public Map getAsEnvironmentVariables() {
        return map(JavaHomeProvider::asEnvironmentVariables).orElseGet(Collections::emptyMap);
    }

    private static Map asEnvironmentVariables(File javaHomeDirectory) {
        return singletonMap("JAVA_HOME", javaHomeDirectory.getAbsolutePath());
    }

    @Override
    public void validate(GradleExecutionContext context) {
        if (context.getEnvironmentVariables().isPresent() && context.getEnvironmentVariables().get().containsKey("JAVA_HOME")) {
            throw new InvalidRunnerConfigurationException("Please use GradleRunner#withJavaHomeDirectory(File) instead of using environment variables.");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy