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

org.gradle.StartParameter Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.gradle.api.Incubating;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.configuration.ConsoleOutput;
import org.gradle.api.logging.configuration.LoggingConfiguration;
import org.gradle.api.logging.configuration.ShowStacktrace;
import org.gradle.api.logging.configuration.WarningMode;
import org.gradle.concurrent.ParallelismConfiguration;
import org.gradle.initialization.BuildLayoutParameters;
import org.gradle.initialization.CompositeInitScriptFinder;
import org.gradle.initialization.DistributionInitScriptFinder;
import org.gradle.initialization.UserHomeInitScriptFinder;
import org.gradle.internal.DefaultTaskExecutionRequest;
import org.gradle.internal.FileUtils;
import org.gradle.internal.concurrent.DefaultParallelismConfiguration;
import org.gradle.internal.logging.DefaultLoggingConfiguration;
import org.gradle.util.DeprecationLogger;

import javax.annotation.Nullable;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptyList;

/**
 * 

{@code StartParameter} defines the configuration used by a Gradle instance to execute a build. The properties of {@code StartParameter} generally correspond to the command-line options of * Gradle. * *

You can obtain an instance of a {@code StartParameter} by either creating a new one, or duplicating an existing one using {@link #newInstance} or {@link #newBuild}.

*/ public class StartParameter implements LoggingConfiguration, ParallelismConfiguration, Serializable { public static final String GRADLE_USER_HOME_PROPERTY_KEY = BuildLayoutParameters.GRADLE_USER_HOME_PROPERTY_KEY; /** * The default user home directory. */ public static final File DEFAULT_GRADLE_USER_HOME = new BuildLayoutParameters().getGradleUserHomeDir(); private final DefaultLoggingConfiguration loggingConfiguration = new DefaultLoggingConfiguration(); private final DefaultParallelismConfiguration parallelismConfiguration = new DefaultParallelismConfiguration(); private List taskRequests = new ArrayList(); private Set excludedTaskNames = new LinkedHashSet(); private boolean buildProjectDependencies = true; private File currentDir; private File projectDir; private boolean searchUpwards; private Map projectProperties = new HashMap(); private Map systemPropertiesArgs = new HashMap(); private File gradleUserHomeDir; protected File gradleHomeDir; private File settingsFile; private boolean useEmptySettings; private File buildFile; private List initScripts = new ArrayList(); private boolean dryRun; private boolean rerunTasks; private boolean profile; private boolean continueOnFailure; private boolean offline; private File projectCacheDir; private boolean refreshDependencies; private boolean recompileScripts; private boolean buildCacheEnabled; private boolean buildCacheDebugLogging; private boolean configureOnDemand; private boolean continuous; private List includedBuilds = new ArrayList(); private boolean buildScan; private boolean noBuildScan; private boolean interactive; private boolean writeDependencyLocks; private List lockedDependenciesToUpdate = emptyList(); /** * {@inheritDoc} */ @Override public LogLevel getLogLevel() { return loggingConfiguration.getLogLevel(); } /** * {@inheritDoc} */ @Override public void setLogLevel(LogLevel logLevel) { loggingConfiguration.setLogLevel(logLevel); } /** * {@inheritDoc} */ @Override public ShowStacktrace getShowStacktrace() { return loggingConfiguration.getShowStacktrace(); } /** * {@inheritDoc} */ @Override public void setShowStacktrace(ShowStacktrace showStacktrace) { loggingConfiguration.setShowStacktrace(showStacktrace); } /** * {@inheritDoc} */ @Override public ConsoleOutput getConsoleOutput() { return loggingConfiguration.getConsoleOutput(); } /** * {@inheritDoc} */ @Override public void setConsoleOutput(ConsoleOutput consoleOutput) { loggingConfiguration.setConsoleOutput(consoleOutput); } /** * {@inheritDoc} */ @Override public WarningMode getWarningMode() { return loggingConfiguration.getWarningMode(); } /** * {@inheritDoc} */ @Override public void setWarningMode(WarningMode warningMode) { loggingConfiguration.setWarningMode(warningMode); } /** * Sets the project's cache location. Set to null to use the default location. */ public void setProjectCacheDir(@Nullable File projectCacheDir) { this.projectCacheDir = projectCacheDir; } /** * Returns the project's cache dir. * * @return project's cache dir, or null if the default location is to be used. */ @Nullable public File getProjectCacheDir() { return projectCacheDir; } /** * Creates a {@code StartParameter} with default values. This is roughly equivalent to running Gradle on the command-line with no arguments. */ public StartParameter() { BuildLayoutParameters layoutParameters = new BuildLayoutParameters(); gradleHomeDir = layoutParameters.getGradleInstallationHomeDir(); searchUpwards = layoutParameters.getSearchUpwards(); currentDir = layoutParameters.getCurrentDir(); projectDir = layoutParameters.getProjectDir(); gradleUserHomeDir = layoutParameters.getGradleUserHomeDir(); } /** * Duplicates this {@code StartParameter} instance. * * @return the new parameters. */ public StartParameter newInstance() { return prepareNewInstance(new StartParameter()); } protected StartParameter prepareNewInstance(StartParameter p) { prepareNewBuild(p); p.setWarningMode(getWarningMode()); p.buildFile = buildFile; p.projectDir = projectDir; p.settingsFile = settingsFile; p.useEmptySettings = useEmptySettings; p.taskRequests = new ArrayList(taskRequests); p.excludedTaskNames = new LinkedHashSet(excludedTaskNames); p.buildProjectDependencies = buildProjectDependencies; p.currentDir = currentDir; p.searchUpwards = searchUpwards; p.projectProperties = new HashMap(projectProperties); p.systemPropertiesArgs = new HashMap(systemPropertiesArgs); p.initScripts = new ArrayList(initScripts); p.includedBuilds = new ArrayList(includedBuilds); p.dryRun = dryRun; p.projectCacheDir = projectCacheDir; return p; } /** *

Creates the parameters for a new build, using these parameters as a template. Copies the environmental properties from this parameter (eg Gradle user home dir, etc), but does not copy the * build specific properties (eg task names).

* * @return The new parameters. */ public StartParameter newBuild() { return prepareNewBuild(new StartParameter()); } protected StartParameter prepareNewBuild(StartParameter p) { p.gradleUserHomeDir = gradleUserHomeDir; p.gradleHomeDir = gradleHomeDir; p.setLogLevel(getLogLevel()); p.setConsoleOutput(getConsoleOutput()); p.setShowStacktrace(getShowStacktrace()); p.setWarningMode(getWarningMode()); p.profile = profile; p.continueOnFailure = continueOnFailure; p.offline = offline; p.rerunTasks = rerunTasks; p.recompileScripts = recompileScripts; p.refreshDependencies = refreshDependencies; p.setParallelProjectExecutionEnabled(isParallelProjectExecutionEnabled()); p.buildCacheEnabled = buildCacheEnabled; p.configureOnDemand = configureOnDemand; p.setMaxWorkerCount(getMaxWorkerCount()); p.systemPropertiesArgs = new HashMap(systemPropertiesArgs); p.interactive = interactive; p.writeDependencyLocks = writeDependencyLocks; p.lockedDependenciesToUpdate = new ArrayList(lockedDependenciesToUpdate); return p; } public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } /** * Returns the build file to use to select the default project. Returns null when the build file is not used to select the default project. * * @return The build file. May be null. */ @Nullable public File getBuildFile() { return buildFile; } /** * Sets the build file to use to select the default project. Use null to disable selecting the default project using the build file. * * @param buildFile The build file. May be null. */ public void setBuildFile(@Nullable File buildFile) { if (buildFile == null) { this.buildFile = null; setCurrentDir(null); } else { this.buildFile = FileUtils.canonicalize(buildFile); setProjectDir(this.buildFile.getParentFile()); } } /** * Specifies that an empty settings script should be used. * * This means that even if a settings file exists in the conventional location, or has been previously specified by {@link #setSettingsFile(File)}, it will not be used. * * If {@link #setSettingsFile(File)} is called after this, it will supersede calling this method. * * @return this */ public StartParameter useEmptySettings() { searchUpwards = false; useEmptySettings = true; settingsFile = null; return this; } /** * Returns whether an empty settings script will be used regardless of whether one exists in the default location. * * @return Whether to use empty settings or not. */ public boolean isUseEmptySettings() { return useEmptySettings; } /** * Returns the names of the tasks to execute in this build. When empty, the default tasks for the project will be executed. If {@link TaskExecutionRequest}s are set for this build then names from these task parameters are returned. * * @return the names of the tasks to execute in this build. Never returns null. */ public List getTaskNames() { List taskNames = Lists.newArrayList(); for (TaskExecutionRequest taskRequest : taskRequests) { taskNames.addAll(taskRequest.getArgs()); } return taskNames; } /** *

Sets the tasks to execute in this build. Set to an empty list, or null, to execute the default tasks for the project. The tasks are executed in the order provided, subject to dependency * between the tasks.

* * @param taskNames the names of the tasks to execute in this build. */ public void setTaskNames(@Nullable Iterable taskNames) { if (taskNames == null) { this.taskRequests = emptyList(); } else { this.taskRequests = Arrays.asList(new DefaultTaskExecutionRequest(taskNames)); } } /** * Returns the tasks to execute in this build. When empty, the default tasks for the project will be executed. * * @return the tasks to execute in this build. Never returns null. */ public List getTaskRequests() { return taskRequests; } /** *

Sets the task parameters to execute in this build. Set to an empty list, to execute the default tasks for the project. The tasks are executed in the order provided, subject to dependency * between the tasks.

* * @param taskParameters the tasks to execute in this build. */ public void setTaskRequests(Iterable taskParameters) { this.taskRequests = Lists.newArrayList(taskParameters); } /** * Returns the names of the tasks to be excluded from this build. When empty, no tasks are excluded from the build. * * @return The names of the excluded tasks. Returns an empty set if there are no such tasks. */ public Set getExcludedTaskNames() { return excludedTaskNames; } /** * Sets the tasks to exclude from this build. * * @param excludedTaskNames The task names. */ public void setExcludedTaskNames(Iterable excludedTaskNames) { this.excludedTaskNames = Sets.newLinkedHashSet(excludedTaskNames); } /** * Returns the directory to use to select the default project, and to search for the settings file. * * @return The current directory. Never returns null. */ public File getCurrentDir() { return currentDir; } /** * Sets the directory to use to select the default project, and to search for the settings file. Set to null to use the default current directory. * * @param currentDir The directory. Set to null to use the default. */ public void setCurrentDir(@Nullable File currentDir) { if (currentDir != null) { this.currentDir = FileUtils.canonicalize(currentDir); } else { this.currentDir = new BuildLayoutParameters().getCurrentDir(); } } public boolean isSearchUpwards() { return searchUpwards; } public void setSearchUpwards(boolean searchUpwards) { this.searchUpwards = searchUpwards; } public Map getProjectProperties() { return projectProperties; } public void setProjectProperties(Map projectProperties) { this.projectProperties = projectProperties; } public Map getSystemPropertiesArgs() { return systemPropertiesArgs; } public void setSystemPropertiesArgs(Map systemPropertiesArgs) { this.systemPropertiesArgs = systemPropertiesArgs; } /** * Returns the directory to use as the user home directory. * * @return The home directory. */ public File getGradleUserHomeDir() { return gradleUserHomeDir; } /** * Sets the directory to use as the user home directory. Set to null to use the default directory. * * @param gradleUserHomeDir The home directory. May be null. */ public void setGradleUserHomeDir(@Nullable File gradleUserHomeDir) { this.gradleUserHomeDir = gradleUserHomeDir == null ? new BuildLayoutParameters().getGradleUserHomeDir() : FileUtils.canonicalize(gradleUserHomeDir); } /** * Returns true if project dependencies are to be built, false if they should not be. The default is true. */ public boolean isBuildProjectDependencies() { return buildProjectDependencies; } /** * Specifies whether project dependencies should be built. Defaults to true. * * @return this */ public StartParameter setBuildProjectDependencies(boolean build) { this.buildProjectDependencies = build; return this; } public boolean isDryRun() { return dryRun; } public void setDryRun(boolean dryRun) { this.dryRun = dryRun; } /** * Sets the settings file to use for the build. Use null to use the default settings file. * * @param settingsFile The settings file to use. May be null. */ public void setSettingsFile(@Nullable File settingsFile) { if (settingsFile == null) { this.settingsFile = null; } else { this.useEmptySettings = false; this.settingsFile = FileUtils.canonicalize(settingsFile); currentDir = this.settingsFile.getParentFile(); } } /** * Returns the explicit settings file to use for the build, or null. * * Will return null if the default settings file is to be used. However, if {@link #isUseEmptySettings()} returns true, then no settings file at all will be used. * * @return The settings file. May be null. * @see #isUseEmptySettings() */ @Nullable public File getSettingsFile() { return settingsFile; } /** * Adds the given file to the list of init scripts that are run before the build starts. This list is in addition to the default init scripts. * * @param initScriptFile The init scripts. */ public void addInitScript(File initScriptFile) { initScripts.add(initScriptFile); } /** * Sets the list of init scripts to be run before the build starts. This list is in addition to the default init scripts. * * @param initScripts The init scripts. */ public void setInitScripts(List initScripts) { this.initScripts = initScripts; } /** * Returns all explicitly added init scripts that will be run before the build starts. This list does not contain the user init script located in ${user.home}/.gradle/init.gradle, even though * that init script will also be run. * * @return list of all explicitly added init scripts. */ public List getInitScripts() { return Collections.unmodifiableList(initScripts); } /** * Returns all init scripts, including explicit init scripts and implicit init scripts. * * @return All init scripts, including explicit init scripts and implicit init scripts. */ public List getAllInitScripts() { CompositeInitScriptFinder initScriptFinder = new CompositeInitScriptFinder( new UserHomeInitScriptFinder(getGradleUserHomeDir()), new DistributionInitScriptFinder(gradleHomeDir) ); List scripts = new ArrayList(getInitScripts()); initScriptFinder.findScripts(scripts); return Collections.unmodifiableList(scripts); } /** * Sets the project directory to use to select the default project. Use null to use the default criteria for selecting the default project. * * @param projectDir The project directory. May be null. */ public void setProjectDir(@Nullable File projectDir) { if (projectDir == null) { setCurrentDir(null); this.projectDir = null; } else { File canonicalFile = FileUtils.canonicalize(projectDir); currentDir = canonicalFile; this.projectDir = canonicalFile; } } /** * Returns the project dir to use to select the default project. * * Returns null when the build file is not used to select the default project * * @return The project dir. May be null. */ @Nullable public File getProjectDir() { return projectDir; } /** * Specifies if a profile report should be generated. * * @param profile true if a profile report should be generated */ public void setProfile(boolean profile) { this.profile = profile; } /** * Returns true if a profile report will be generated. */ public boolean isProfile() { return profile; } /** * Specifies whether the build should continue on task failure. The default is false. */ public boolean isContinueOnFailure() { return continueOnFailure; } /** * Specifies whether the build should continue on task failure. The default is false. */ public void setContinueOnFailure(boolean continueOnFailure) { this.continueOnFailure = continueOnFailure; } /** * Specifies whether the build should be performed offline (ie without network access). */ public boolean isOffline() { return offline; } /** * Specifies whether the build should be performed offline (ie without network access). */ public void setOffline(boolean offline) { this.offline = offline; } /** * Specifies whether the dependencies should be refreshed.. */ public boolean isRefreshDependencies() { return refreshDependencies; } /** * Specifies whether the dependencies should be refreshed.. */ public void setRefreshDependencies(boolean refreshDependencies) { this.refreshDependencies = refreshDependencies; } /** * Specifies whether the cached task results should be ignored and each task should be forced to be executed. */ public boolean isRerunTasks() { return rerunTasks; } /** * Specifies whether the cached task results should be ignored and each task should be forced to be executed. */ public void setRerunTasks(boolean rerunTasks) { this.rerunTasks = rerunTasks; } /** * Specifies whether to force the build scripts to be recompiled. * * @deprecated This flag is no longer used. */ @Deprecated public boolean isRecompileScripts() { DeprecationLogger.nagUserOfDiscontinuedMethod("StartParameter.isRecompileScripts()"); return recompileScripts; } /** * Specifies whether to force the build scripts to be recompiled. * * @deprecated This flag is no longer used and simply defaults to 'false'. */ @Deprecated public void setRecompileScripts(boolean recompileScripts) { DeprecationLogger.nagUserOfDiscontinuedMethod("StartParameter.setRecompileScripts()"); this.recompileScripts = recompileScripts; } /** * {@inheritDoc} */ @Override public boolean isParallelProjectExecutionEnabled() { return parallelismConfiguration.isParallelProjectExecutionEnabled(); } /** * {@inheritDoc} */ @Override public void setParallelProjectExecutionEnabled(boolean parallelProjectExecution) { parallelismConfiguration.setParallelProjectExecutionEnabled(parallelProjectExecution); } /** * Returns true if the build cache is enabled. * * @since 3.5 */ public boolean isBuildCacheEnabled() { return buildCacheEnabled; } /** * Enables/disables the build cache. * * @since 3.5 */ public void setBuildCacheEnabled(boolean buildCacheEnabled) { this.buildCacheEnabled = buildCacheEnabled; } /** * Whether build cache debug logging is enabled. * * @since 4.6 */ @Incubating public boolean isBuildCacheDebugLogging() { return buildCacheDebugLogging; } /** * Whether build cache debug logging is enabled. * * @since 4.6 */ @Incubating public void setBuildCacheDebugLogging(boolean buildCacheDebugLogging) { this.buildCacheDebugLogging = buildCacheDebugLogging; } /** * {@inheritDoc} */ @Override public int getMaxWorkerCount() { return parallelismConfiguration.getMaxWorkerCount(); } /** * {@inheritDoc} */ @Override public void setMaxWorkerCount(int maxWorkerCount) { parallelismConfiguration.setMaxWorkerCount(maxWorkerCount); } /** * If the configure-on-demand mode is active */ @Incubating public boolean isConfigureOnDemand() { return configureOnDemand; } @Override public String toString() { return "StartParameter{" + "taskRequests=" + taskRequests + ", excludedTaskNames=" + excludedTaskNames + ", currentDir=" + currentDir + ", searchUpwards=" + searchUpwards + ", projectProperties=" + projectProperties + ", systemPropertiesArgs=" + systemPropertiesArgs + ", gradleUserHomeDir=" + gradleUserHomeDir + ", gradleHome=" + gradleHomeDir + ", logLevel=" + getLogLevel() + ", showStacktrace=" + getShowStacktrace() + ", buildFile=" + buildFile + ", initScripts=" + initScripts + ", dryRun=" + dryRun + ", rerunTasks=" + rerunTasks + ", recompileScripts=" + recompileScripts + ", offline=" + offline + ", refreshDependencies=" + refreshDependencies + ", parallelProjectExecution=" + isParallelProjectExecutionEnabled() + ", configureOnDemand=" + configureOnDemand + ", maxWorkerCount=" + getMaxWorkerCount() + ", buildCacheEnabled=" + buildCacheEnabled + ", interactive=" + interactive + ", writeDependencyLocks=" + writeDependencyLocks + '}'; } /** * Package scope for testing purposes. */ void setGradleHomeDir(File gradleHomeDir) { this.gradleHomeDir = gradleHomeDir; } @Incubating public void setConfigureOnDemand(boolean configureOnDemand) { this.configureOnDemand = configureOnDemand; } public boolean isContinuous() { return continuous; } public void setContinuous(boolean enabled) { this.continuous = enabled; } public void includeBuild(File includedBuild) { includedBuilds.add(includedBuild); } public void setIncludedBuilds(List includedBuilds) { this.includedBuilds = includedBuilds; } public List getIncludedBuilds() { return Collections.unmodifiableList(includedBuilds); } /** * Returns true if build scan should be created. * * @since 3.4 */ public boolean isBuildScan() { return buildScan; } /** * Specifies whether a build scan should be created. * * @since 3.4 */ public void setBuildScan(boolean buildScan) { this.buildScan = buildScan; } /** * Returns true when build scan creation is explicitly disabled. * * @since 3.4 */ public boolean isNoBuildScan() { return noBuildScan; } /** * Specifies whether build scan creation is explicitly disabled. * * @since 3.4 */ public void setNoBuildScan(boolean noBuildScan) { this.noBuildScan = noBuildScan; } /** * Returns true when console is interactive. * * @since 4.3 * @deprecated This flag is no longer used and simply defaults to 'false'. */ @Deprecated public boolean isInteractive() { DeprecationLogger.nagUserOfDiscontinuedMethod("StartParameter.isInteractive()"); return interactive; } /** * Specifies whether console is interactive. * * @since 4.3 * @deprecated This flag is no longer used. */ @Deprecated public void setInteractive(boolean interactive) { DeprecationLogger.nagUserOfDiscontinuedMethod("StartParameter.setInteractive()"); this.interactive = interactive; } /** * Specifies whether dependency resolution needs to be persisted for locking * * @since 4.8 */ @Incubating public void setWriteDependencyLocks(boolean writeDependencyLocks) { this.writeDependencyLocks = writeDependencyLocks; } /** * Returns true when dependency resolution is to be persisted for locking * * @since 4.8 */ @Incubating public boolean isWriteDependencyLocks() { return writeDependencyLocks; } /** * Indicates that specified dependencies are to be allowed to update their version. * Implicitly activates dependency locking persistence. * * @param lockedDependenciesToUpdate the modules to update * @see #isWriteDependencyLocks() * * @since 4.8 */ @Incubating public void setLockedDependenciesToUpdate(List lockedDependenciesToUpdate) { this.lockedDependenciesToUpdate = Lists.newArrayList(lockedDependenciesToUpdate); this.writeDependencyLocks = true; } /** * Returns the list of modules that are to be allowed to update their version compared to the lockfile. * * @return a list of modules allowed to have a version update * * @since 4.8 */ @Incubating public List getLockedDependenciesToUpdate() { return lockedDependenciesToUpdate; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy