org.apache.maven.shared.invoker.MavenCommandLineBuilder Maven / Gradle / Ivy
package org.apache.maven.shared.invoker;
/*
* 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.
*/
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.maven.shared.invoker.InvocationRequest.CheckSumPolicy;
import org.apache.maven.shared.invoker.InvocationRequest.ReactorFailureBehavior;
import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.cli.CommandLineException;
import org.apache.maven.shared.utils.cli.CommandLineUtils;
import org.apache.maven.shared.utils.cli.Commandline;
/**
* MavenCommandLineBuilder class.
*/
public class MavenCommandLineBuilder
{
private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
private InvokerLogger logger = DEFAULT_LOGGER;
private File workingDirectory;
private File localRepositoryDirectory;
private File mavenHome;
private File mavenExecutable;
private Properties systemEnvVars;
/**
* build.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @return a {@link org.apache.maven.shared.utils.cli.Commandline} object.
* @throws org.apache.maven.shared.invoker.CommandLineConfigurationException if any.
*/
public Commandline build( InvocationRequest request )
throws CommandLineConfigurationException
{
try
{
checkRequiredState();
}
catch ( IOException e )
{
throw new CommandLineConfigurationException( e.getMessage(), e );
}
File mvn;
try
{
mvn = findMavenExecutable();
}
catch ( IOException e )
{
throw new CommandLineConfigurationException( e.getMessage(), e );
}
Commandline cli = new Commandline();
cli.setExecutable( mvn.getAbsolutePath() );
// handling for OS-level envars
setShellEnvironment( request, cli );
// interactive, offline, update-snapshots,
// debug/show-errors, checksum policy
setFlags( request, cli );
// failure behavior and [eventually] forced-reactor
// includes/excludes, etc.
setReactorBehavior( request, cli );
// working directory and local repository location
setEnvironmentPaths( request, cli );
// pom-file and basedir handling
setPomLocation( request, cli );
setSettingsLocation( request, cli );
setToolchainsLocation( request, cli );
setProperties( request, cli );
setProfiles( request, cli );
setGoals( request, cli );
setThreads( request, cli );
return cli;
}
/**
* checkRequiredState.
*
* @throws java.io.IOException if any.
*/
protected void checkRequiredState()
throws IOException
{
if ( logger == null )
{
throw new IllegalStateException( "A logger instance is required." );
}
if ( ( mavenHome == null ) && ( System.getProperty( "maven.home" ) == null ) )
// can be restored with 1.5
// && ( System.getenv( "M2_HOME" ) != null ) )
{
if ( !getSystemEnvVars().containsKey( "M2_HOME" ) )
{
throw new IllegalStateException( "Maven application directory was not "
+ "specified, and ${maven.home} is not provided in the system "
+ "properties. Specify at least one of these." );
}
}
}
/**
* setSettingsLocation.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setSettingsLocation( InvocationRequest request, Commandline cli )
{
File userSettingsFile = request.getUserSettingsFile();
if ( userSettingsFile != null )
{
try
{
userSettingsFile = userSettingsFile.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize user settings path: " + userSettingsFile.getAbsolutePath()
+ ". Using as-is.", e );
}
cli.createArg().setValue( "-s" );
cli.createArg().setValue( userSettingsFile.getPath() );
}
File globalSettingsFile = request.getGlobalSettingsFile();
if ( globalSettingsFile != null )
{
try
{
globalSettingsFile = globalSettingsFile.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize global settings path: " + globalSettingsFile.getAbsolutePath()
+ ". Using as-is.", e );
}
cli.createArg().setValue( "-gs" );
cli.createArg().setValue( globalSettingsFile.getPath() );
}
}
/**
* setToolchainsLocation.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setToolchainsLocation( InvocationRequest request, Commandline cli )
{
File toolchainsFile = request.getToolchainsFile();
if ( toolchainsFile != null )
{
try
{
toolchainsFile = toolchainsFile.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize toolchains path: " + toolchainsFile.getAbsolutePath()
+ ". Using as-is.", e );
}
cli.createArg().setValue( "-t" );
cli.createArg().setValue( toolchainsFile.getPath() );
}
}
/**
* setShellEnvironment.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
* @throws org.apache.maven.shared.invoker.CommandLineConfigurationException if any.
*/
protected void setShellEnvironment( InvocationRequest request, Commandline cli )
throws CommandLineConfigurationException
{
if ( request.isShellEnvironmentInherited() )
{
try
{
cli.addSystemEnvironment();
cli.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
// MSHARED-261: Ensure M2_HOME is not inherited, but gets a
// proper value
cli.addEnvironment( "M2_HOME", getMavenHome().getAbsolutePath() );
}
catch ( RuntimeException e )
{
throw e;
}
catch ( Exception e )
{
throw new IllegalStateException(
"Unknown error retrieving shell environment variables. Reason: " + e.getMessage(), e );
}
}
if ( request.getJavaHome() != null )
{
cli.addEnvironment( "JAVA_HOME", request.getJavaHome().getAbsolutePath() );
}
if ( request.getMavenOpts() != null )
{
cli.addEnvironment( "MAVEN_OPTS", request.getMavenOpts() );
}
for ( Map.Entry entry : request.getShellEnvironments().entrySet() )
{
cli.addEnvironment( entry.getKey(), entry.getValue() );
}
}
/**
* setProfiles.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setProfiles( InvocationRequest request, Commandline cli )
{
List profiles = request.getProfiles();
if ( ( profiles != null ) && !profiles.isEmpty() )
{
cli.createArg().setValue( "-P" );
cli.createArg().setValue( StringUtils.join( profiles.iterator(), "," ) );
}
}
/**
* setGoals.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
* @throws org.apache.maven.shared.invoker.CommandLineConfigurationException if any.
*/
protected void setGoals( InvocationRequest request, Commandline cli ) throws CommandLineConfigurationException
{
List goals = request.getGoals();
if ( ( goals != null ) && !goals.isEmpty() )
{
try
{
cli.createArg().setLine( StringUtils.join( goals.iterator(), " " ) );
}
catch ( CommandLineException e )
{
throw new CommandLineConfigurationException( "Problem to set goals: " + e.getMessage(), e );
}
}
}
/**
* setProperties.
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setProperties( InvocationRequest request, Commandline cli )
{
Properties properties = request.getProperties();
if ( properties != null )
{
for ( Entry