Coverage Report - org.apache.maven.cli.MavenCli
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenCli
12%
32/271
6%
9/148
6,467
 
 1  
 package org.apache.maven.cli;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.text.SimpleDateFormat;
 26  
 import java.util.Arrays;
 27  
 import java.util.Date;
 28  
 import java.util.Iterator;
 29  
 import java.util.Locale;
 30  
 import java.util.Properties;
 31  
 import java.util.StringTokenizer;
 32  
 import java.util.Map.Entry;
 33  
 
 34  
 import org.apache.commons.cli.CommandLine;
 35  
 import org.apache.commons.cli.ParseException;
 36  
 import org.apache.maven.Maven;
 37  
 import org.apache.maven.SettingsConfigurationException;
 38  
 import org.apache.maven.artifact.manager.WagonManager;
 39  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 40  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 41  
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 42  
 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
 43  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 44  
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 45  
 import org.apache.maven.execution.MavenExecutionRequest;
 46  
 import org.apache.maven.execution.ReactorManager;
 47  
 import org.apache.maven.monitor.event.DefaultEventDispatcher;
 48  
 import org.apache.maven.monitor.event.DefaultEventMonitor;
 49  
 import org.apache.maven.monitor.event.EventDispatcher;
 50  
 import org.apache.maven.plugin.Mojo;
 51  
 import org.apache.maven.profiles.DefaultProfileManager;
 52  
 import org.apache.maven.profiles.ProfileManager;
 53  
 import org.apache.maven.reactor.MavenExecutionException;
 54  
 import org.apache.maven.settings.MavenSettingsBuilder;
 55  
 import org.apache.maven.settings.RuntimeInfo;
 56  
 import org.apache.maven.settings.Settings;
 57  
 import org.codehaus.classworlds.ClassWorld;
 58  
 import org.codehaus.plexus.PlexusContainerException;
 59  
 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
 60  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 61  
 import org.codehaus.plexus.embed.Embedder;
 62  
 import org.codehaus.plexus.logging.Logger;
 63  
 import org.codehaus.plexus.logging.LoggerManager;
 64  
 import org.codehaus.plexus.util.IOUtil;
 65  
 import org.codehaus.plexus.util.Os;
 66  
 import org.codehaus.plexus.util.StringUtils;
 67  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 68  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 69  
 import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
 70  
 import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
 71  
 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
 72  
 import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
 73  
 import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
 74  
 
 75  
 /**
 76  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 77  
  * @version $Id: MavenCli.java 746212 2009-02-20 11:36:05Z brett $
 78  
  * @noinspection UseOfSystemOutOrSystemErr,ACCESS_STATIC_VIA_INSTANCE
 79  
  */
 80  0
 public class MavenCli
 81  
 {
 82  
     /** @deprecated use {@link Os#OS_NAME} */
 83  1
     public static final String OS_NAME = Os.OS_NAME;
 84  
 
 85  
     /** @deprecated use {@link Os#OS_ARCH} */
 86  1
     public static final String OS_ARCH = Os.OS_ARCH;
 87  
 
 88  
     /** @deprecated use {@link Os#OS_VERSION} */
 89  1
     public static final String OS_VERSION = Os.OS_VERSION;
 90  
 
 91  
     private static Embedder embedder;
 92  
 
 93  
     public static void main( String[] args )
 94  
     {
 95  0
         ClassWorld classWorld = new ClassWorld( "plexus.core", Thread.currentThread().getContextClassLoader() );
 96  
 
 97  0
         int result = main( args, classWorld );
 98  
 
 99  0
         System.exit( result );
 100  0
     }
 101  
 
 102  
     /**
 103  
      * @noinspection ConfusingMainMethod
 104  
      */
 105  
     public static int main( String[] args, ClassWorld classWorld )
 106  
     {
 107  
         // ----------------------------------------------------------------------
 108  
         // Setup the command line parser
 109  
         // ----------------------------------------------------------------------
 110  
 
 111  0
         CLIManager cliManager = new CLIManager();
 112  
 
 113  
         CommandLine commandLine;
 114  
         try
 115  
         {
 116  0
             commandLine = cliManager.parse( args );
 117  
         }
 118  0
         catch ( ParseException e )
 119  
         {
 120  0
             System.err.println( "Unable to parse command line options: " + e.getMessage() );
 121  0
             cliManager.displayHelp();
 122  0
             return 1;
 123  0
         }
 124  
 
 125  0
         boolean debug = commandLine.hasOption( CLIManager.DEBUG );
 126  
 
 127  0
         boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS );
 128  
 
 129  0
         if ( showErrors )
 130  
         {
 131  0
             System.out.println( "+ Error stacktraces are turned on." );
 132  
         }
 133  
 
 134  
         // ----------------------------------------------------------------------
 135  
         // Process particular command line options
 136  
         // ----------------------------------------------------------------------
 137  
 
 138  0
         if ( commandLine.hasOption( CLIManager.HELP ) )
 139  
         {
 140  0
             cliManager.displayHelp();
 141  0
             return 0;
 142  
         }
 143  
         
 144  0
         if ( commandLine.hasOption( CLIManager.VERSION ) )
 145  
         {
 146  0
             showVersion();
 147  
 
 148  0
             return 0;
 149  
         }
 150  0
         else if ( debug || commandLine.hasOption( CLIManager.SHOW_VERSION ) )
 151  
         {
 152  0
             showVersion();
 153  
         }
 154  
 
 155  0
         EventDispatcher eventDispatcher = new DefaultEventDispatcher();
 156  
 
 157  
         // Make sure the Maven home directory is an absolute path to save us from confusion with say drive-relative
 158  
         // Windows paths.
 159  0
         String mavenHome = System.getProperty( "maven.home" );
 160  0
         if ( mavenHome != null )
 161  
         {
 162  0
             System.setProperty( "maven.home", new File( mavenHome ).getAbsolutePath() );
 163  
         }
 164  
 
 165  
         // ----------------------------------------------------------------------
 166  
         // Now that we have everything that we need we will fire up plexus and
 167  
         // bring the maven component to life for use.
 168  
         // ----------------------------------------------------------------------
 169  
 
 170  0
         embedder = new Embedder();
 171  
 
 172  
         try
 173  
         {
 174  0
             embedder.start( classWorld );
 175  
         }
 176  0
         catch ( PlexusContainerException e )
 177  
         {
 178  0
             showFatalError( "Unable to start the embedded plexus container", e, showErrors );
 179  
 
 180  0
             return 1;
 181  0
         }
 182  
 
 183  
         // ----------------------------------------------------------------------
 184  
         // The execution properties need to be created before the settings
 185  
         // are constructed.
 186  
         // ----------------------------------------------------------------------
 187  
 
 188  0
         Properties executionProperties = new Properties();
 189  0
         Properties userProperties = new Properties();
 190  0
         populateProperties( commandLine, executionProperties, userProperties );
 191  
 
 192  
         Settings settings;
 193  
 
 194  
         try
 195  
         {
 196  0
             settings = buildSettings( commandLine );
 197  
         }
 198  0
         catch ( SettingsConfigurationException e )
 199  
         {
 200  0
             showError( "Error reading settings.xml: " + e.getMessage(), e, showErrors );
 201  
 
 202  0
             return 1;
 203  
         }
 204  0
         catch ( ComponentLookupException e )
 205  
         {
 206  0
             showFatalError( "Unable to read settings.xml", e, showErrors );
 207  
 
 208  0
             return 1;
 209  0
         }
 210  
 
 211  
         DefaultSecDispatcher dispatcher;
 212  
         try
 213  
         {
 214  0
             if ( commandLine.hasOption( CLIManager.ENCRYPT_MASTER_PASSWORD ) )
 215  
             {
 216  0
                 String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_MASTER_PASSWORD );
 217  
 
 218  0
                 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
 219  
 
 220  0
                 System.out.println( cipher.encryptAndDecorate( passwd,
 221  
                                                                DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) );
 222  
                 
 223  0
                 return 0;
 224  
             }
 225  0
             else if ( commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
 226  
             {
 227  0
                 String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_PASSWORD );
 228  
                 
 229  0
                 dispatcher = (DefaultSecDispatcher) embedder.lookup( SecDispatcher.ROLE );
 230  0
                 String configurationFile = dispatcher.getConfigurationFile();
 231  0
                 if ( configurationFile.startsWith( "~" ) )
 232  
                 {
 233  0
                     configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 );
 234  
                 }
 235  0
                 String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile );
 236  0
                 embedder.release( dispatcher );
 237  
                 
 238  0
                 String master = null;
 239  
                 
 240  0
                 SettingsSecurity sec = SecUtil.read( file, true );
 241  0
                 if ( sec != null )
 242  
                 {
 243  0
                     master = sec.getMaster();
 244  
                 }
 245  
 
 246  0
                 if ( master == null )
 247  
                 {
 248  0
                     System.err.println( "Master password is not set in the setting security file" );
 249  
                     
 250  0
                     return 1;
 251  
                 }
 252  
                 
 253  0
                 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
 254  0
                 String masterPasswd =
 255  
                     cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION );
 256  0
                 System.out.println( cipher.encryptAndDecorate( passwd, masterPasswd ) );
 257  
                 
 258  0
                 return 0;
 259  
             }
 260  
         }
 261  0
         catch ( Exception e )
 262  
         {
 263  0
             showFatalError( "Error encrypting password: " + e.getMessage(), e, showErrors );
 264  
             
 265  0
             return 1;
 266  0
         }
 267  
             
 268  0
         Maven maven = null;
 269  
 
 270  0
         MavenExecutionRequest request = null;
 271  
 
 272  0
         LoggerManager loggerManager = null;
 273  
 
 274  
         try
 275  
         {
 276  
             // logger must be created first
 277  0
             loggerManager = (LoggerManager) embedder.lookup( LoggerManager.ROLE );
 278  
 
 279  0
             if ( debug )
 280  
             {
 281  0
                 loggerManager.setThreshold( Logger.LEVEL_DEBUG );
 282  
             }
 283  0
             else if ( commandLine.hasOption( CLIManager.QUIET ) )
 284  
             {
 285  
                 // TODO: we need to do some more work here. Some plugins use sys out or log errors at info level.
 286  
                 // Ideally, we could use Warn across the board
 287  0
                 loggerManager.setThreshold( Logger.LEVEL_ERROR );
 288  
                 // TODO:Additionally, we can't change the mojo level because the component key includes the version and it isn't known ahead of time. This seems worth changing.
 289  
             }
 290  
 
 291  0
             ProfileManager profileManager = new DefaultProfileManager( embedder.getContainer(), executionProperties );
 292  
 
 293  0
             if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) )
 294  
             {
 295  0
                 String [] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES );
 296  
 
 297  0
                 if ( profileOptionValues != null )
 298  
                 {
 299  0
                     for ( int i=0; i < profileOptionValues.length; ++i )
 300  
                     {
 301  0
                         StringTokenizer profileTokens = new StringTokenizer( profileOptionValues[i], "," );
 302  
 
 303  0
                         while ( profileTokens.hasMoreTokens() )
 304  
                         {
 305  0
                             String profileAction = profileTokens.nextToken().trim();
 306  
 
 307  0
                             if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) )
 308  
                             {
 309  0
                                 profileManager.explicitlyDeactivate( profileAction.substring( 1 ) );
 310  
                             }
 311  0
                             else if ( profileAction.startsWith( "+" ) )
 312  
                             {
 313  0
                                 profileManager.explicitlyActivate( profileAction.substring( 1 ) );
 314  
                             }
 315  
                             else
 316  
                             {
 317  0
                                 profileManager.explicitlyActivate( profileAction );
 318  
                             }
 319  
                         }
 320  
                     }
 321  
                 }
 322  
             }
 323  
 
 324  0
             request = createRequest( commandLine, settings, eventDispatcher, loggerManager, profileManager,
 325  
                                      executionProperties, userProperties, showErrors );
 326  
 
 327  0
             setProjectFileOptions( commandLine, request );
 328  
 
 329  0
             maven =
 330  
                 createMavenInstance( settings.isInteractiveMode(),
 331  
                                      loggerManager.getLoggerForComponent( WagonManager.ROLE ) );
 332  0
         }
 333  0
         catch ( ComponentLookupException e )
 334  
         {
 335  0
             showFatalError( "Unable to configure the Maven application", e, showErrors );
 336  
 
 337  0
             return 1;
 338  
         }
 339  
         finally
 340  
         {
 341  0
             if ( loggerManager != null )
 342  
             {
 343  
                 try
 344  
                 {
 345  0
                     embedder.release( loggerManager );
 346  
                 }
 347  0
                 catch ( ComponentLifecycleException e )
 348  
                 {
 349  0
                     showFatalError( "Error releasing logging manager", e, showErrors );
 350  0
                 }
 351  
             }
 352  0
         }
 353  
 
 354  
         try
 355  
         {
 356  0
             maven.execute( request );
 357  
         }
 358  0
         catch ( MavenExecutionException e )
 359  
         {
 360  0
             return 1;
 361  0
         }
 362  
 
 363  0
         return 0;
 364  
     }
 365  
 
 366  
     private static Settings buildSettings( CommandLine commandLine )
 367  
         throws ComponentLookupException, SettingsConfigurationException
 368  
     {
 369  0
         String userSettingsPath = null;
 370  
 
 371  0
         if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_SETTINGS ) )
 372  
         {
 373  0
             userSettingsPath = commandLine.getOptionValue( CLIManager.ALTERNATE_USER_SETTINGS );
 374  
         }
 375  
 
 376  0
         if ( commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) )
 377  
         {
 378  0
             String globalSettingsPath = commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_SETTINGS );
 379  0
             System.setProperty( MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION, globalSettingsPath );
 380  
         }
 381  
 
 382  0
         Settings settings = null;
 383  
 
 384  0
         MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE );
 385  
 
 386  
         try
 387  
         {
 388  0
             if ( userSettingsPath != null )
 389  
             {
 390  0
                 File userSettingsFile = new File( userSettingsPath );
 391  
 
 392  0
                 if ( userSettingsFile.exists() && !userSettingsFile.isDirectory() )
 393  
                 {
 394  0
                     settings = settingsBuilder.buildSettings( userSettingsFile );
 395  
                 }
 396  
                 else
 397  
                 {
 398  0
                     System.out.println( "WARNING: Alternate user settings file: " + userSettingsPath +
 399  
                         " is invalid. Using default path." );
 400  
                 }
 401  
             }
 402  
 
 403  0
             if ( settings == null )
 404  
             {
 405  0
                 settings = settingsBuilder.buildSettings();
 406  
             }
 407  
         }
 408  0
         catch ( IOException e )
 409  
         {
 410  0
             throw new SettingsConfigurationException( "Error reading settings file", e );
 411  
         }
 412  0
         catch ( XmlPullParserException e )
 413  
         {
 414  0
             throw new SettingsConfigurationException( e.getMessage(), e, e.getLineNumber(),
 415  
                                                       e.getColumnNumber() );
 416  0
         }
 417  
 
 418  
         // why aren't these part of the runtime info? jvz.
 419  
 
 420  0
         if ( commandLine.hasOption( CLIManager.BATCH_MODE ) )
 421  
         {
 422  0
             settings.setInteractiveMode( false );
 423  
         }
 424  
 
 425  0
         if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_REGISTRY ) )
 426  
         {
 427  0
             settings.setUsePluginRegistry( false );
 428  
         }
 429  
 
 430  
         // Create settings runtime info
 431  
 
 432  0
         settings.setRuntimeInfo( createRuntimeInfo( commandLine, settings ) );
 433  
 
 434  0
         return settings;
 435  
     }
 436  
 
 437  
     private static RuntimeInfo createRuntimeInfo( CommandLine commandLine, Settings settings )
 438  
     {
 439  0
         RuntimeInfo runtimeInfo = new RuntimeInfo( settings );
 440  
 
 441  0
         if ( commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES ) ||
 442  
             commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES2 ) )
 443  
         {
 444  0
             runtimeInfo.setPluginUpdateOverride( Boolean.TRUE );
 445  
         }
 446  0
         else if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_UPDATES ) )
 447  
         {
 448  0
             runtimeInfo.setPluginUpdateOverride( Boolean.FALSE );
 449  
         }
 450  
 
 451  0
         return runtimeInfo;
 452  
     }
 453  
 
 454  
 
 455  
     private static void showFatalError( String message, Exception e, boolean show )
 456  
     {
 457  0
         System.err.println( "FATAL ERROR: " + message );
 458  0
         if ( show )
 459  
         {
 460  0
             System.err.println( "Error stacktrace:" );
 461  
 
 462  0
             e.printStackTrace();
 463  
         }
 464  
         else
 465  
         {
 466  0
             System.err.println( "For more information, run with the -e flag" );
 467  
         }
 468  0
     }
 469  
 
 470  
     private static void showError( String message, Exception e, boolean show )
 471  
     {
 472  0
         System.err.println( message );
 473  0
         if ( show )
 474  
         {
 475  0
             System.err.println( "Error stacktrace:" );
 476  
 
 477  0
             e.printStackTrace();
 478  
         }
 479  0
     }
 480  
 
 481  
     private static MavenExecutionRequest createRequest( CommandLine commandLine, Settings settings,
 482  
                                                         EventDispatcher eventDispatcher, LoggerManager loggerManager,
 483  
                                                         ProfileManager profileManager, Properties executionProperties,
 484  
                                                         Properties userProperties, boolean showErrors )
 485  
         throws ComponentLookupException
 486  
     {
 487  
         MavenExecutionRequest request;
 488  
 
 489  0
         ArtifactRepository localRepository = createLocalRepository( embedder, settings, commandLine );
 490  
 
 491  0
         File userDir = new File( System.getProperty( "user.dir" ) );
 492  
 
 493  0
         request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher,
 494  
                                                     commandLine.getArgList(), userDir.getPath(), profileManager,
 495  
                                                     executionProperties, userProperties, showErrors );
 496  
 
 497  
         // TODO [BP]: do we set one per mojo? where to do it?
 498  0
         Logger logger = loggerManager.getLoggerForComponent( Mojo.ROLE );
 499  
 
 500  0
         if ( logger != null )
 501  
         {
 502  0
             request.addEventMonitor( new DefaultEventMonitor( logger ) );
 503  
         }
 504  
 
 505  0
         if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) )
 506  
         {
 507  0
             request.setRecursive( false );
 508  
         }
 509  
 
 510  0
         if ( commandLine.hasOption( CLIManager.FAIL_FAST ) )
 511  
         {
 512  0
             request.setFailureBehavior( ReactorManager.FAIL_FAST );
 513  
         }
 514  0
         else if ( commandLine.hasOption( CLIManager.FAIL_AT_END ) )
 515  
         {
 516  0
             request.setFailureBehavior( ReactorManager.FAIL_AT_END );
 517  
         }
 518  0
         else if ( commandLine.hasOption( CLIManager.FAIL_NEVER ) )
 519  
         {
 520  0
             request.setFailureBehavior( ReactorManager.FAIL_NEVER );
 521  
         }
 522  
 
 523  0
         return request;
 524  
     }
 525  
 
 526  
     private static void setProjectFileOptions( CommandLine commandLine, MavenExecutionRequest request )
 527  
     {
 528  0
         if ( commandLine.hasOption( CLIManager.REACTOR ) )
 529  
         {
 530  0
             request.setReactorActive( true );
 531  
         }
 532  0
         else if ( commandLine.hasOption( CLIManager.ALTERNATE_POM_FILE ) )
 533  
         {
 534  0
             request.setPomFile( commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE ) );
 535  
         }
 536  
         
 537  0
         if ( commandLine.hasOption( CLIManager.RESUME_FROM ) )
 538  
         {
 539  0
             request.setResumeFrom( commandLine.getOptionValue( CLIManager.RESUME_FROM ) );
 540  
         }
 541  
         
 542  0
         if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) )
 543  
         {
 544  0
             String projectList = commandLine.getOptionValue( CLIManager.PROJECT_LIST );
 545  0
             String[] projects = StringUtils.split( projectList, "," );
 546  0
             request.setSelectedProjects( Arrays.asList( projects ) );
 547  
         }
 548  
         
 549  0
         if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && !commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
 550  
         {
 551  0
             request.setMakeBehavior( ReactorManager.MAKE_MODE );
 552  
         }
 553  0
         else if ( !commandLine.hasOption( CLIManager.ALSO_MAKE )
 554  
             && commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
 555  
         {
 556  0
             request.setMakeBehavior( ReactorManager.MAKE_DEPENDENTS_MODE );
 557  
         }
 558  0
         if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
 559  
         {
 560  0
             request.setMakeBehavior( ReactorManager.MAKE_BOTH_MODE );
 561  
         }
 562  0
     }
 563  
 
 564  
     private static Maven createMavenInstance( boolean interactive, Logger logger )
 565  
         throws ComponentLookupException
 566  
     {
 567  
         // TODO [BP]: doing this here as it is CLI specific, though it doesn't feel like the right place (likewise logger).
 568  0
         WagonManager wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE );
 569  0
         if ( interactive )
 570  
         {
 571  0
             wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor( logger ) );
 572  
         }
 573  
         else
 574  
         {
 575  0
             wagonManager.setDownloadMonitor( new BatchModeDownloadMonitor( logger ) );
 576  
         }
 577  
 
 578  0
         wagonManager.setInteractive( interactive );
 579  
 
 580  0
         return (Maven) embedder.lookup( Maven.ROLE );
 581  
     }
 582  
 
 583  
     private static ArtifactRepository createLocalRepository( Embedder embedder, Settings settings,
 584  
                                                              CommandLine commandLine )
 585  
         throws ComponentLookupException
 586  
     {
 587  
         // TODO: release
 588  
         // TODO: something in plexus to show all active hooks?
 589  0
         ArtifactRepositoryLayout repositoryLayout =
 590  
             (ArtifactRepositoryLayout) embedder.lookup( ArtifactRepositoryLayout.ROLE, "default" );
 591  
 
 592  0
         ArtifactRepositoryFactory artifactRepositoryFactory =
 593  
             (ArtifactRepositoryFactory) embedder.lookup( ArtifactRepositoryFactory.ROLE );
 594  
 
 595  0
         String url = settings.getLocalRepository();
 596  
 
 597  0
         if ( !url.startsWith( "file:" ) )
 598  
         {
 599  0
             url = "file://" + url;
 600  
         }
 601  
 
 602  0
         ArtifactRepository localRepository = new DefaultArtifactRepository( "local", url, repositoryLayout );
 603  
 
 604  0
         boolean snapshotPolicySet = false;
 605  
 
 606  0
         if ( commandLine.hasOption( CLIManager.OFFLINE ) )
 607  
         {
 608  0
             settings.setOffline( true );
 609  
 
 610  0
             snapshotPolicySet = true;
 611  
         }
 612  
 
 613  0
         if ( !snapshotPolicySet && commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) )
 614  
         {
 615  0
             artifactRepositoryFactory.setGlobalUpdatePolicy( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS );
 616  
         }
 617  
 
 618  0
         if ( commandLine.hasOption( CLIManager.CHECKSUM_FAILURE_POLICY ) )
 619  
         {
 620  0
             System.out.println( "+ Enabling strict checksum verification on all artifact downloads." );
 621  
 
 622  0
             artifactRepositoryFactory.setGlobalChecksumPolicy( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
 623  
         }
 624  0
         else if ( commandLine.hasOption( CLIManager.CHECKSUM_WARNING_POLICY ) )
 625  
         {
 626  0
             System.out.println( "+ Disabling strict checksum verification on all artifact downloads." );
 627  
 
 628  0
             artifactRepositoryFactory.setGlobalChecksumPolicy( ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
 629  
         }
 630  
 
 631  0
         return localRepository;
 632  
     }
 633  
 
 634  
     static Properties getBuildProperties()
 635  
     {
 636  1
         Properties properties = new Properties();
 637  1
         InputStream resourceAsStream = null;
 638  
         try
 639  
         {
 640  2
             resourceAsStream = MavenCli.class.getClassLoader().getResourceAsStream( "org/apache/maven/messages/build.properties" );
 641  
 
 642  1
             if ( resourceAsStream != null )
 643  
             {
 644  1
                 properties.load( resourceAsStream );
 645  
             }
 646  
         }
 647  0
         catch ( IOException e )
 648  
         {
 649  0
             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
 650  
         }
 651  
         finally
 652  
         {
 653  1
             IOUtil.close( resourceAsStream );
 654  1
         }
 655  
 
 656  1
         return properties;
 657  
     }
 658  
 
 659  
     private static void showVersion()
 660  
     {
 661  0
         Properties properties = getBuildProperties();
 662  
 
 663  0
         String timestamp = reduce( properties.getProperty( "timestamp" ) );
 664  0
         String version = reduce( properties.getProperty( "version" ) );
 665  0
         String rev = reduce( properties.getProperty( "buildNumber" ) );
 666  
 
 667  0
         String msg = "Apache Maven ";
 668  0
         msg += ( version != null ? version : "<version unknown>" );
 669  0
         if ( rev != null || timestamp != null )
 670  
         {
 671  0
             msg += " (";
 672  0
             msg += ( rev != null ? "r" + rev : "" );
 673  0
             if ( timestamp != null )
 674  
             {
 675  0
                 SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
 676  0
                 String ts = fmt.format( new Date( Long.valueOf( timestamp ).longValue() ) );
 677  0
                 msg += ( rev != null ? "; " : "" ) + ts;
 678  
             }
 679  0
             msg += ")";
 680  
         }
 681  
 
 682  0
         System.out.println( msg );
 683  
 
 684  0
         System.out.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );
 685  
 
 686  0
         System.out.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) );
 687  
 
 688  0
         System.out.println( "Default locale: " + Locale.getDefault() + ", platform encoding: "
 689  
                             + System.getProperty( "file.encoding", "<unknown encoding>" ) );
 690  
 
 691  0
         System.out.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION +
 692  
                             "\" arch: \"" + Os.OS_ARCH + "\" Family: \"" + Os.OS_FAMILY + "\"" );
 693  0
     }
 694  
 
 695  
     private static String reduce( String s )
 696  
     {
 697  0
         return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
 698  
     }
 699  
 
 700  
     // ----------------------------------------------------------------------
 701  
     // System properties handling
 702  
     // ----------------------------------------------------------------------
 703  
 
 704  
     static void populateProperties( CommandLine commandLine, Properties executionProperties, Properties userProperties )
 705  
     {
 706  
         // add the env vars to the property set, with the "env." prefix
 707  
         // XXX support for env vars should probably be removed from the ModelInterpolator
 708  
         try
 709  
         {
 710  1
             Properties envVars = CommandLineUtils.getSystemEnvVars();
 711  1
             Iterator i = envVars.entrySet().iterator();
 712  39
             while ( i.hasNext() )
 713  
             {
 714  38
                 Entry e = (Entry) i.next();
 715  38
                 executionProperties.setProperty( "env." + e.getKey().toString(), e.getValue().toString() );
 716  
             }
 717  
         }
 718  0
         catch ( IOException e )
 719  
         {
 720  0
             System.err.println( "Error getting environment vars for profile activation: " + e );
 721  1
         }
 722  
 
 723  
         // ----------------------------------------------------------------------
 724  
         // Options that are set on the command line become system properties
 725  
         // and therefore are set in the session properties. System properties
 726  
         // are most dominant.
 727  
         // ----------------------------------------------------------------------
 728  
 
 729  1
         if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
 730  
         {
 731  1
             String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
 732  
 
 733  1
             if ( defStrs != null )
 734  
             {
 735  3
                 for ( int i = 0; i < defStrs.length; ++i )
 736  
                 {
 737  2
                     setCliProperty( defStrs[i], userProperties );
 738  
                 }
 739  
             }
 740  
 
 741  1
             executionProperties.putAll( userProperties );
 742  
         }
 743  
 
 744  1
         executionProperties.putAll( System.getProperties() );
 745  1
     }
 746  
 
 747  
     private static void setCliProperty( String property, Properties requestProperties )
 748  
     {
 749  
         String name;
 750  
 
 751  
         String value;
 752  
 
 753  2
         int i = property.indexOf( "=" );
 754  
 
 755  2
         if ( i <= 0 )
 756  
         {
 757  0
             name = property.trim();
 758  
 
 759  0
             value = "true";
 760  
         }
 761  
         else
 762  
         {
 763  2
             name = property.substring( 0, i ).trim();
 764  
 
 765  2
             value = property.substring( i + 1 ).trim();
 766  
         }
 767  
 
 768  2
         requestProperties.setProperty( name, value );
 769  
 
 770  
         // ----------------------------------------------------------------------
 771  
         // I'm leaving the setting of system properties here as not to break
 772  
         // the SystemPropertyProfileActivator. This won't harm embedding. jvz.
 773  
         // ----------------------------------------------------------------------
 774  
 
 775  2
         System.setProperty( name, value );
 776  2
     }
 777  
 }