Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.maven.cli;
import java.io.BufferedInputStream;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import com.google.inject.AbstractModule;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.maven.BuildAbort;
import org.apache.maven.InternalErrorException;
import org.apache.maven.Maven;
import org.apache.maven.building.FileSource;
import org.apache.maven.building.Problem;
import org.apache.maven.building.Source;
import org.apache.maven.cli.configuration.ConfigurationProcessor;
import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor;
import org.apache.maven.cli.event.DefaultEventSpyContext;
import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader;
import org.apache.maven.cli.logging.Slf4jConfiguration;
import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.cli.logging.Slf4jStdoutLogger;
import org.apache.maven.cli.transfer.*;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
import org.apache.maven.exception.DefaultExceptionHandler;
import org.apache.maven.exception.ExceptionHandler;
import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequestPopulationException;
import org.apache.maven.execution.MavenExecutionRequestPopulator;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.extension.internal.CoreExtensionEntry;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.properties.internal.EnvironmentUtils;
import org.apache.maven.properties.internal.SystemProperties;
import org.apache.maven.session.scope.internal.SessionScopeModule;
import org.apache.maven.shared.utils.logging.MessageBuilder;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
import org.apache.maven.toolchain.building.ToolchainsBuilder;
import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.transfer.TransferListener;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
import static org.apache.maven.cli.CLIManager.COLOR;
import static org.apache.maven.cli.ResolveFile.resolveFile;
import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
// TODO push all common bits back to plexus cli and prepare for transition to Guice. We don't need 50 ways to make CLIs
/**
* @author Jason van Zyl
*/
public class MavenCli {
public static final String LOCAL_REPO_PROPERTY = "maven.repo.local";
public static final String MULTIMODULE_PROJECT_DIRECTORY = "maven.multiModuleProjectDirectory";
public static final String USER_HOME = System.getProperty("user.home");
public static final File USER_MAVEN_CONFIGURATION_HOME = new File(USER_HOME, ".m2");
public static final File DEFAULT_USER_TOOLCHAINS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "toolchains.xml");
public static final File DEFAULT_GLOBAL_TOOLCHAINS_FILE =
new File(System.getProperty("maven.conf"), "toolchains.xml");
private static final String EXT_CLASS_PATH = "maven.ext.class.path";
private static final String DOT_MVN = ".mvn";
private static final String UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE = "Unable to find the root directory. Create a "
+ DOT_MVN + " directory in the project root directory to identify it.";
private static final String EXTENSIONS_FILENAME = DOT_MVN + "/extensions.xml";
private static final String MVN_MAVEN_CONFIG = DOT_MVN + "/maven.config";
public static final String STYLE_COLOR_PROPERTY = "style.color";
private ClassWorld classWorld;
private LoggerManager plexusLoggerManager;
private ILoggerFactory slf4jLoggerFactory;
private Logger slf4jLogger;
private EventSpyDispatcher eventSpyDispatcher;
private ModelProcessor modelProcessor;
private Maven maven;
private MavenExecutionRequestPopulator executionRequestPopulator;
private ToolchainsBuilder toolchainsBuilder;
private DefaultSecDispatcher dispatcher;
private Map configurationProcessors;
private CLIManager cliManager;
private static final Pattern NEXT_LINE = Pattern.compile("\r?\n");
public MavenCli() {
this(null);
}
// This supports painless invocation by the Verifier during embedded execution of the core ITs
public MavenCli(ClassWorld classWorld) {
this.classWorld = classWorld;
}
public static void main(String[] args) {
int result = main(args, null);
System.exit(result);
}
public static int main(String[] args, ClassWorld classWorld) {
MavenCli cli = new MavenCli();
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
int result = cli.doMain(new CliRequest(args, classWorld));
MessageUtils.systemUninstall();
return result;
}
// TODO need to externalize CliRequest
public static int doMain(String[] args, ClassWorld classWorld) {
MavenCli cli = new MavenCli();
return cli.doMain(new CliRequest(args, classWorld));
}
/**
* This supports painless invocation by the Verifier during embedded execution of the core ITs.
* See
* Embedded3xLauncher in maven-verifier
*/
public int doMain(String[] args, String workingDirectory, PrintStream stdout, PrintStream stderr) {
PrintStream oldout = System.out;
PrintStream olderr = System.err;
final Set realms;
if (classWorld != null) {
realms = new HashSet<>();
for (ClassRealm realm : classWorld.getRealms()) {
realms.add(realm.getId());
}
} else {
realms = Collections.emptySet();
}
try {
if (stdout != null) {
System.setOut(stdout);
}
if (stderr != null) {
System.setErr(stderr);
}
CliRequest cliRequest = new CliRequest(args, classWorld);
cliRequest.workingDirectory = workingDirectory;
return doMain(cliRequest);
} finally {
if (classWorld != null) {
for (ClassRealm realm : new ArrayList<>(classWorld.getRealms())) {
String realmId = realm.getId();
if (!realms.contains(realmId)) {
try {
classWorld.disposeRealm(realmId);
} catch (NoSuchRealmException ignored) {
// can't happen
}
}
}
}
System.setOut(oldout);
System.setErr(olderr);
}
}
// TODO need to externalize CliRequest
public int doMain(CliRequest cliRequest) {
PlexusContainer localContainer = null;
try {
initialize(cliRequest);
cli(cliRequest);
properties(cliRequest);
logging(cliRequest);
informativeCommands(cliRequest);
version(cliRequest);
localContainer = container(cliRequest);
commands(cliRequest);
configure(cliRequest);
toolchains(cliRequest);
populateRequest(cliRequest);
encryption(cliRequest);
return execute(cliRequest);
} catch (ExitException e) {
return e.exitCode;
} catch (UnrecognizedOptionException e) {
// pure user error, suppress stack trace
return 1;
} catch (BuildAbort e) {
CLIReportingUtils.showError(slf4jLogger, "ABORTED", e, cliRequest.showErrors);
return 2;
} catch (Exception e) {
CLIReportingUtils.showError(slf4jLogger, "Error executing Maven.", e, cliRequest.showErrors);
return 1;
} finally {
if (localContainer != null) {
localContainer.dispose();
}
}
}
void initialize(CliRequest cliRequest) throws ExitException {
if (cliRequest.workingDirectory == null) {
cliRequest.workingDirectory = System.getProperty("user.dir");
}
if (cliRequest.multiModuleProjectDirectory == null) {
String basedirProperty = System.getProperty(MULTIMODULE_PROJECT_DIRECTORY);
if (basedirProperty == null) {
System.err.format("-D%s system property is not set.", MULTIMODULE_PROJECT_DIRECTORY);
throw new ExitException(1);
}
File basedir = basedirProperty != null ? new File(basedirProperty) : new File("");
try {
cliRequest.multiModuleProjectDirectory = basedir.getCanonicalFile();
} catch (IOException e) {
cliRequest.multiModuleProjectDirectory = basedir.getAbsoluteFile();
}
}
// We need to locate the top level project which may be pointed at using
// the -f/--file option. However, the command line isn't parsed yet, so
// we need to iterate through the args to find it and act upon it.
Path topDirectory = Paths.get(cliRequest.workingDirectory);
boolean isAltFile = false;
for (String arg : cliRequest.args) {
if (isAltFile) {
// this is the argument following -f/--file
Path path = topDirectory.resolve(arg);
if (Files.isDirectory(path)) {
topDirectory = path;
} else if (Files.isRegularFile(path)) {
topDirectory = path.getParent();
if (!Files.isDirectory(topDirectory)) {
System.err.println("Directory " + topDirectory
+ " extracted from the -f/--file command-line argument " + arg + " does not exist");
throw new ExitException(1);
}
} else {
System.err.println(
"POM file " + arg + " specified with the -f/--file command line argument does not exist");
throw new ExitException(1);
}
break;
} else {
// Check if this is the -f/--file option
isAltFile = arg.equals(String.valueOf(CLIManager.ALTERNATE_POM_FILE)) || arg.equals("file");
}
}
topDirectory = getCanonicalPath(topDirectory);
cliRequest.topDirectory = topDirectory;
// We're very early in the process and we don't have the container set up yet,
// so we on searchAcceptableRootDirectory method to find us acceptable directory.
// The method may return null if nothing acceptable found.
cliRequest.rootDirectory = searchAcceptableRootDirectory(topDirectory);
//
// Make sure the Maven home directory is an absolute path to save us from confusion with say drive-relative
// Windows paths.
//
String mavenHome = System.getProperty("maven.home");
if (mavenHome != null) {
System.setProperty("maven.home", new File(mavenHome).getAbsolutePath());
}
}
void cli(CliRequest cliRequest) throws Exception {
//
// Parsing errors can happen during the processing of the arguments and we prefer not having to check if
// the logger is null and construct this so we can use an SLF4J logger everywhere.
//
slf4jLogger = new Slf4jStdoutLogger();
cliManager = new CLIManager();
CommandLine mavenConfig = null;
try {
File configFile = new File(cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG);
if (configFile.isFile()) {
try (Stream lines = Files.lines(configFile.toPath(), Charset.defaultCharset())) {
String[] args = lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#"))
.toArray(String[]::new);
mavenConfig = cliManager.parse(args);
List> unrecognized = mavenConfig.getArgList();
if (!unrecognized.isEmpty()) {
// This file can only contain options, not args (goals or phases)
throw new ParseException("Unrecognized maven.config file entries: " + unrecognized);
}
}
}
} catch (ParseException e) {
System.err.println("Unable to parse maven.config file options: " + e.getMessage());
cliManager.displayHelp(System.out);
throw e;
}
try {
CommandLine mavenCli = cliManager.parse(cliRequest.args);
if (mavenConfig == null) {
cliRequest.commandLine = mavenCli;
} else {
cliRequest.commandLine = cliMerge(mavenConfig, mavenCli);
}
} catch (ParseException e) {
System.err.println("Unable to parse command line options: " + e.getMessage());
cliManager.displayHelp(System.out);
throw e;
}
// check for presence of unsupported command line options
try {
if (cliRequest.commandLine.hasOption("llr")) {
throw new UnrecognizedOptionException("Option '-llr' is not supported starting with Maven 3.9.1");
}
} catch (ParseException e) {
System.err.println("Unsupported options: " + e.getMessage());
cliManager.displayHelp(System.out);
throw e;
}
}
private void informativeCommands(CliRequest cliRequest) throws ExitException {
if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
cliManager.displayHelp(System.out);
throw new ExitException(0);
}
if (cliRequest.commandLine.hasOption(CLIManager.VERSION)) {
if (cliRequest.commandLine.hasOption(CLIManager.QUIET)) {
System.out.println(CLIReportingUtils.showVersionMinimal());
} else {
System.out.println(CLIReportingUtils.showVersion());
}
throw new ExitException(0);
}
}
private CommandLine cliMerge(CommandLine mavenConfig, CommandLine mavenCli) {
CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
// the args are easy, CLI only since maven.config file can only contain options
for (String arg : mavenCli.getArgs()) {
commandLineBuilder.addArg(arg);
}
/* Although this looks wrong in terms of order Commons CLI stores the value of options in
* an array and when a value is potentionally overriden it is added to the array. The single
* arg option value is retrieved and instead of returning values[values.length-1] it returns
* values[0] which means that the original value instead of the overridden one is returned
* (first wins). With properties values are truely overriden since at the end a map is used
* to merge which means last wins.
*
* TODO Report this behavioral bug with Commons CLI
*/
// now add all options, except for user properties with CLI first then maven.config file
List